Repository: astral-sh/ruff
File: crates/ruff_linter/src/suppression.rs (Rust)

Code before:

        for suppression in &self.valid {
            let suppression_code =
                get_redirect_target(suppression.code.as_str()).unwrap_or(suppression.code.as_str());
            if *code == suppression_code && suppression.range.contains_range(range) {
                suppression.used.set(true);
                return true;
            }
        }
        false
    }

    pub(crate) fn check_suppressions(&self, context: &LintContext, locator: &Locator) {
        let mut grouped_diagnostics: FxHashMap<TextRange, SuppressionDiagnostic> =
            FxHashMap::default();
        let mut unmatched_ranges = FxHashSet::default();
        for suppression in &self.valid {
            let key = suppression.comments.disable_comment().range;
            let code_str = suppression.code.as_str();

            if !code_is_valid(&suppression.code, &context.settings().external) {
                // InvalidRuleCode
                let entry = grouped_diagnostics
                    .entry(key)
                    .or_insert_with(|| SuppressionDiagnostic::new(suppression));
                entry.invalid_codes.push(suppression.code.as_str());
            } else if !suppression.used.get() {
                // UnusedNOQA
                let Ok(rule) = Rule::from_code(
                    get_redirect_target(&suppression.code).unwrap_or(&suppression.code),
                ) else {
                    continue; // "external" lint code, don't treat it as unused
                };

                let entry = grouped_diagnostics
                    .entry(key)
                    .or_insert_with(|| SuppressionDiagnostic::new(suppression));

                if context.is_rule_enabled(rule) {
                    if suppression
                        .comments
                        .disable_comment()
                        .codes_as_str(locator.contents())
                        .filter(|code| *code == code_str)
                        .collect_vec()
                        .len()
                        > 1
                    {
                        entry.duplicated_codes.push(code_str);
                    } else {
                        entry.unused_codes.push(code_str);

Review comment (suggestion):
small nit: I think this is the same as the variable above:


```suggestion
                entry.invalid_codes.push(code_str);
```

Code after:

        for suppression in &self.valid {
            let suppression_code =
                get_redirect_target(suppression.code.as_str()).unwrap_or(suppression.code.as_str());
            if *code == suppression_code && suppression.range.contains_range(range) {
                suppression.used.set(true);
                return true;
            }
        }
        false
    }

    pub(crate) fn check_suppressions(&self, context: &LintContext, locator: &Locator) {
        let mut grouped_diagnostic: Option<(TextRange, SuppressionDiagnostic)> = None;
        let mut unmatched_ranges = FxHashSet::default();
        for suppression in &self.valid {
            let key = suppression.comments.disable_comment().range;

            // Process any pending grouped diagnostics
            if let Some((group_key, ref group)) = grouped_diagnostic
                && key != group_key
            {
                if group.any_invalid() {
                    Suppressions::report_suppression_codes(
                        context,
                        locator,
                        group.suppression,
                        &group.invalid_codes,
                        true,
                        InvalidRuleCode {
                            rule_code: group.invalid_codes.iter().join(", "),
                            kind: InvalidRuleCodeKind::Suppression,
                            whole_comment: group.suppression.codes().len()
                                == group.invalid_codes.len(),
                        },
                    );
                }
                if group.any_unused() {
                    let mut codes = group.disabled_codes.clone();
                    codes.extend(group.unused_codes.clone());
                    Suppressions::report_suppression_codes(
                        context,
                        locator,
                        group.suppression,
                        &codes,
                        false,
                        UnusedNOQA {
                            codes: Some(UnusedCodes {
                                disabled: group
                                    .disabled_codes
                                    .iter()