option_if_let_else Do not warn when complex subpat is present

This commit is contained in:
dswij 2021-12-07 15:34:44 +08:00
parent 1c7df44e2d
commit b5fa5b37cc
4 changed files with 33 additions and 5 deletions

View file

@ -112,7 +112,7 @@ fn detect_option_if_let_else<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) ->
if !is_result_ok(cx, let_expr); // Don't lint on Result::ok because a different lint does it already if !is_result_ok(cx, let_expr); // Don't lint on Result::ok because a different lint does it already
if let PatKind::TupleStruct(struct_qpath, [inner_pat], _) = &let_pat.kind; if let PatKind::TupleStruct(struct_qpath, [inner_pat], _) = &let_pat.kind;
if is_lang_ctor(cx, struct_qpath, OptionSome); if is_lang_ctor(cx, struct_qpath, OptionSome);
if let PatKind::Binding(bind_annotation, _, id, _) = &inner_pat.kind; if let PatKind::Binding(bind_annotation, _, id, None) = &inner_pat.kind;
if let Some(some_captures) = can_move_expr_to_closure(cx, if_then); if let Some(some_captures) = can_move_expr_to_closure(cx, if_then);
if let Some(none_captures) = can_move_expr_to_closure(cx, if_else); if let Some(none_captures) = can_move_expr_to_closure(cx, if_else);
if some_captures if some_captures

View file

@ -86,6 +86,19 @@ fn pattern_to_vec(pattern: &str) -> Vec<String> {
.collect::<Vec<_>>() .collect::<Vec<_>>()
} }
enum DummyEnum {
One(u8),
Two,
}
// should not warn since there is a compled complex subpat
// see #7991
fn complex_subpat() -> DummyEnum {
let x = Some(DummyEnum::One(1));
let _ = if let Some(_one @ DummyEnum::One(..)) = x { 1 } else { 2 };
DummyEnum::Two
}
fn main() { fn main() {
let optional = Some(5); let optional = Some(5);
let _ = optional.map_or(5, |x| x + 2); let _ = optional.map_or(5, |x| x + 2);
@ -159,4 +172,5 @@ fn main() {
} }
let _ = pattern_to_vec("hello world"); let _ = pattern_to_vec("hello world");
let _ = complex_subpat();
} }

View file

@ -109,6 +109,19 @@ fn pattern_to_vec(pattern: &str) -> Vec<String> {
.collect::<Vec<_>>() .collect::<Vec<_>>()
} }
enum DummyEnum {
One(u8),
Two,
}
// should not warn since there is a compled complex subpat
// see #7991
fn complex_subpat() -> DummyEnum {
let x = Some(DummyEnum::One(1));
let _ = if let Some(_one @ DummyEnum::One(..)) = x { 1 } else { 2 };
DummyEnum::Two
}
fn main() { fn main() {
let optional = Some(5); let optional = Some(5);
let _ = if let Some(x) = optional { x + 2 } else { 5 }; let _ = if let Some(x) = optional { x + 2 } else { 5 };
@ -188,4 +201,5 @@ fn main() {
} }
let _ = pattern_to_vec("hello world"); let _ = pattern_to_vec("hello world");
let _ = complex_subpat();
} }

View file

@ -153,13 +153,13 @@ LL | | }
| |_____________^ help: try: `s.find('.').map_or_else(|| vec![s.to_string()], |idx| vec![s[..idx].to_string(), s[idx..].to_string()])` | |_____________^ help: try: `s.find('.').map_or_else(|| vec![s.to_string()], |idx| vec![s[..idx].to_string(), s[idx..].to_string()])`
error: use Option::map_or instead of an if let/else error: use Option::map_or instead of an if let/else
--> $DIR/option_if_let_else.rs:114:13 --> $DIR/option_if_let_else.rs:127:13
| |
LL | let _ = if let Some(x) = optional { x + 2 } else { 5 }; LL | let _ = if let Some(x) = optional { x + 2 } else { 5 };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `optional.map_or(5, |x| x + 2)` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `optional.map_or(5, |x| x + 2)`
error: use Option::map_or instead of an if let/else error: use Option::map_or instead of an if let/else
--> $DIR/option_if_let_else.rs:123:13 --> $DIR/option_if_let_else.rs:136:13
| |
LL | let _ = if let Some(x) = Some(0) { LL | let _ = if let Some(x) = Some(0) {
| _____________^ | _____________^
@ -181,13 +181,13 @@ LL ~ });
| |
error: use Option::map_or instead of an if let/else error: use Option::map_or instead of an if let/else
--> $DIR/option_if_let_else.rs:151:13 --> $DIR/option_if_let_else.rs:164:13
| |
LL | let _ = if let Some(x) = Some(0) { s.len() + x } else { s.len() }; LL | let _ = if let Some(x) = Some(0) { s.len() + x } else { s.len() };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Some(0).map_or(s.len(), |x| s.len() + x)` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Some(0).map_or(s.len(), |x| s.len() + x)`
error: use Option::map_or instead of an if let/else error: use Option::map_or instead of an if let/else
--> $DIR/option_if_let_else.rs:155:13 --> $DIR/option_if_let_else.rs:168:13
| |
LL | let _ = if let Some(x) = Some(0) { LL | let _ = if let Some(x) = Some(0) {
| _____________^ | _____________^