mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-24 05:25:17 +00:00
Fix find_only_token_in_range with expression parentheses (#5645)
## Summary Fix an oversight in `find_only_token_in_range` where the following code would panic due do the closing and opening parentheses being in the range we scan: ```python d1 = [ ("a") if # 1 ("b") else # 2 ("c") ] ``` Closing and opening parentheses respectively are now correctly skipped. ## Test Plan I added a regression test
This commit is contained in:
parent
82317ba1fd
commit
cab3a507bc
3 changed files with 32 additions and 2 deletions
|
@ -1215,11 +1215,15 @@ fn handle_expr_if_comment<'a>(
|
|||
CommentPlacement::Default(comment)
|
||||
}
|
||||
|
||||
/// Looks for a token in the range that contains no other tokens.
|
||||
/// Looks for a token in the range that contains no other tokens except for parentheses outside
|
||||
/// the expression ranges
|
||||
fn find_only_token_in_range(range: TextRange, locator: &Locator, token_kind: TokenKind) -> Token {
|
||||
let mut tokens = SimpleTokenizer::new(locator.contents(), range).skip_trivia();
|
||||
let mut tokens = SimpleTokenizer::new(locator.contents(), range)
|
||||
.skip_trivia()
|
||||
.skip_while(|token| token.kind == TokenKind::RParen);
|
||||
let token = tokens.next().expect("Expected a token");
|
||||
debug_assert_eq!(token.kind(), token_kind);
|
||||
let mut tokens = tokens.skip_while(|token| token.kind == TokenKind::LParen);
|
||||
debug_assert_eq!(tokens.next(), None);
|
||||
token
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue