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

Code before:
                        let Some(indent) =
                            indentation_at_offset(suppression.range.start(), self.source)
                        else {
                            // trailing suppressions are not supported
                            self.invalid.push(InvalidSuppression {
                                kind: InvalidSuppressionKind::Trailing,
                                comment: suppression,
                            });
                            continue;
                        };

                        // comment matches current block's indentation, or precedes an indent/dedent token
                        if indent == current_indent
                            || tokens[token_index..]
                                .iter()
                                .find(|t| !t.kind().is_trivia())
                                .is_some_and(|t| {
                                    t.kind() == TokenKind::Dedent || t.kind() == TokenKind::Indent
                                })
                        {
                            self.pending.push(PendingSuppressionComment {
                                indent,
                                comment: suppression,
                            });
                        } else {
                            // weirdly indented? ¯\_(ツ)_/¯
                            self.invalid.push(InvalidSuppression {
                                kind: InvalidSuppressionKind::Indentation,
                                comment: suppression,
                            });
                        }
                    }
                    _ => {}
                }
            }

            self.match_comments("", TextRange::up_to(self.source.text_len()));
        }

        Suppressions {
            valid: self.valid,
            invalid: self.invalid,
            errors,
        }
    }

    fn match_comments(&mut self, current_indent: &str, dedent_range: TextRange) {
        let mut comment_index = 0;

        // for each pending comment, search for matching comments at the same indentation level,
        // generate range suppressions for any matches, and then discard any unmatched comments

Review comment (suggestion):
Nit: You could use let chains here

Change the tokens loop to:

```rust
let mut tokens = tokens.after(suppression.range.start()).iter();
while let Some(token) = tokens.next() {
	...
```

```suggestion
                        if indent == current_indent
                            && let Some(non_trivia) = tokens.clone().find(|t| !t.kind().is_trivia())
                            && matches!(non_trivia.kind(), TokenKind::Dedent | TokenKind::Indent)
```

Cloning `tokens` here is fine because cloning a slice iterator is cheap (it's only two pointers, one pointing to the start and one to the end of the slice)

Code after:
                        let Some(suppression) =
                            suppressions.next_if(|suppression| suppression.range == token.range())
                        else {
                            continue;
                        };

                        let Some(indent) =
                            indentation_at_offset(suppression.range.start(), self.source)
                        else {
                            // trailing suppressions are not supported
                            self.invalid.push(InvalidSuppression {
                                kind: InvalidSuppressionKind::Trailing,
                                comment: suppression,
                            });
                            continue;
                        };

                        // comment matches current block's indentation, or precedes an indent/dedent token
                        if indent == current_indent
                            || after[token_index..]
                                .iter()
                                .find(|t| !t.kind().is_trivia())
                                .is_some_and(|t| {
                                    matches!(t.kind(), TokenKind::Dedent | TokenKind::Indent)
                                })
                        {
                            self.pending.push(PendingSuppressionComment {
                                indent,
                                comment: suppression,
                            });
                        } else {
                            // weirdly indented? ¯\_(ツ)_/¯
                            self.invalid.push(InvalidSuppression {
                                kind: InvalidSuppressionKind::Indentation,
                                comment: suppression,
                            });
                        }
                    }
                    _ => {}
                }
            }
        }

        self.match_comments("", TextRange::up_to(self.source.text_len()));

        Suppressions {
            valid: self.valid,
            invalid: self.invalid,
            errors,
        }
    }