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

Code before:
        locator: &Locator<'_>,
        comment: &SuppressionComment,
        remove_codes: &[&str],
        highlight_only_code: bool,
    ) -> (TextRange, Edit) {
        let mut range = comment.range;
        let edit = if comment.codes.len() == 1 {
            if highlight_only_code {
                range = comment.codes[0];
            }
            delete_comment(comment.range, locator)
        } else {
            let first = comment
                .codes
                .first()
                .expect("suppression comment without codes");
            let last = comment
                .codes
                .last()
                .expect("suppression comment without codes");
            let code_range = TextRange::new(first.start(), last.end());
            let remaining = comment
                .codes_as_str(locator.contents())
                .filter(|code| !remove_codes.contains(code))
                .dedup()
                .collect_vec();

            if remaining.is_empty() {
                if highlight_only_code {
                    range = code_range;
                }
                delete_comment(comment.range, locator)
            } else {
                range = code_range;
                let replacement = remaining.join(", ");
                Edit::range_replacement(replacement, code_range)
            }
        };
        (range, edit)
    }
}

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

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

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

Review comment (suggestion):
This might be getting too clever, but I think we could go ahead and do the `join` here. The empty string will also work for the `is_empty` check, and then we can avoid allocating a separate `Vec` first.


```suggestion
                .join(", ");
```

Code after:
        remove_codes: &[&str],
        highlight_only_code: bool,
    ) -> (TextRange, Edit) {
        let mut range = comment.range;
        let edit = if comment.codes.len() == 1 {
            if highlight_only_code {
                range = comment.codes[0];
            }
            delete_comment(comment.range, locator)
        } else if remove_codes.len() == 1 {
            let code_index = comment
                .codes
                .iter()
                .position(|range| locator.slice(range) == remove_codes[0])
                .unwrap();
            if highlight_only_code {
                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)
        } else {
            let first = comment
                .codes
                .first()
                .expect("suppression comment without codes");
            let last = comment
                .codes
                .last()
                .expect("suppression comment without codes");
            let code_range = TextRange::new(first.start(), last.end());
            let remaining = comment
                .codes_as_str(locator.contents())
                .filter(|code| !remove_codes.contains(code))
                .dedup()
                .join(", ");

            if remaining.is_empty() {
                delete_comment(comment.range, locator)
            } else {
                Edit::range_replacement(remaining, code_range)
            }