-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix suggestion for collapsible_if
and collapsible_else_if
when the inner if
is enclosed in parentheses
#15304
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
base: master
Are you sure you want to change the base?
Conversation
collapsible_if
and collapsible_else_if
when the inner if
is enclosed in parentheses
585b21b
to
93591d5
Compare
93591d5
to
ad64c81
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.
I just have a small suggestion to improve the code, otherwise this looks mergeworthy.
clippy_lints/src/collapsible_if.rs
Outdated
let start = span.shrink_to_lo(); | ||
let end = span.shrink_to_hi(); | ||
|
||
loop { |
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 could simplify this loop: first we create a function that returns Option<Span>
, trimming the outermost parens, then this can be a while let
loop, updating the span.
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.
The tricky thing there is that we need to update 3 variables, so it gets kind of verbose. Anyway, I've now solved it using recursion, lmk what you think.
ad64c81
to
052a4b0
Compare
Does blocks also leads to the same FN? If so, it would be great to also include fix for that too |
These lints don't fire when the inner if is wrapped in a block, e.g. these fail because the lint is not found fn in_brackets() {
if true {
{if true {
println!("In parens, linted");
}}
}
//~^^^^^ collapsible_if
}
fn in_brackets() {
let x = "hello";
let y = "world";
if x == "hello" {
print!("Hello ");
} else {
{if y == "world" { println!("world") } else { println!("!") }}
}
//~^^^ collapsible_else_if
} |
changelog: [
collapsible_else_if
]: fix suggestion when innerif
as wrapped in parentheseschangelog: [
collapsible_if
]: fix suggestion when innerif
as wrapped in parenthesesfixes #15303
I'm sure this is a bit dirty, but don't currently see a better way.