Skip to main content

mir_analyzer/
suppression.rs

1//! Inline issue suppression via source comments.
2//!
3//! Lets users silence a single false positive without touching `mir.xml` or a
4//! baseline file. A [`SuppressionMap`] is built once per file from its source
5//! text and consulted as a final post-filter over the analyzer's issues
6//! (`batch.rs`), so it applies uniformly across every emitting pass —
7//! body analysis, the collector, class checks and dead-code detection.
8//!
9//! ## Recognised directives
10//!
11//! Native (preferred), matching the existing `@mir-check` convention:
12//!
13//! | Directive                  | Scope                                   |
14//! |----------------------------|-----------------------------------------|
15//! | `@mir-ignore [Kind …]`     | trailing comment → its line; otherwise the next code line |
16//! | `@mir-ignore-line [Kind …]`      | the comment's own line            |
17//! | `@mir-ignore-next-line [Kind …]` | the next physical line            |
18//! | `@mir-ignore-file [Kind …]`      | the whole file                    |
19//!
20//! `@mir-suppress*` is accepted as an alias of `@mir-ignore*`.
21//!
22//! Third-party aliases for drop-in compatibility:
23//!
24//! | Directive                   | Scope / kinds                          |
25//! |-----------------------------|----------------------------------------|
26//! | `@psalm-suppress Kind …`    | like `@mir-ignore` (named kinds)       |
27//! | `@suppress Kind …`          | like `@mir-ignore` (named kinds)       |
28//! | `@phpstan-ignore-line`      | the comment's own line, all kinds      |
29//! | `@phpstan-ignore-next-line` | the next line, all kinds               |
30//! | `@phpstan-ignore …`         | the next line, all kinds               |
31//!
32//! When no `Kind` follows the directive, *all* issues on the target line are
33//! suppressed. Kinds may be given by name (`UndefinedClass`) or by code
34//! (`MIR0123`); multiple kinds are space- or comma-separated. PHPStan's
35//! `@phpstan-ignore*` forms always suppress every kind on their target, since
36//! PHPStan identifiers do not map onto mir's [`IssueKind`] names.
37//!
38//! [`IssueKind`]: mir_issues::IssueKind
39
40use rustc_hash::{FxHashMap, FxHashSet};
41
42/// Set of issue kinds a directive applies to.
43#[derive(Debug, Clone)]
44enum KindSet {
45    /// Every kind on the target.
46    All,
47    /// Specific kinds, matched against `IssueKind::name()` or `code()`.
48    Named(FxHashSet<String>),
49}
50
51impl KindSet {
52    fn matches(&self, name: &str, code: &str) -> bool {
53        match self {
54            KindSet::All => true,
55            KindSet::Named(set) => set.contains(name) || set.contains(code),
56        }
57    }
58
59    fn merge(&mut self, other: KindSet) {
60        match (self, other) {
61            // Already broadest possible.
62            (KindSet::All, _) => {}
63            (slot @ KindSet::Named(_), KindSet::All) => *slot = KindSet::All,
64            (KindSet::Named(a), KindSet::Named(b)) => a.extend(b),
65        }
66    }
67}
68
69/// Where a directive applies, relative to the comment's own line.
70#[derive(Debug, Clone, Copy, PartialEq, Eq)]
71enum Scope {
72    /// The comment's own physical line.
73    SameLine,
74    /// The next code line (next non-blank physical line).
75    NextLine,
76    /// Every line in the file.
77    File,
78}
79
80struct Directive {
81    scope: Scope,
82    kinds: KindSet,
83    /// For [`Scope::NextLine`]: whether to skip intervening comment lines (not
84    /// just blanks) when locating the target. Set for "documents the following
85    /// element" forms (`@psalm-suppress`, bare `@mir-ignore`, …) so a directive
86    /// inside a multi-line docblock still lands on the declaration it annotates,
87    /// past the closing `*/`.
88    skip_comments: bool,
89}
90
91/// Per-file map of suppressed lines, built from source comments.
92#[derive(Debug, Default)]
93pub struct SuppressionMap {
94    /// 1-based line number → kinds suppressed on that line.
95    lines: FxHashMap<u32, KindSet>,
96    /// Whole-file suppression, if any directive requested it.
97    file: Option<KindSet>,
98    /// Named (non-All) suppressions with their target lines, for
99    /// `UnusedPsalmSuppress` detection. Each entry is `(target_line, kind_name)`.
100    /// Only `@psalm-suppress X` / `@suppress X` / `@mir-suppress X` forms populate
101    /// this — blanket `@phpstan-ignore*` suppressions are intentionally excluded.
102    pub named_suppressions: Vec<(u32, String)>,
103}
104
105impl SuppressionMap {
106    /// No directives — used to skip work for files with no suppression comments.
107    pub fn is_empty(&self) -> bool {
108        self.lines.is_empty() && self.file.is_none()
109    }
110
111    /// Whether an issue of `name`/`code` reported at 1-based `line` is suppressed.
112    pub fn is_suppressed(&self, line: u32, name: &str, code: &str) -> bool {
113        if let Some(file) = &self.file {
114            if file.matches(name, code) {
115                return true;
116            }
117        }
118        self.lines.get(&line).is_some_and(|k| k.matches(name, code))
119    }
120
121    /// Scan `source` for suppression directives.
122    pub fn from_source(source: &str) -> Self {
123        let raw_lines: Vec<&str> = source.lines().collect();
124        let mut map = SuppressionMap::default();
125
126        for (idx, raw) in raw_lines.iter().enumerate() {
127            let Some((directive, track_named)) = parse_directive_with_tracking(raw) else {
128                continue;
129            };
130            match directive.scope {
131                Scope::File => match &mut map.file {
132                    Some(existing) => existing.merge(directive.kinds),
133                    None => map.file = Some(directive.kinds),
134                },
135                Scope::SameLine => {
136                    let line_no = idx as u32 + 1;
137                    if track_named {
138                        if let KindSet::Named(ref names) = directive.kinds {
139                            for name in names {
140                                map.named_suppressions.push((line_no, name.clone()));
141                            }
142                        }
143                    }
144                    insert_line(&mut map.lines, line_no, directive.kinds);
145                }
146                Scope::NextLine => {
147                    let target = next_code_line(&raw_lines, idx, directive.skip_comments);
148                    if track_named {
149                        if let KindSet::Named(ref names) = directive.kinds {
150                            for name in names {
151                                map.named_suppressions.push((target, name.clone()));
152                            }
153                        }
154                    }
155                    insert_line(&mut map.lines, target, directive.kinds);
156                }
157            }
158        }
159
160        map
161    }
162
163    /// Returns unused named suppressions: those that did not match any issue
164    /// in `all_issues`. The returned vec contains `(target_line, kind_name)`.
165    ///
166    /// `pre_suppressed` is the subset of `all_issues` that arrived already
167    /// suppressed (via the `IssueBuffer` mechanism in collector/body analysis).
168    /// These may be emitted at a different line than the suppression target
169    /// (e.g. `InvalidDocblock` at a docblock-start line vs. the following
170    /// declaration line), so they are matched within a 30-line window before
171    /// the target.
172    pub fn unused_named(
173        &self,
174        all_issues: &[mir_issues::Issue],
175        pre_suppressed: &[&mir_issues::Issue],
176    ) -> Vec<(u32, String)> {
177        self.named_suppressions
178            .iter()
179            .filter(|(target_line, kind)| {
180                let kind_matches = |issue: &&mir_issues::Issue| {
181                    issue.kind.name() == kind.as_str() || issue.kind.code() == kind.as_str()
182                };
183                // Normal case: SuppressionMap-suppressed issue at the exact target line.
184                let at_target = all_issues
185                    .iter()
186                    .any(|issue| issue.location.line == *target_line && kind_matches(&issue));
187                if at_target {
188                    return false; // suppression IS used
189                }
190                // Docblock case: collector-emitted issues (like `InvalidDocblock`)
191                // land at the docblock-start line, which precedes the declaration
192                // that the suppression targets. Allow a 30-line look-back so a
193                // `@psalm-suppress InvalidDocblock` in a multi-line docblock is
194                // recognised as used even though its issue line != target_line.
195                let min_line = target_line.saturating_sub(30);
196                let covered_by_pre_suppressed = pre_suppressed.iter().any(|issue| {
197                    issue.location.line >= min_line
198                        && issue.location.line < *target_line
199                        && kind_matches(issue)
200                });
201                !covered_by_pre_suppressed
202            })
203            .cloned()
204            .collect()
205    }
206
207    /// Like `unused_named` but takes a slice of `Issue` references.
208    pub fn unused_named_ref(&self, issues: &[&mir_issues::Issue]) -> Vec<(u32, String)> {
209        self.named_suppressions
210            .iter()
211            .filter(|(line, kind)| {
212                !issues.iter().any(|issue| {
213                    issue.location.line == *line
214                        && (issue.kind.name() == kind || issue.kind.code() == kind)
215                })
216            })
217            .cloned()
218            .collect()
219    }
220}
221
222fn insert_line(lines: &mut FxHashMap<u32, KindSet>, line: u32, kinds: KindSet) {
223    match lines.get_mut(&line) {
224        Some(existing) => existing.merge(kinds),
225        None => {
226            lines.insert(line, kinds);
227        }
228    }
229}
230
231/// Locate a directive's target line strictly after `idx`, as a 1-based number.
232///
233/// Always skips blank lines. When `skip_comments` is set, also skips
234/// comment-only lines (`//`, `#`, `/* … */`, ` * …` docblock bodies and the
235/// closing `*/`) so a directive written inside a multi-line docblock lands on
236/// the declaration that follows it. Falls back to `idx + 2` when nothing
237/// qualifies, so the directive still has a deterministic target.
238fn next_code_line(raw_lines: &[&str], idx: usize, skip_comments: bool) -> u32 {
239    for (offset, line) in raw_lines.iter().enumerate().skip(idx + 1) {
240        let trimmed = line.trim();
241        if trimmed.is_empty() {
242            continue;
243        }
244        if skip_comments && is_comment_only(trimmed) {
245            continue;
246        }
247        return offset as u32 + 1;
248    }
249    idx as u32 + 2
250}
251
252/// Whether a trimmed line is purely a comment (no PHP code). `#[` is treated as
253/// a PHP 8 attribute (code), not a `#` comment.
254fn is_comment_only(trimmed: &str) -> bool {
255    trimmed.starts_with("//")
256        || trimmed.starts_with("/*")
257        || trimmed.starts_with('*')
258        || (trimmed.starts_with('#') && !trimmed.starts_with("#["))
259}
260
261/// Directive keyword table, ordered longest-first so that, e.g.,
262/// `@mir-ignore-next-line` is matched before the `@mir-ignore` prefix.
263///
264/// Each entry is `(keyword, scope, force_all)`. `force_all` makes the directive
265/// suppress every kind regardless of trailing tokens (PHPStan semantics).
266const KEYWORDS: &[(&str, Scope, bool)] = &[
267    ("@mir-ignore-next-line", Scope::NextLine, false),
268    ("@mir-suppress-next-line", Scope::NextLine, false),
269    ("@phpstan-ignore-next-line", Scope::NextLine, true),
270    ("@mir-ignore-line", Scope::SameLine, false),
271    ("@mir-suppress-line", Scope::SameLine, false),
272    ("@phpstan-ignore-line", Scope::SameLine, true),
273    ("@mir-ignore-file", Scope::File, false),
274    ("@mir-suppress-file", Scope::File, false),
275    // Bare forms (scope resolved below from comment position).
276    ("@mir-ignore", Scope::NextLine, false),
277    ("@mir-suppress", Scope::NextLine, false),
278    ("@psalm-suppress", Scope::NextLine, false),
279    ("@suppress", Scope::NextLine, false),
280    ("@phpstan-ignore", Scope::NextLine, true),
281];
282
283/// Bare directives (no `-line`/`-next-line`/`-file` suffix) resolve their scope
284/// from where the comment sits: a trailing comment annotates its own line, a
285/// standalone comment annotates the statement that follows it.
286const BARE_KEYWORDS: &[&str] = &[
287    "@mir-ignore",
288    "@mir-suppress",
289    "@psalm-suppress",
290    "@suppress",
291    "@phpstan-ignore",
292];
293
294/// Like `parse_directive` (which is parse_directive_with_tracking discarding the tracking flag),
295/// but also returns whether named suppression tracking
296/// should be applied (true for `@psalm-suppress`, `@mir-suppress`, `@suppress`
297/// and `@mir-ignore` forms; false for `@phpstan-*` which are blanket suppressors
298/// not tied to specific issue kinds).
299fn parse_directive_with_tracking(raw: &str) -> Option<(Directive, bool)> {
300    let comment = extract_comment(raw)?;
301
302    for &(keyword, scope, force_all) in KEYWORDS {
303        let Some(pos) = comment.content.find(keyword) else {
304            continue;
305        };
306        // Reject keyword matches that are really a prefix of a longer token
307        // (e.g. `@mir-ignore` inside `@mir-ignore-line`).
308        let after = &comment.content[pos + keyword.len()..];
309        if after
310            .chars()
311            .next()
312            .is_some_and(|c| c.is_ascii_alphanumeric() || c == '-')
313        {
314            continue;
315        }
316
317        let is_bare = BARE_KEYWORDS.contains(&keyword);
318
319        // Bare forms: a trailing comment suppresses its own line.
320        let scope = if is_bare && comment.has_code_before {
321            Scope::SameLine
322        } else {
323            scope
324        };
325
326        // The "documents the following element" forms (bare `@psalm-suppress`,
327        // `@mir-ignore`, …) skip past intervening comment lines — e.g. the
328        // closing `*/` of a multi-line docblock — to reach the declaration.
329        // PHPStan's explicit `*-next-line` and bare `@phpstan-ignore` keep their
330        // literal next-non-blank-line semantics.
331        let skip_comments = scope == Scope::NextLine && is_bare && !force_all;
332
333        let kinds = if force_all {
334            KindSet::All
335        } else {
336            parse_kinds(after)
337        };
338
339        // Track named suppressions only for non-phpstan forms (phpstan forms
340        // always suppress all kinds, so they can never be "unused for a specific kind").
341        let track_named = !keyword.starts_with("@phpstan");
342
343        return Some((
344            Directive {
345                scope,
346                kinds,
347                skip_comments,
348            },
349            track_named,
350        ));
351    }
352
353    None
354}
355
356struct Comment<'a> {
357    /// Text from the comment introducer onward (still includes `*/`, `*`, etc.).
358    content: &'a str,
359    /// Whether non-whitespace code precedes the comment on the same line.
360    has_code_before: bool,
361}
362
363/// Isolate the comment portion of a physical line, if any. Handles `//`, `#`
364/// and `/* … */` introducers, block-comment continuation lines (` * …`) and
365/// bare directive lines inside block comments (`@psalm-suppress …`).
366fn extract_comment(raw: &str) -> Option<Comment<'_>> {
367    let trimmed = raw.trim_start();
368
369    // Block-comment continuation or a bare directive line: no code precedes it.
370    if trimmed.starts_with('*') {
371        return Some(Comment {
372            content: trimmed.trim_start_matches('*'),
373            has_code_before: false,
374        });
375    }
376    if trimmed.starts_with('@') {
377        return Some(Comment {
378            content: trimmed,
379            has_code_before: false,
380        });
381    }
382
383    // Earliest single-line / block introducer on the line.
384    let pos = [raw.find("//"), raw.find('#'), raw.find("/*")]
385        .into_iter()
386        .flatten()
387        .min()?;
388    let has_code_before = !raw[..pos].trim().is_empty();
389    Some(Comment {
390        content: &raw[pos..],
391        has_code_before,
392    })
393}
394
395/// Collect issue kind names/codes following a directive keyword. Stops at the
396/// block-comment terminator and ignores non-identifier tokens. An empty result
397/// means "all kinds".
398fn parse_kinds(rest: &str) -> KindSet {
399    let mut set = FxHashSet::default();
400    for token in rest.split([' ', '\t', ',']) {
401        let token = token.trim();
402        if token.is_empty() {
403            continue;
404        }
405        // End of the comment / docblock — stop scanning.
406        if token.starts_with("*/") || token.starts_with('*') {
407            break;
408        }
409        // A kind name is alphanumeric (plus `_`); anything else (a PHPStan
410        // identifier like `argument.type`, prose, etc.) is skipped.
411        if token.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
412            set.insert(token.to_string());
413        }
414    }
415    if set.is_empty() {
416        KindSet::All
417    } else {
418        KindSet::Named(set)
419    }
420}
421
422#[cfg(test)]
423mod tests {
424    use super::*;
425
426    fn map(src: &str) -> SuppressionMap {
427        SuppressionMap::from_source(src)
428    }
429
430    #[test]
431    fn line_comment_above_statement_suppresses_next_line() {
432        // line 2 comment → suppress line 3
433        let m = map("<?php\n// @psalm-suppress UndefinedClass\nnew NoSuchClass();\n");
434        assert!(m.is_suppressed(3, "UndefinedClass", "MIR0000"));
435        assert!(!m.is_suppressed(2, "UndefinedClass", "MIR0000"));
436    }
437
438    #[test]
439    fn trailing_comment_suppresses_own_line() {
440        let m = map("<?php\nnew NoSuchClass(); // @mir-ignore UndefinedClass\n");
441        assert!(m.is_suppressed(2, "UndefinedClass", "MIR0000"));
442    }
443
444    #[test]
445    fn single_line_docblock_above_statement() {
446        let m = map("<?php\n/** @psalm-suppress UndefinedClass */\nnew NoSuchClass();\n");
447        assert!(m.is_suppressed(3, "UndefinedClass", "MIR0000"));
448    }
449
450    #[test]
451    fn phpstan_ignore_next_line_suppresses_all() {
452        let m = map("<?php\n// @phpstan-ignore-next-line\nnew NoSuchClass();\n");
453        assert!(m.is_suppressed(3, "UndefinedClass", "MIR0000"));
454        assert!(m.is_suppressed(3, "AnyOtherKind", "MIR9999"));
455    }
456
457    #[test]
458    fn ignore_line_targets_own_line() {
459        let m = map("<?php\nnew NoSuchClass(); // @mir-ignore-line\n");
460        assert!(m.is_suppressed(2, "UndefinedClass", "MIR0000"));
461    }
462
463    #[test]
464    fn next_line_skips_blank_lines() {
465        let m = map("<?php\n/** @psalm-suppress UndefinedClass */\n\n\nnew NoSuchClass();\n");
466        assert!(m.is_suppressed(5, "UndefinedClass", "MIR0000"));
467    }
468
469    #[test]
470    fn multiline_docblock_skips_to_declaration() {
471        // line 2: /**, line 3: * @psalm-suppress, line 4: */, line 5: declaration.
472        let src =
473            "<?php\n/**\n * @psalm-suppress UnusedMethod\n */\nprivate function a(): void {}\n";
474        let m = map(src);
475        assert!(m.is_suppressed(5, "UnusedMethod", "MIR0000"));
476    }
477
478    #[test]
479    fn phpstan_next_line_is_literal_not_comment_skipping() {
480        // PHPStan's -next-line targets the next non-blank line even if it's a
481        // comment; it does not hunt for the next code line.
482        let m = map("<?php\n// @phpstan-ignore-next-line\n// unrelated comment\nfoo();\n");
483        assert!(m.is_suppressed(3, "X", "MIR0000"));
484        assert!(!m.is_suppressed(4, "X", "MIR0000"));
485    }
486
487    #[test]
488    fn named_kind_does_not_suppress_other_kinds() {
489        let m = map("<?php\n// @mir-ignore UndefinedClass\nfoo();\n");
490        assert!(m.is_suppressed(3, "UndefinedClass", "MIR0000"));
491        assert!(!m.is_suppressed(3, "UndefinedFunction", "MIR0001"));
492    }
493
494    #[test]
495    fn match_by_code() {
496        let m = map("<?php\n// @mir-ignore MIR1400\nfoo();\n");
497        assert!(m.is_suppressed(3, "ParseError", "MIR1400"));
498    }
499
500    #[test]
501    fn file_scope_suppresses_every_line() {
502        let m = map("<?php // @mir-ignore-file UndefinedClass\nfoo();\nbar();\n");
503        assert!(m.is_suppressed(2, "UndefinedClass", "MIR0000"));
504        assert!(m.is_suppressed(99, "UndefinedClass", "MIR0000"));
505        assert!(!m.is_suppressed(2, "UndefinedFunction", "MIR0001"));
506    }
507
508    #[test]
509    fn multiple_kinds_one_directive() {
510        let m = map("<?php\n// @psalm-suppress UndefinedClass, NullMethodCall\nfoo();\n");
511        assert!(m.is_suppressed(3, "UndefinedClass", "MIR0000"));
512        assert!(m.is_suppressed(3, "NullMethodCall", "MIR0001"));
513    }
514
515    #[test]
516    fn no_directive_is_empty() {
517        let m = map("<?php\n$x = \"@psalm-suppress not a comment\";\nfoo();\n");
518        // It's inside a string but after `//`? No `//` here, so not detected.
519        assert!(m.is_empty());
520    }
521
522    #[test]
523    fn prefix_is_not_confused_with_longer_keyword() {
524        // `@mir-ignore-next-line` must be parsed as next-line, not bare same-line.
525        let m = map("<?php\nfoo(); // @mir-ignore-next-line\nbar();\n");
526        assert!(m.is_suppressed(3, "AnyKind", "MIR0000"));
527        assert!(!m.is_suppressed(2, "AnyKind", "MIR0000"));
528    }
529}