mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 20:42:10 +00:00
Avoid searching for bracketed comments in unparenthesized generators (#7627)
Similar to tuples, a generator _can_ be parenthesized or unparenthesized. Only search for bracketed comments if it contains its own parentheses. Closes https://github.com/astral-sh/ruff/issues/7623.
This commit is contained in:
parent
1a22eae98c
commit
865c89800e
5 changed files with 66 additions and 7 deletions
|
@ -102,8 +102,9 @@ impl NeedsParentheses for ExprGeneratorExp {
|
|||
}
|
||||
}
|
||||
|
||||
fn is_generator_parenthesized(generator: &ExprGeneratorExp, source: &str) -> bool {
|
||||
// / Count the number of open parentheses between the start of the tuple and the first element.
|
||||
/// Return `true` if a generator is parenthesized in the source code.
|
||||
pub(crate) fn is_generator_parenthesized(generator: &ExprGeneratorExp, source: &str) -> bool {
|
||||
// Count the number of open parentheses between the start of the generator and the first element.
|
||||
let open_parentheses_count = SimpleTokenizer::new(
|
||||
source,
|
||||
TextRange::new(generator.start(), generator.elt.start()),
|
||||
|
@ -115,7 +116,7 @@ fn is_generator_parenthesized(generator: &ExprGeneratorExp, source: &str) -> boo
|
|||
return false;
|
||||
}
|
||||
|
||||
// Count the number of parentheses between the end of the first element and its trailing comma.
|
||||
// Count the number of parentheses between the end of the generator and its trailing comma.
|
||||
let close_parentheses_count = SimpleTokenizer::new(
|
||||
source,
|
||||
TextRange::new(
|
||||
|
@ -130,7 +131,7 @@ fn is_generator_parenthesized(generator: &ExprGeneratorExp, source: &str) -> boo
|
|||
.filter(|token| token.kind() == SimpleTokenKind::RParen)
|
||||
.count();
|
||||
|
||||
// If the number of open parentheses is greater than the number of close parentheses, the generator
|
||||
// is parenthesized.
|
||||
// If the number of open parentheses is greater than the number of close parentheses, the
|
||||
// generator is parenthesized.
|
||||
open_parentheses_count > close_parentheses_count
|
||||
}
|
||||
|
|
|
@ -221,7 +221,7 @@ impl NeedsParentheses for ExprTuple {
|
|||
}
|
||||
}
|
||||
|
||||
/// Check if a tuple has already had parentheses in the input
|
||||
/// Return `true` if a tuple is parenthesized in the source code.
|
||||
pub(crate) fn is_tuple_parenthesized(tuple: &ExprTuple, source: &str) -> bool {
|
||||
let Some(elt) = tuple.elts.first() else {
|
||||
return false;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue