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

Code before:
                            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 {
            source,
            ..Default::default()
        }
    }

    pub(crate) fn load_from_tokens(mut self, tokens: &Tokens) -> Suppressions {
        let default_indent = "";
        let mut indents: Vec<&str> = vec![];


Review comment (suggestion):
Nit: I would probably use some early returns here to reduce the nesting
```suggestion
        if !context.is_rule_enabled(Rule::UnusedSuppression) {
            return;
        }

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

        for suppression in unused_suppressions {
            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));
            }
        }
    }
```

Code after:
        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()],
                        ..Default::default()
                    }
                };

                let mut diagnostic = context.report_diagnostic(
                    UnusedNOQA {
                        codes: Some(codes),
                        kind: UnusedNOQAKind::Suppression,
                    },