-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[flake8-comprehensions
]: Handle trailing comma in C403
fix
#16110
Conversation
|
// Place `}` at argument's end or at trailing comma if present | ||
let mut tokenizer = SimpleTokenizer::starts_at( | ||
argument.end(), | ||
checker.locator().slice(TextRange::up_to(call.end())), | ||
); | ||
let right_brace_loc = tokenizer | ||
.find(|token| token.kind == SimpleTokenKind::Comma) | ||
.map_or(call.arguments.end() - one, |comma| { | ||
comma.end() - TextSize::from(1) | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should use checker.tokens().in_range(...)
here as we already have access to the parsed tokens. I think the range would be TextRange::new(argument.end(), call.end())
. You can refer to other references of in_range
method to check how it's being used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call @dhruvmanila ! Do you (@ayushbaweja) want to also make the same changes to unnecessary_generator_list.rs
and unnecessary_generator_set.rs
? Here's the patch for the first of those, for example:
diff --git a/crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_generator_list.rs b/crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_generator_list.rs
index cc389e1f4..2e3b7fbc9 100644
--- a/crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_generator_list.rs
+++ b/crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_generator_list.rs
@@ -4,7 +4,7 @@ use ruff_python_ast as ast;
use ruff_python_ast::comparable::ComparableExpr;
use ruff_python_ast::parenthesize::parenthesized_range;
use ruff_python_ast::ExprGenerator;
-use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
+use ruff_python_parser::TokenKind;
use ruff_text_size::{Ranged, TextRange, TextSize};
use crate::checkers::ast::Checker;
@@ -125,10 +125,12 @@ pub(crate) fn unnecessary_generator_list(checker: &Checker, call: &ast::ExprCall
// Replace `)` with `]`.
// 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)
+ let after_arg_tokens = checker
+ .tokens()
+ .in_range(TextRange::new(argument.end(), call.end()));
+ let right_bracket_loc = after_arg_tokens
+ .iter()
+ .find(|token| token.kind() == TokenKind::Comma)
.map_or(call.arguments.end(), |comma| comma.end())
- TextSize::from(1);
let call_end = Edit::replacement("]".to_string(), right_bracket_loc, call.end());
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure! Will make the change and also update the unnecessary_generator files
fcd1884
to
4dfd52d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks so much, this is great!
flake8-comprehensions
]: Handle trailing comma in C403
fix
## Summary This is mainly on me for not noticing this during the last release but I noticed in the last changelog that there's only 1 bug fix which didn't seem correct as I saw multiple of them so I looked at a couple of PRs that are in "Rule changes" section and the PRs that were marked with the `bug` label was categorized there because 1. It _also_ had other labels like `rule` and `fixes` (#16080, #16110, #16219, etc.) 2. Some PRs didn't have the `bug` label (but the issue as marked as `bug`) but _only_ labels like "fixes" (#16011, #16132, etc.)
Summary
Resolves #16099 based on #15929
Test Plan
Added test case
s = set([x for x in range(3)],)
and updated snapshot.