-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Lint nested and independent if let
clauses
#5218
Comments
While this is true, this could increase the cognitive complexity, since the if-condition is longer. So we have to be careful here.
This has to be ignored, except for the special in #2521, where the
I don't think we should lint this, since detecting side-effects is really hard. In the
As stated above, I'm not sure about that, we may need some other opinions about this.
This seems hard for more complex enums, than
We can track this in this issue. |
let variable_01 = Some(1_usize);
let variable_02 = Some(2_usize);
let variable_03 = Some(3_usize);
let variable_04 = Some(4_usize);
let variable_05 = Some(5_usize);
let variable_06 = Some(6_usize);
let variable_07 = Some(7_usize);
let variable_08 = Some(8_usize);
if let Some(variable_01) = variable_01 {
if let Some(variable_02) = variable_02 {
if let Some(variable_03) = variable_03 {
if let Some(variable_04) = variable_04 {
if let Some(variable_05) = variable_05 {
if let Some(variable_06) = variable_06 {
if let Some(variable_07) = variable_07 {
if let Some(variable_08) = variable_08 {
println!("{}", variable_01);
println!("{}", variable_02);
println!("{}", variable_03);
println!("{}", variable_04);
println!("{}", variable_05);
println!("{}", variable_06);
println!("{}", variable_07);
println!("{}", variable_08);
}
}
}
}
}
}
}
}
if let (
Some(variable_01),
Some(variable_02),
Some(variable_03),
Some(variable_04),
Some(variable_05),
Some(variable_06),
Some(variable_07),
Some(variable_08),
) = (
variable_01,
variable_02,
variable_03,
variable_04,
variable_05,
variable_06,
variable_07,
variable_08,
) {
println!("{}", variable_01);
println!("{}", variable_02);
println!("{}", variable_03);
println!("{}", variable_04);
println!("{}", variable_05);
println!("{}", variable_06);
println!("{}", variable_07);
println!("{}", variable_08);
} or with a more complex enum enum Example {
Variant01 {
string: String,
number: usize,
character: char,
},
Variant02 {
string: String,
number: usize,
character: char,
},
Variant03 {
string: String,
number: usize,
character: char,
},
Variant04 {
string: String,
number: usize,
character: char,
},
Variant05 {
string: String,
number: usize,
character: char,
},
Variant06 {
string: String,
number: usize,
character: char,
},
Variant07 {
string: String,
number: usize,
character: char,
},
Variant08 {
string: String,
number: usize,
character: char,
},
}
let value_01 = Example::Variant01 {
"example".into(),
2_usize,
'b',
};
let value_02 = Example::Variant02 {
"example".into(),
2_usize,
'b',
};
let value_03 = Example::Variant03 {
"example".into(),
2_usize,
'b',
};
let value_04 = Example::Variant04 {
"example".into(),
2_usize,
'b',
};
let value_05 = Example::Variant05 {
"example".into(),
2_usize,
'b',
};
let value_06 = Example::Variant06 {
"example".into(),
2_usize,
'b',
};
let value_07 = Example::Variant07 {
"example".into(),
2_usize,
'b',
};
let value_08 = Example::Variant08 {
"example".into(),
2_usize,
'b',
};
if let Example::Variant01 { string_01, number_01, character_01 } = value_01 {
println!("{}: {},{}", string_01, number_01, character_01);
if let Example::Variant02 { string_02, number_02, character_02 } = value_02 {
println!("{}: {},{}", string_02, number_02, character_02);
if let Example::Variant03 { string_03, number_03, character_03 } = value_03 {
println!("{}: {},{}", string_03, number_03, character_03);
if let Example::Variant04 { string_04, number_04, character_04 } = value_04 {
println!("{}: {},{}", string_04, number_04, character_04);
if let Example::Variant05 { string_05, number_05, character_05 } = value_05 {
println!("{}: {},{}", string_05, number_05, character_05);
if let Example::Variant06 { string_06, number_06, character_06 } = value_06 {
println!("{}: {},{}", string_06, number_06, character_06);
if let Example::Variant07 { string_07, number_07, character_07 } = value_07 {
println!("{}: {},{}", string_07, number_07, character_07);
if let Example::Variant08 { string_08, number_08, character_08 } = value_08 {
println!("{}: {},{}", string_08, number_08, character_08);
}
}
}
}
}
}
}
}
if let (
Example::Variant01 { string_01, number_01, character_01 },
Example::Variant02 { string_02, number_02, character_02 },
Example::Variant03 { string_03, number_03, character_03 },
Example::Variant04 { string_04, number_04, character_04 },
Example::Variant05 { string_05, number_05, character_05 },
Example::Variant06 { string_06, number_06, character_06 },
Example::Variant07 { string_07, number_07, character_07 },
Example::Variant08 { string_08, number_08, character_08 },
) = (
value_01,
value_02,
value_03,
value_04,
value_05,
value_06,
value_07,
value_08,
) {
println!("{}: {},{}", string_01, number_01, character_01);
println!("{}: {},{}", string_02, number_02, character_02);
println!("{}: {},{}", string_03, number_03, character_03);
println!("{}: {},{}", string_04, number_04, character_04);
println!("{}: {},{}", string_05, number_05, character_05);
println!("{}: {},{}", string_06, number_06, character_06);
println!("{}: {},{}", string_07, number_07, character_07);
println!("{}: {},{}", string_08, number_08, character_08);
} both of the above examples look cleaner with the tuple version. We would have to look out for name shadowing: (maybe suggest to change the variable names to something unique?) if let Example::Variant01 { string, number, character } = value_01 {
println!("{}: {},{}", string, number, character);
if let Example::Variant02 { string, number, character } = value_02 {
println!("{}: {},{}", string, number, character);
}
}
if let (
Example::Variant01 { string, number, character },
Example::Variant02 { string, number, character },
) = (value_01, value_02) {
println!("{}: {},{}", string, number, character);
println!("{}: {},{}", string, number, character);
} |
This lint might be changed with |
Wouldn't this remove short-circuiting? if let (Some(a), Some(b)) = (expensive(), expensive()) { .. } |
This code
could be written like this
which would remove a layer of nesting.
The lint should only lint nested
if let
clauses, that are independent of each other.For example this must be ignored (or handled like in #2521):
but it should not matter, if there is code in between the
if let
clauses, as long as it does not influence the otherif let
clause(s):I think that there should not be a limit on how many nested
if let
clauses should be linted, because it would improve the readability either way.Another thing to consider would be else clauses. I think this lint should only trigger, if the nested else clauses have the same body:
would become
It might be too difficult (for clippy), but it would be possible to refactor nested
if let
-clauses with differentelse
-clauses too->
Should I move this issue in to several smaller ones?
The text was updated successfully, but these errors were encountered: