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

Code before:
            return false;
        };

        for suppression in &mut self.valid {
            if *code == suppression.code.as_str() && suppression.range.contains_range(range) {
                suppression.used = true;
                return true;
            }
        }
        false
    }

    pub(crate) fn generate_diagnostics(&self, context: &LintContext, locator: &Locator) {
        if !context.is_rule_enabled(Rule::UnusedSuppression) {
            return;
        }

        let unused = self.valid.iter().filter(|suppression| !suppression.used);

        for suppression in unused {
            let Ok(rule) = Rule::from_code(&suppression.code) else {
                continue; // TODO: invalid code
            };
            if !context.is_rule_enabled(rule) {
                continue; // don't count disabled rules as unused
            }
            for comment in &suppression.comments {
                let mut range = comment.range;
                let edit = if comment.codes.len() == 1 {
                    delete_comment(comment.range, locator)
                } else {
                    let code_index = comment
                        .codes
                        .iter()
                        .position(|range| locator.slice(range) == suppression.code)
                        .unwrap();
                    range = comment.codes[code_index];
                    let code_range = if code_index < (comment.codes.len() - 1) {
                        TextRange::new(
                            comment.codes[code_index].start(),
                            comment.codes[code_index + 1].start(),
                        )
                    } else {
                        TextRange::new(
                            comment.codes[code_index - 1].end(),
                            comment.codes[code_index].end(),
                        )
                    };
                    Edit::range_deletion(code_range)
                };


Review comment (suggestion):
I think this is a divergence from unused `noqa` suppressions, and I think it's very desirable to flag unused suppressions after you disabled a rule. That's why I think we shouldn't skip here


Code after:
                suppression.used.set(true);
                return true;
            }
        }
        false
    }

    pub(crate) fn check_suppressions(&self, context: &LintContext, locator: &Locator) {
        if !context.any_rule_enabled(&[Rule::UnusedNOQA, Rule::InvalidRuleCode]) {
            return;
        }

        let unused = self
            .valid
            .iter()
            .filter(|suppression| !suppression.used.get());

        for suppression in unused {
            let Ok(rule) = Rule::from_code(&suppression.code) else {
                continue; // TODO: invalid code
            };
            for comment in &suppression.comments {
                let mut range = comment.range;
                let edit = if comment.codes.len() == 1 {
                    delete_comment(comment.range, locator)
                } else {
                    let code_index = comment
                        .codes
                        .iter()
                        .position(|range| locator.slice(range) == suppression.code)
                        .unwrap();
                    range = comment.codes[code_index];
                    let code_range = if code_index < (comment.codes.len() - 1) {
                        TextRange::new(
                            comment.codes[code_index].start(),
                            comment.codes[code_index + 1].start(),
                        )
                    } else {
                        TextRange::new(
                            comment.codes[code_index - 1].end(),
                            comment.codes[code_index].end(),
                        )
                    };
                    Edit::range_deletion(code_range)
                };

                let codes = if context.is_rule_enabled(rule) {
                    UnusedCodes {
                        unmatched: vec![suppression.code.to_string()],
                        ..Default::default()
                    }