Skip to content

Commit 7e59dbc

Browse files
committed
Fix: if let _ = .. is not the same is if ..
1 parent a02806e commit 7e59dbc

10 files changed

+69
-30
lines changed

clippy_lints/src/matches.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,8 +1565,8 @@ mod redundant_pattern_match {
15651565
if let ExprKind::Match(op, arms, ref match_source) = &expr.kind {
15661566
match match_source {
15671567
MatchSource::Normal => find_sugg_for_match(cx, expr, op, arms),
1568-
MatchSource::IfLetDesugar { .. } => find_sugg_for_if_let(cx, expr, op, arms, "if"),
1569-
MatchSource::WhileLetDesugar => find_sugg_for_if_let(cx, expr, op, arms, "while"),
1568+
MatchSource::IfLetDesugar { .. } => find_sugg_for_if_let(cx, expr, op, &arms[0], "if"),
1569+
MatchSource::WhileLetDesugar => find_sugg_for_if_let(cx, expr, op, &arms[0], "while"),
15701570
_ => {},
15711571
}
15721572
}
@@ -1576,13 +1576,16 @@ mod redundant_pattern_match {
15761576
cx: &LateContext<'tcx>,
15771577
expr: &'tcx Expr<'_>,
15781578
op: &Expr<'_>,
1579-
arms: &[Arm<'_>],
1579+
arm: &Arm<'_>,
15801580
keyword: &'static str,
15811581
) {
1582-
let good_method = match arms[0].pat.kind {
1582+
let good_method = match arm.pat.kind {
15831583
PatKind::TupleStruct(ref path, ref patterns, _) if patterns.len() == 1 => {
15841584
if let PatKind::Wild = patterns[0].kind {
1585-
if match_qpath(path, &paths::RESULT_OK) {
1585+
if cx.typeck_results().expr_ty(op).needs_drop(cx.tcx, cx.param_env) {
1586+
// if let doesn't drop the value until after the block ends
1587+
return;
1588+
} else if match_qpath(path, &paths::RESULT_OK) {
15861589
"is_ok()"
15871590
} else if match_qpath(path, &paths::RESULT_ERR) {
15881591
"is_err()"
@@ -1631,7 +1634,7 @@ mod redundant_pattern_match {
16311634
span_lint_and_then(
16321635
cx,
16331636
REDUNDANT_PATTERN_MATCHING,
1634-
arms[0].pat.span,
1637+
arm.pat.span,
16351638
&format!("redundant pattern matching, consider using `{}`", good_method),
16361639
|diag| {
16371640
// while let ... = ... { ... }

tests/ui/redundant_pattern_matching_option.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ fn main() {
4848
} else {
4949
3
5050
};
51+
52+
// Issue #5746
53+
if let Some(_) = Some(vec![0]) {}
54+
if Some(vec![0]).is_none() {}
5155
}
5256

5357
fn gen_opt() -> Option<()> {

tests/ui/redundant_pattern_matching_option.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ fn main() {
5757
} else {
5858
3
5959
};
60+
61+
// Issue #5746
62+
if let Some(_) = Some(vec![0]) {}
63+
if let None = Some(vec![0]) {}
6064
}
6165

6266
fn gen_opt() -> Option<()> {

tests/ui/redundant_pattern_matching_option.stderr

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,32 +88,38 @@ error: redundant pattern matching, consider using `is_none()`
8888
LL | } else if let None = gen_opt() {
8989
| -------^^^^------------ help: try this: `if gen_opt().is_none()`
9090

91+
error: redundant pattern matching, consider using `is_none()`
92+
--> $DIR/redundant_pattern_matching_option.rs:63:12
93+
|
94+
LL | if let None = Some(vec![0]) {}
95+
| -------^^^^---------------- help: try this: `if Some(vec![0]).is_none()`
96+
9197
error: redundant pattern matching, consider using `is_some()`
92-
--> $DIR/redundant_pattern_matching_option.rs:74:12
98+
--> $DIR/redundant_pattern_matching_option.rs:78:12
9399
|
94100
LL | if let Some(_) = Some(42) {}
95101
| -------^^^^^^^----------- help: try this: `if Some(42).is_some()`
96102

97103
error: redundant pattern matching, consider using `is_none()`
98-
--> $DIR/redundant_pattern_matching_option.rs:76:12
104+
--> $DIR/redundant_pattern_matching_option.rs:80:12
99105
|
100106
LL | if let None = None::<()> {}
101107
| -------^^^^------------- help: try this: `if None::<()>.is_none()`
102108

103109
error: redundant pattern matching, consider using `is_some()`
104-
--> $DIR/redundant_pattern_matching_option.rs:78:15
110+
--> $DIR/redundant_pattern_matching_option.rs:82:15
105111
|
106112
LL | while let Some(_) = Some(42) {}
107113
| ----------^^^^^^^----------- help: try this: `while Some(42).is_some()`
108114

109115
error: redundant pattern matching, consider using `is_none()`
110-
--> $DIR/redundant_pattern_matching_option.rs:80:15
116+
--> $DIR/redundant_pattern_matching_option.rs:84:15
111117
|
112118
LL | while let None = None::<()> {}
113119
| ----------^^^^------------- help: try this: `while None::<()>.is_none()`
114120

115121
error: redundant pattern matching, consider using `is_some()`
116-
--> $DIR/redundant_pattern_matching_option.rs:82:5
122+
--> $DIR/redundant_pattern_matching_option.rs:86:5
117123
|
118124
LL | / match Some(42) {
119125
LL | | Some(_) => true,
@@ -122,13 +128,13 @@ LL | | };
122128
| |_____^ help: try this: `Some(42).is_some()`
123129

124130
error: redundant pattern matching, consider using `is_none()`
125-
--> $DIR/redundant_pattern_matching_option.rs:87:5
131+
--> $DIR/redundant_pattern_matching_option.rs:91:5
126132
|
127133
LL | / match None::<()> {
128134
LL | | Some(_) => false,
129135
LL | | None => true,
130136
LL | | };
131137
| |_____^ help: try this: `None::<()>.is_none()`
132138

133-
error: aborting due to 19 previous errors
139+
error: aborting due to 20 previous errors
134140

tests/ui/redundant_pattern_matching_poll.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ fn main() {
4545
} else {
4646
3
4747
};
48+
49+
// Issue #5746
50+
if let Ready(_) = Ready(vec![0]) {}
51+
if Ready(vec![0]).is_pending() {}
4852
}
4953

5054
fn gen_poll() -> Poll<()> {

tests/ui/redundant_pattern_matching_poll.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ fn main() {
5454
} else {
5555
3
5656
};
57+
58+
// Issue #5746
59+
if let Ready(_) = Ready(vec![0]) {}
60+
if let Pending = Ready(vec![0]) {}
5761
}
5862

5963
fn gen_poll() -> Poll<()> {

tests/ui/redundant_pattern_matching_poll.stderr

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,32 +82,38 @@ error: redundant pattern matching, consider using `is_pending()`
8282
LL | } else if let Pending = gen_poll() {
8383
| -------^^^^^^^------------- help: try this: `if gen_poll().is_pending()`
8484

85+
error: redundant pattern matching, consider using `is_pending()`
86+
--> $DIR/redundant_pattern_matching_poll.rs:60:12
87+
|
88+
LL | if let Pending = Ready(vec![0]) {}
89+
| -------^^^^^^^----------------- help: try this: `if Ready(vec![0]).is_pending()`
90+
8591
error: redundant pattern matching, consider using `is_ready()`
86-
--> $DIR/redundant_pattern_matching_poll.rs:68:12
92+
--> $DIR/redundant_pattern_matching_poll.rs:72:12
8793
|
8894
LL | if let Ready(_) = Ready(42) {}
8995
| -------^^^^^^^^------------ help: try this: `if Ready(42).is_ready()`
9096

9197
error: redundant pattern matching, consider using `is_pending()`
92-
--> $DIR/redundant_pattern_matching_poll.rs:70:12
98+
--> $DIR/redundant_pattern_matching_poll.rs:74:12
9399
|
94100
LL | if let Pending = Pending::<()> {}
95101
| -------^^^^^^^---------------- help: try this: `if Pending::<()>.is_pending()`
96102

97103
error: redundant pattern matching, consider using `is_ready()`
98-
--> $DIR/redundant_pattern_matching_poll.rs:72:15
104+
--> $DIR/redundant_pattern_matching_poll.rs:76:15
99105
|
100106
LL | while let Ready(_) = Ready(42) {}
101107
| ----------^^^^^^^^------------ help: try this: `while Ready(42).is_ready()`
102108

103109
error: redundant pattern matching, consider using `is_pending()`
104-
--> $DIR/redundant_pattern_matching_poll.rs:74:15
110+
--> $DIR/redundant_pattern_matching_poll.rs:78:15
105111
|
106112
LL | while let Pending = Pending::<()> {}
107113
| ----------^^^^^^^---------------- help: try this: `while Pending::<()>.is_pending()`
108114

109115
error: redundant pattern matching, consider using `is_ready()`
110-
--> $DIR/redundant_pattern_matching_poll.rs:76:5
116+
--> $DIR/redundant_pattern_matching_poll.rs:80:5
111117
|
112118
LL | / match Ready(42) {
113119
LL | | Ready(_) => true,
@@ -116,13 +122,13 @@ LL | | };
116122
| |_____^ help: try this: `Ready(42).is_ready()`
117123

118124
error: redundant pattern matching, consider using `is_pending()`
119-
--> $DIR/redundant_pattern_matching_poll.rs:81:5
125+
--> $DIR/redundant_pattern_matching_poll.rs:85:5
120126
|
121127
LL | / match Pending::<()> {
122128
LL | | Ready(_) => false,
123129
LL | | Pending => true,
124130
LL | | };
125131
| |_____^ help: try this: `Pending::<()>.is_pending()`
126132

127-
error: aborting due to 18 previous errors
133+
error: aborting due to 19 previous errors
128134

tests/ui/redundant_pattern_matching_result.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ fn main() {
5151
} else {
5252
3
5353
};
54+
55+
// Issue #5746
56+
if let Ok(_) = Ok::<_, ()>(vec![0]) {}
57+
if let Err(_) = Err::<(), _>(vec![0]) {}
5458
}
5559

5660
fn gen_res() -> Result<(), ()> {

tests/ui/redundant_pattern_matching_result.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ fn main() {
6363
} else {
6464
3
6565
};
66+
67+
// Issue #5746
68+
if let Ok(_) = Ok::<_, ()>(vec![0]) {}
69+
if let Err(_) = Err::<(), _>(vec![0]) {}
6670
}
6771

6872
fn gen_res() -> Result<(), ()> {

tests/ui/redundant_pattern_matching_result.stderr

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,55 +85,55 @@ LL | } else if let Err(_) = gen_res() {
8585
| -------^^^^^^------------ help: try this: `if gen_res().is_err()`
8686

8787
error: redundant pattern matching, consider using `is_some()`
88-
--> $DIR/redundant_pattern_matching_result.rs:84:19
88+
--> $DIR/redundant_pattern_matching_result.rs:88:19
8989
|
9090
LL | while let Some(_) = r#try!(result_opt()) {}
9191
| ----------^^^^^^^----------------------- help: try this: `while r#try!(result_opt()).is_some()`
9292

9393
error: redundant pattern matching, consider using `is_some()`
94-
--> $DIR/redundant_pattern_matching_result.rs:85:16
94+
--> $DIR/redundant_pattern_matching_result.rs:89:16
9595
|
9696
LL | if let Some(_) = r#try!(result_opt()) {}
9797
| -------^^^^^^^----------------------- help: try this: `if r#try!(result_opt()).is_some()`
9898

9999
error: redundant pattern matching, consider using `is_some()`
100-
--> $DIR/redundant_pattern_matching_result.rs:91:12
100+
--> $DIR/redundant_pattern_matching_result.rs:95:12
101101
|
102102
LL | if let Some(_) = m!() {}
103103
| -------^^^^^^^------- help: try this: `if m!().is_some()`
104104

105105
error: redundant pattern matching, consider using `is_some()`
106-
--> $DIR/redundant_pattern_matching_result.rs:92:15
106+
--> $DIR/redundant_pattern_matching_result.rs:96:15
107107
|
108108
LL | while let Some(_) = m!() {}
109109
| ----------^^^^^^^------- help: try this: `while m!().is_some()`
110110

111111
error: redundant pattern matching, consider using `is_ok()`
112-
--> $DIR/redundant_pattern_matching_result.rs:110:12
112+
--> $DIR/redundant_pattern_matching_result.rs:114:12
113113
|
114114
LL | if let Ok(_) = Ok::<i32, i32>(42) {}
115115
| -------^^^^^--------------------- help: try this: `if Ok::<i32, i32>(42).is_ok()`
116116

117117
error: redundant pattern matching, consider using `is_err()`
118-
--> $DIR/redundant_pattern_matching_result.rs:112:12
118+
--> $DIR/redundant_pattern_matching_result.rs:116:12
119119
|
120120
LL | if let Err(_) = Err::<i32, i32>(42) {}
121121
| -------^^^^^^---------------------- help: try this: `if Err::<i32, i32>(42).is_err()`
122122

123123
error: redundant pattern matching, consider using `is_ok()`
124-
--> $DIR/redundant_pattern_matching_result.rs:114:15
124+
--> $DIR/redundant_pattern_matching_result.rs:118:15
125125
|
126126
LL | while let Ok(_) = Ok::<i32, i32>(10) {}
127127
| ----------^^^^^--------------------- help: try this: `while Ok::<i32, i32>(10).is_ok()`
128128

129129
error: redundant pattern matching, consider using `is_err()`
130-
--> $DIR/redundant_pattern_matching_result.rs:116:15
130+
--> $DIR/redundant_pattern_matching_result.rs:120:15
131131
|
132132
LL | while let Err(_) = Ok::<i32, i32>(10) {}
133133
| ----------^^^^^^--------------------- help: try this: `while Ok::<i32, i32>(10).is_err()`
134134

135135
error: redundant pattern matching, consider using `is_ok()`
136-
--> $DIR/redundant_pattern_matching_result.rs:118:5
136+
--> $DIR/redundant_pattern_matching_result.rs:122:5
137137
|
138138
LL | / match Ok::<i32, i32>(42) {
139139
LL | | Ok(_) => true,
@@ -142,7 +142,7 @@ LL | | };
142142
| |_____^ help: try this: `Ok::<i32, i32>(42).is_ok()`
143143

144144
error: redundant pattern matching, consider using `is_err()`
145-
--> $DIR/redundant_pattern_matching_result.rs:123:5
145+
--> $DIR/redundant_pattern_matching_result.rs:127:5
146146
|
147147
LL | / match Err::<i32, i32>(42) {
148148
LL | | Ok(_) => false,

0 commit comments

Comments
 (0)