-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[ruff
] Implement unnecessary-nested-literal
(RUF039
)
#14323
base: main
Are you sure you want to change the base?
Conversation
15e08d3
to
ae4a677
Compare
ae4a677
to
ebbd9f1
Compare
|
code | total | + violation | - violation | + fix | - fix |
---|---|---|---|---|---|
RUF039 | 2 | 2 | 0 | 0 | 0 |
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.
@AlexWaygood would you mind doing a quick glance at the rule definition? I already reviewed the code
crates/ruff_linter/src/rules/ruff/rules/unnecessary_nested_literal.rs
Outdated
Show resolved
Hide resolved
if !is_nested { | ||
return; | ||
} |
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.
Can you expand on why we have to check inside the rule whether it is a nested literal, considering that the rule is only called when semantic.in_nested_literal
is true
?
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.
We only consider the top-level union, so when semantic.in_nested_literal
is false
. I've considered to run this run only when semantic.in_nested_literal
is true
, but then the top-level union is unavailable for the autofix.
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.
Oh, we actually only run the rule when not in a nested literal...
crates/ruff_linter/src/rules/ruff/rules/unnecessary_nested_literal.rs
Outdated
Show resolved
Hide resolved
…eral.rs Co-authored-by: Micha Reiser <[email protected]>
|
||
/// RUF039 | ||
pub(crate) fn unnecessary_nested_literal<'a>(checker: &mut Checker, literal_expr: &'a Expr) { | ||
let mut nodes: Vec<&Expr> = Vec::new(); |
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.
It might also be worth to use a SmallVec
here with a size of 1 to avoid allocating if this is a not-nested literal.
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.
nodes
will be populated with all entries in Literal
, so even for not-nested literals it can exceed 1. Shall I do a first pass to check if the Literal
is nested, followed by another to collect the nodes? This will reduce the allocation on the common path.
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 checking first and then collecting could be good for performance
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.
Similar to #14319 (review), I feel like I'm not sure how much this antipattern really comes up in practice. But, I can see the value if this is a pattern that could be introduced by the fix for other rules we implement!
crates/ruff_linter/src/rules/ruff/rules/unnecessary_nested_literal.rs
Outdated
Show resolved
Hide resolved
…eral.rs Co-authored-by: Alex Waygood <[email protected]>
Is there any open feedback that needs addressing? I'm otherwise happy to merge this rule. |
My feedback has been addressed, but it looks like there's quite a few merge conflicts here. The conversations in #14323 (comment) and #14323 (comment) are also not marked as "resolved", and I think you're better placed to judge whether they should be or not ;) |
Summary
Implementing
unnecessary-nested-literal
.This rule could help simplify other rules' fixes by handling the flattening of
Literal
s here.See also https://github.com/astral-sh/ruff/pull/14270/files#r1837810594 (unions in a follow-up PR)
Test Plan
cargo test
The ecosystem results are correct.
Some of the nesting emits multiple violations.
I've got a fix for this, but that depends on #14280.
We can go on with merging this PR after review regardless (the violations are not wrong).