Skip to content

Commit 86cea73

Browse files
committed
Auto merge of rust-lang#8086 - dswij:7991, r=giraffate
Fix bad suggestion on `option_if_let_else` when there is complex subpat closes rust-lang#7991 Prefer not warning any complex subpat in `option_if_let_else` rather than suggesting obscure suggestions. changelog: [`option_if_let_else`] does not warn when complex subpat is present
2 parents ecae70f + b5fa5b3 commit 86cea73

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

clippy_lints/src/option_if_let_else.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fn detect_option_if_let_else<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) ->
112112
if !is_result_ok(cx, let_expr); // Don't lint on Result::ok because a different lint does it already
113113
if let PatKind::TupleStruct(struct_qpath, [inner_pat], _) = &let_pat.kind;
114114
if is_lang_ctor(cx, struct_qpath, OptionSome);
115-
if let PatKind::Binding(bind_annotation, _, id, _) = &inner_pat.kind;
115+
if let PatKind::Binding(bind_annotation, _, id, None) = &inner_pat.kind;
116116
if let Some(some_captures) = can_move_expr_to_closure(cx, if_then);
117117
if let Some(none_captures) = can_move_expr_to_closure(cx, if_else);
118118
if some_captures

tests/ui/option_if_let_else.fixed

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ fn pattern_to_vec(pattern: &str) -> Vec<String> {
8686
.collect::<Vec<_>>()
8787
}
8888

89+
enum DummyEnum {
90+
One(u8),
91+
Two,
92+
}
93+
94+
// should not warn since there is a compled complex subpat
95+
// see #7991
96+
fn complex_subpat() -> DummyEnum {
97+
let x = Some(DummyEnum::One(1));
98+
let _ = if let Some(_one @ DummyEnum::One(..)) = x { 1 } else { 2 };
99+
DummyEnum::Two
100+
}
101+
89102
fn main() {
90103
let optional = Some(5);
91104
let _ = optional.map_or(5, |x| x + 2);
@@ -159,4 +172,5 @@ fn main() {
159172
}
160173

161174
let _ = pattern_to_vec("hello world");
175+
let _ = complex_subpat();
162176
}

tests/ui/option_if_let_else.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,19 @@ fn pattern_to_vec(pattern: &str) -> Vec<String> {
109109
.collect::<Vec<_>>()
110110
}
111111

112+
enum DummyEnum {
113+
One(u8),
114+
Two,
115+
}
116+
117+
// should not warn since there is a compled complex subpat
118+
// see #7991
119+
fn complex_subpat() -> DummyEnum {
120+
let x = Some(DummyEnum::One(1));
121+
let _ = if let Some(_one @ DummyEnum::One(..)) = x { 1 } else { 2 };
122+
DummyEnum::Two
123+
}
124+
112125
fn main() {
113126
let optional = Some(5);
114127
let _ = if let Some(x) = optional { x + 2 } else { 5 };
@@ -188,4 +201,5 @@ fn main() {
188201
}
189202

190203
let _ = pattern_to_vec("hello world");
204+
let _ = complex_subpat();
191205
}

tests/ui/option_if_let_else.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,13 @@ LL | | }
153153
| |_____________^ help: try: `s.find('.').map_or_else(|| vec![s.to_string()], |idx| vec![s[..idx].to_string(), s[idx..].to_string()])`
154154

155155
error: use Option::map_or instead of an if let/else
156-
--> $DIR/option_if_let_else.rs:114:13
156+
--> $DIR/option_if_let_else.rs:127:13
157157
|
158158
LL | let _ = if let Some(x) = optional { x + 2 } else { 5 };
159159
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `optional.map_or(5, |x| x + 2)`
160160

161161
error: use Option::map_or instead of an if let/else
162-
--> $DIR/option_if_let_else.rs:123:13
162+
--> $DIR/option_if_let_else.rs:136:13
163163
|
164164
LL | let _ = if let Some(x) = Some(0) {
165165
| _____________^
@@ -181,13 +181,13 @@ LL ~ });
181181
|
182182

183183
error: use Option::map_or instead of an if let/else
184-
--> $DIR/option_if_let_else.rs:151:13
184+
--> $DIR/option_if_let_else.rs:164:13
185185
|
186186
LL | let _ = if let Some(x) = Some(0) { s.len() + x } else { s.len() };
187187
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Some(0).map_or(s.len(), |x| s.len() + x)`
188188

189189
error: use Option::map_or instead of an if let/else
190-
--> $DIR/option_if_let_else.rs:155:13
190+
--> $DIR/option_if_let_else.rs:168:13
191191
|
192192
LL | let _ = if let Some(x) = Some(0) {
193193
| _____________^

0 commit comments

Comments
 (0)