[flake8-comprehensions] Handle trailing comma in fixes for unnecessary-generator-list/set (C400,C401)#15929
Merged
dylwil3 merged 4 commits intoastral-sh:mainfrom Feb 5, 2025
Merged
Conversation
Contributor
|
MichaReiser
approved these changes
Feb 5, 2025
Member
MichaReiser
left a comment
There was a problem hiding this comment.
Nice, I've only two nit comments that apply to both rules
crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_generator_list.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_generator_list.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_generator_set.rs
Outdated
Show resolved
Hide resolved
flake8-comprehensions] Handle trailing comma in fixes for unnecessary-generator-list/comma (C400,C401)flake8-comprehensions] Handle trailing comma in fixes for unnecessary-generator-list/set (C400,C401)
dhruvmanila
reviewed
Feb 12, 2025
Comment on lines
+127
to
+134
| // Place `]` at argument's end or at trailing comma if present | ||
| let mut tokenizer = | ||
| SimpleTokenizer::new(checker.source(), TextRange::new(argument.end(), call.end())); | ||
| let right_bracket_loc = tokenizer | ||
| .find(|token| token.kind == SimpleTokenKind::Comma) | ||
| .map_or(call.arguments.end(), |comma| comma.end()) | ||
| - TextSize::from(1); | ||
| let call_end = Edit::replacement("]".to_string(), right_bracket_loc, call.end()); |
Member
There was a problem hiding this comment.
I was wondering if we can use checker.tokens().in_range(...) instead here?
dhruvmanila
reviewed
Feb 12, 2025
Comment on lines
+130
to
+136
| // Place `}` at argument's end or at trailing comma if present | ||
| let mut tokenizer = | ||
| SimpleTokenizer::new(checker.source(), TextRange::new(argument.end(), call.end())); | ||
| let right_brace_loc = tokenizer | ||
| .find(|token| token.kind == SimpleTokenKind::Comma) | ||
| .map_or(call.arguments.end(), |comma| comma.end()) | ||
| - TextSize::from(1); |
dylwil3
added a commit
that referenced
this pull request
Feb 15, 2025
## Summary Resolves [#16099 ](#16099) based on [#15929 ](#15929) ## Test Plan Added test case `s = set([x for x in range(3)],)` and updated snapshot. --------- Co-authored-by: dylwil3 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The unsafe fixes for the rules unnecessary-generator-list (C400) and unnecessary-generator-set (C401) used to introduce syntax errors if the argument to
listorsethad a trailing comma, because the fix would retain the comma after transforming the function call to a comprehension.This PR accounts for the trailing comma when replacing the end of the call with a
]or}.Closes #15852