Skip to content
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

Fix bad suggestion on option_if_let_else when there is complex subpat #8086

Merged
merged 1 commit into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clippy_lints/src/option_if_let_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 let PatKind::TupleStruct(struct_qpath, [inner_pat], _) = &let_pat.kind;
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(none_captures) = can_move_expr_to_closure(cx, if_else);
if some_captures
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/option_if_let_else.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ fn pattern_to_vec(pattern: &str) -> Vec<String> {
.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() {
let optional = Some(5);
let _ = optional.map_or(5, |x| x + 2);
Expand Down Expand Up @@ -159,4 +172,5 @@ fn main() {
}

let _ = pattern_to_vec("hello world");
let _ = complex_subpat();
}
14 changes: 14 additions & 0 deletions tests/ui/option_if_let_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ fn pattern_to_vec(pattern: &str) -> Vec<String> {
.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() {
let optional = Some(5);
let _ = if let Some(x) = optional { x + 2 } else { 5 };
Expand Down Expand Up @@ -188,4 +201,5 @@ fn main() {
}

let _ = pattern_to_vec("hello world");
let _ = complex_subpat();
}
8 changes: 4 additions & 4 deletions tests/ui/option_if_let_else.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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()])`

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 };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `optional.map_or(5, |x| x + 2)`

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) {
| _____________^
Expand All @@ -181,13 +181,13 @@ LL ~ });
|

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() };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Some(0).map_or(s.len(), |x| s.len() + x)`

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) {
| _____________^
Expand Down