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

Code before:
        }

        let Some(code) = diagnostic.secondary_code() else {
            return false;
        };
        let Some(span) = diagnostic.primary_span() else {
            return false;
        };
        let Some(range) = span.range() else {
            return false;
        };

        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

Review comment (suggestion):
Is my understanding correct that the suppressions in `self.valid` are sorted by their `TextRange`? If so, you could then avoid using a hash map and instead use a single `Option<(TextRange, SuppressionDiagnostic)>` that you "flush" when the `TextRange` no longer matches `suppression.comments.disable_comment().range` (and when you're done processing all `self.valid` 

Code after:
        }

        let Some(code) = diagnostic.secondary_code() else {
            return false;
        };
        let Some(span) = diagnostic.primary_span() else {
            return false;
        };
        let Some(range) = span.range() else {
            return false;
        };

        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());