Skip to content

Commit c556695

Browse files
committed
Auto merge of #11522 - y21:redundant_guards_pat_lhs, r=giraffate
[`redundant_guards`]: lint if the pattern is on the left side A tiny improvement to the `redundant_guards` lint. There's no associated issue for this, just noticed it while going through the code. Right now it warns on `Some(x) if x == 2` and suggests `Some(2)`, but it didn't do that for `Some(x) if 2 == x` (i.e. when the local is on the right side and the pattern on the left side). changelog: [`redundant_guards`]: also lint if the pattern is on the left side
2 parents ddbe110 + 558ae4c commit c556695

File tree

4 files changed

+71
-12
lines changed

4 files changed

+71
-12
lines changed

clippy_lints/src/matches/redundant_guards.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,23 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'tcx>]) {
7070
);
7171
}
7272
// `Some(x) if x == Some(2)`
73+
// `Some(x) if Some(2) == x`
7374
else if let Guard::If(if_expr) = guard
7475
&& let ExprKind::Binary(bin_op, local, pat) = if_expr.kind
7576
&& matches!(bin_op.node, BinOpKind::Eq)
76-
&& expr_can_be_pat(cx, pat)
7777
// Ensure they have the same type. If they don't, we'd need deref coercion which isn't
7878
// possible (currently) in a pattern. In some cases, you can use something like
7979
// `as_deref` or similar but in general, we shouldn't lint this as it'd create an
8080
// extraordinary amount of FPs.
8181
//
8282
// This isn't necessary in the other two checks, as they must be a pattern already.
8383
&& cx.typeck_results().expr_ty(local) == cx.typeck_results().expr_ty(pat)
84-
&& let Some(binding) = get_pat_binding(cx, local, outer_arm)
84+
// Since we want to lint on both `x == Some(2)` and `Some(2) == x`, we might have to "swap"
85+
// `local` and `pat`, depending on which side they are.
86+
&& let Some((binding, pat)) = get_pat_binding(cx, local, outer_arm)
87+
.map(|binding| (binding, pat))
88+
.or_else(|| get_pat_binding(cx, pat, outer_arm).map(|binding| (binding, local)))
89+
&& expr_can_be_pat(cx, pat)
8590
{
8691
let pat_span = match (pat.kind, binding.byref_ident) {
8792
(ExprKind::AddrOf(BorrowKind::Ref, _, expr), Some(_)) => expr.span,

tests/ui/redundant_guards.fixed

+9
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ fn main() {
4343
},
4444
Some(Some(1)) => ..,
4545
Some(Some(2)) => ..,
46+
Some(Some(2)) => ..,
4647
// Don't lint, since x is used in the body
4748
Some(x) if let Some(1) = x => {
4849
x;
@@ -56,11 +57,13 @@ fn main() {
5657
Some(x) if matches!(y, 1 if true) => ..,
5758
Some(x) if let 1 = y => ..,
5859
Some(x) if y == 2 => ..,
60+
Some(x) if 2 == y => ..,
5961
_ => todo!(),
6062
};
6163
let a = A(1);
6264
match a {
6365
_ if a.0 == 1 => {},
66+
_ if 1 == a.0 => {},
6467
_ => todo!(),
6568
}
6669
let b = B { e: Some(A(0)) };
@@ -119,6 +122,7 @@ fn h(v: Option<u32>) {
119122
fn f(s: Option<std::ffi::OsString>) {
120123
match s {
121124
Some(x) if x == "a" => {},
125+
Some(x) if "a" == x => {},
122126
_ => {},
123127
}
124128
}
@@ -140,6 +144,7 @@ static CONST_S: S = S { a: 1 };
140144
fn g(opt_s: Option<S>) {
141145
match opt_s {
142146
Some(x) if x == CONST_S => {},
147+
Some(x) if CONST_S == x => {},
143148
_ => {},
144149
}
145150
}
@@ -157,6 +162,7 @@ mod issue11465 {
157162
fn issue11465() {
158163
let c = Some(1);
159164
match c {
165+
Some(1) => {},
160166
Some(1) => {},
161167
Some(2) => {},
162168
Some(3) => {},
@@ -166,6 +172,7 @@ mod issue11465 {
166172
let enum_a = A::Foo([98, 97, 114]);
167173
match enum_a {
168174
A::Foo(ref arr) if arr == b"foo" => {},
175+
A::Foo(ref arr) if b"foo" == arr => {},
169176
A::Foo(ref arr) if let b"bar" = arr => {},
170177
A::Foo(ref arr) if matches!(arr, b"baz") => {},
171178
_ => {},
@@ -177,6 +184,8 @@ mod issue11465 {
177184
};
178185
match struct_b {
179186
B { ref b, .. } if b == "bar" => {},
187+
B { ref b, .. } if "bar" == b => {},
188+
B { c: 1, .. } => {},
180189
B { c: 1, .. } => {},
181190
B { c: 1, .. } => {},
182191
B { c: 1, .. } => {},

tests/ui/redundant_guards.rs

+9
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ fn main() {
4343
},
4444
Some(x) if let Some(1) = x => ..,
4545
Some(x) if x == Some(2) => ..,
46+
Some(x) if Some(2) == x => ..,
4647
// Don't lint, since x is used in the body
4748
Some(x) if let Some(1) = x => {
4849
x;
@@ -56,11 +57,13 @@ fn main() {
5657
Some(x) if matches!(y, 1 if true) => ..,
5758
Some(x) if let 1 = y => ..,
5859
Some(x) if y == 2 => ..,
60+
Some(x) if 2 == y => ..,
5961
_ => todo!(),
6062
};
6163
let a = A(1);
6264
match a {
6365
_ if a.0 == 1 => {},
66+
_ if 1 == a.0 => {},
6467
_ => todo!(),
6568
}
6669
let b = B { e: Some(A(0)) };
@@ -119,6 +122,7 @@ fn h(v: Option<u32>) {
119122
fn f(s: Option<std::ffi::OsString>) {
120123
match s {
121124
Some(x) if x == "a" => {},
125+
Some(x) if "a" == x => {},
122126
_ => {},
123127
}
124128
}
@@ -140,6 +144,7 @@ static CONST_S: S = S { a: 1 };
140144
fn g(opt_s: Option<S>) {
141145
match opt_s {
142146
Some(x) if x == CONST_S => {},
147+
Some(x) if CONST_S == x => {},
143148
_ => {},
144149
}
145150
}
@@ -158,6 +163,7 @@ mod issue11465 {
158163
let c = Some(1);
159164
match c {
160165
Some(ref x) if x == &1 => {},
166+
Some(ref x) if &1 == x => {},
161167
Some(ref x) if let &2 = x => {},
162168
Some(ref x) if matches!(x, &3) => {},
163169
_ => {},
@@ -166,6 +172,7 @@ mod issue11465 {
166172
let enum_a = A::Foo([98, 97, 114]);
167173
match enum_a {
168174
A::Foo(ref arr) if arr == b"foo" => {},
175+
A::Foo(ref arr) if b"foo" == arr => {},
169176
A::Foo(ref arr) if let b"bar" = arr => {},
170177
A::Foo(ref arr) if matches!(arr, b"baz") => {},
171178
_ => {},
@@ -177,7 +184,9 @@ mod issue11465 {
177184
};
178185
match struct_b {
179186
B { ref b, .. } if b == "bar" => {},
187+
B { ref b, .. } if "bar" == b => {},
180188
B { ref c, .. } if c == &1 => {},
189+
B { ref c, .. } if &1 == c => {},
181190
B { ref c, .. } if let &1 = c => {},
182191
B { ref c, .. } if matches!(c, &1) => {},
183192
_ => {},

tests/ui/redundant_guards.stderr

+46-10
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,19 @@ LL + Some(Some(2)) => ..,
6060
|
6161

6262
error: redundant guard
63-
--> $DIR/redundant_guards.rs:68:20
63+
--> $DIR/redundant_guards.rs:46:20
64+
|
65+
LL | Some(x) if Some(2) == x => ..,
66+
| ^^^^^^^^^^^^
67+
|
68+
help: try
69+
|
70+
LL - Some(x) if Some(2) == x => ..,
71+
LL + Some(Some(2)) => ..,
72+
|
73+
74+
error: redundant guard
75+
--> $DIR/redundant_guards.rs:71:20
6476
|
6577
LL | B { e } if matches!(e, Some(A(2))) => ..,
6678
| ^^^^^^^^^^^^^^^^^^^^^^^
@@ -72,7 +84,7 @@ LL + B { e: Some(A(2)) } => ..,
7284
|
7385

7486
error: redundant guard
75-
--> $DIR/redundant_guards.rs:105:20
87+
--> $DIR/redundant_guards.rs:108:20
7688
|
7789
LL | E::A(y) if y == "not from an or pattern" => {},
7890
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -84,7 +96,7 @@ LL + E::A("not from an or pattern") => {},
8496
|
8597

8698
error: redundant guard
87-
--> $DIR/redundant_guards.rs:112:14
99+
--> $DIR/redundant_guards.rs:115:14
88100
|
89101
LL | x if matches!(x, Some(0)) => ..,
90102
| ^^^^^^^^^^^^^^^^^^^^
@@ -96,7 +108,7 @@ LL + Some(0) => ..,
96108
|
97109

98110
error: redundant guard
99-
--> $DIR/redundant_guards.rs:160:28
111+
--> $DIR/redundant_guards.rs:165:28
100112
|
101113
LL | Some(ref x) if x == &1 => {},
102114
| ^^^^^^^
@@ -108,7 +120,19 @@ LL + Some(1) => {},
108120
|
109121

110122
error: redundant guard
111-
--> $DIR/redundant_guards.rs:161:28
123+
--> $DIR/redundant_guards.rs:166:28
124+
|
125+
LL | Some(ref x) if &1 == x => {},
126+
| ^^^^^^^
127+
|
128+
help: try
129+
|
130+
LL - Some(ref x) if &1 == x => {},
131+
LL + Some(1) => {},
132+
|
133+
134+
error: redundant guard
135+
--> $DIR/redundant_guards.rs:167:28
112136
|
113137
LL | Some(ref x) if let &2 = x => {},
114138
| ^^^^^^^^^^
@@ -120,7 +144,7 @@ LL + Some(2) => {},
120144
|
121145

122146
error: redundant guard
123-
--> $DIR/redundant_guards.rs:162:28
147+
--> $DIR/redundant_guards.rs:168:28
124148
|
125149
LL | Some(ref x) if matches!(x, &3) => {},
126150
| ^^^^^^^^^^^^^^^
@@ -132,7 +156,7 @@ LL + Some(3) => {},
132156
|
133157

134158
error: redundant guard
135-
--> $DIR/redundant_guards.rs:180:32
159+
--> $DIR/redundant_guards.rs:188:32
136160
|
137161
LL | B { ref c, .. } if c == &1 => {},
138162
| ^^^^^^^
@@ -144,7 +168,19 @@ LL + B { c: 1, .. } => {},
144168
|
145169

146170
error: redundant guard
147-
--> $DIR/redundant_guards.rs:181:32
171+
--> $DIR/redundant_guards.rs:189:32
172+
|
173+
LL | B { ref c, .. } if &1 == c => {},
174+
| ^^^^^^^
175+
|
176+
help: try
177+
|
178+
LL - B { ref c, .. } if &1 == c => {},
179+
LL + B { c: 1, .. } => {},
180+
|
181+
182+
error: redundant guard
183+
--> $DIR/redundant_guards.rs:190:32
148184
|
149185
LL | B { ref c, .. } if let &1 = c => {},
150186
| ^^^^^^^^^^
@@ -156,7 +192,7 @@ LL + B { c: 1, .. } => {},
156192
|
157193

158194
error: redundant guard
159-
--> $DIR/redundant_guards.rs:182:32
195+
--> $DIR/redundant_guards.rs:191:32
160196
|
161197
LL | B { ref c, .. } if matches!(c, &1) => {},
162198
| ^^^^^^^^^^^^^^^
@@ -167,5 +203,5 @@ LL - B { ref c, .. } if matches!(c, &1) => {},
167203
LL + B { c: 1, .. } => {},
168204
|
169205

170-
error: aborting due to 14 previous errors
206+
error: aborting due to 17 previous errors
171207

0 commit comments

Comments
 (0)