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

Code before:
        false
    }

    pub(crate) fn generate_diagnostics(&self, context: &LintContext, locator: &Locator) {
        if context.is_rule_enabled(Rule::UnusedSuppression) {
            for suppression in &self.valid {
                if !suppression.used {
                    for comment in &suppression.comments {
                        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();
                            let code_range = if code_index < (comment.codes.len() - 1) {
                                TextRange::new(
                                    comment.codes[code_index].start(),
                                    comment.codes[code_index + 1].start(),
                                )
                            } else {
                                comment.codes[code_index]
                            };
                            Edit::range_deletion(code_range)
                        };

                        let mut diagnostic = context
                            .report_diagnostic(UnusedSuppression, suppression.comments[0].range);
                        diagnostic.set_fix(Fix::safe_edit(edit));
                    }
                }
            }
        }
    }
}

#[derive(Default)]
pub(crate) struct SuppressionsBuilder<'a> {
    source: &'a str,

    valid: Vec<Suppression>,
    invalid: Vec<InvalidSuppression>,
    errors: Vec<ParseError>,

    pending: Vec<PendingSuppressionComment<'a>>,
}

impl<'a> SuppressionsBuilder<'a> {
    pub(crate) fn new(source: &'a str) -> Self {
        Self {

Review comment (suggestion):
I think the logic here only works now because there are always at most 2 comments. This loop would, otherwise, delete the `+1` comment twice. 

I'd be leaning towards splitting `comments` into two fields: `disable_comment` (could also be an `ignore` in the future), `enable_comment: Option<...>`. This way, making the assumption here that we always have at most two comments is safe (and it also removes the need to find the comment again.)

Code after:
        }
        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()
                    }
                } else {
                    UnusedCodes {
                        disabled: vec![suppression.code.to_string()],