Open poopsicles opened 1 year ago
Another example would be
if 1 > 0 {
println!("hi");
}
This example just deals with static ints, and not any variables since that might require the compiler probably?
Perhaps simply lifting the inner if-else is more achieveable?
Going from:
let a = 13;
if if a == 13 { 10 } else { 0 } > 5 { // <- nested if-else
println!("This will be printed");
}
To:
let a = 13;
let thing = if a == 13 { 10 } else { 0 };
if thing > 5 {
println!("This will be printed");
}
What it does
If an
if else
expression only contains static types, such asi8
s andu32
s, not variables, then it's likely that one branch can be totally removed without the "correctness" of the code changing.A Reddit thread (Archive.org link) demonstrated this by putting an
if else
inside anotherif
, which was evaluated correctly.I'm not exactly sure whether this lint should capture:
if
statements that can be determined ahead (comparing two integers)if else
statements (which most definitely yield a value) inside anotherif
statement (like the Reddit example)Advantage
Drawbacks
Someone might intend to later "fill out" the other branches, which is why this is limiting it to
if-else
expressions in anotherif
statement, as opposed to freestandingif
statements might make more sense, the former is usually an unintended, while the latter could be intended to be added to.Example
Could be written as this (if we're just looking for nested ones):
Or