Skip to content

Commit 4fb9e12

Browse files
committed
Auto merge of rust-lang#11641 - Alexendoo:negative-redundant-guards, r=Jarcho
Allow negative literals in `redundant_guards` changelog: none
2 parents 32c006c + b32d7c0 commit 4fb9e12

File tree

4 files changed

+62
-18
lines changed

4 files changed

+62
-18
lines changed

clippy_lints/src/matches/redundant_guards.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clippy_utils::visitors::{for_each_expr, is_local_used};
55
use rustc_ast::{BorrowKind, LitKind};
66
use rustc_errors::Applicability;
77
use rustc_hir::def::{DefKind, Res};
8-
use rustc_hir::{Arm, BinOpKind, Expr, ExprKind, MatchSource, Node, Pat, PatKind};
8+
use rustc_hir::{Arm, BinOpKind, Expr, ExprKind, MatchSource, Node, Pat, PatKind, UnOp};
99
use rustc_lint::LateContext;
1010
use rustc_span::symbol::Ident;
1111
use rustc_span::{Span, Symbol};
@@ -269,7 +269,11 @@ fn expr_can_be_pat(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
269269
Res::Def(DefKind::Struct | DefKind::Enum | DefKind::Ctor(..), ..),
270270
)
271271
},
272-
ExprKind::AddrOf(..) | ExprKind::Array(..) | ExprKind::Tup(..) | ExprKind::Struct(..) => true,
272+
ExprKind::AddrOf(..)
273+
| ExprKind::Array(..)
274+
| ExprKind::Tup(..)
275+
| ExprKind::Struct(..)
276+
| ExprKind::Unary(UnOp::Neg, _) => true,
273277
ExprKind::Lit(lit) if !matches!(lit.node, LitKind::Float(..)) => true,
274278
_ => false,
275279
} {

tests/ui/redundant_guards.fixed

+8
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@ fn h(v: Option<u32>) {
117117
};
118118
}
119119

120+
fn negative_literal(i: i32) {
121+
match i {
122+
-1 => {},
123+
1 => {},
124+
_ => {},
125+
}
126+
}
127+
120128
// Do not lint
121129

122130
fn f(s: Option<std::ffi::OsString>) {

tests/ui/redundant_guards.rs

+8
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@ fn h(v: Option<u32>) {
117117
};
118118
}
119119

120+
fn negative_literal(i: i32) {
121+
match i {
122+
i if i == -1 => {},
123+
i if i == 1 => {},
124+
_ => {},
125+
}
126+
}
127+
120128
// Do not lint
121129

122130
fn f(s: Option<std::ffi::OsString>) {

tests/ui/redundant_guards.stderr

+40-16
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,31 @@ LL + Some(0) => ..,
108108
|
109109

110110
error: redundant guard
111-
--> $DIR/redundant_guards.rs:165:28
111+
--> $DIR/redundant_guards.rs:122:14
112+
|
113+
LL | i if i == -1 => {},
114+
| ^^^^^^^
115+
|
116+
help: try
117+
|
118+
LL - i if i == -1 => {},
119+
LL + -1 => {},
120+
|
121+
122+
error: redundant guard
123+
--> $DIR/redundant_guards.rs:123:14
124+
|
125+
LL | i if i == 1 => {},
126+
| ^^^^^^
127+
|
128+
help: try
129+
|
130+
LL - i if i == 1 => {},
131+
LL + 1 => {},
132+
|
133+
134+
error: redundant guard
135+
--> $DIR/redundant_guards.rs:173:28
112136
|
113137
LL | Some(ref x) if x == &1 => {},
114138
| ^^^^^^^
@@ -120,7 +144,7 @@ LL + Some(1) => {},
120144
|
121145

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

134158
error: redundant guard
135-
--> $DIR/redundant_guards.rs:167:28
159+
--> $DIR/redundant_guards.rs:175:28
136160
|
137161
LL | Some(ref x) if let &2 = x => {},
138162
| ^^^^^^^^^^
@@ -144,7 +168,7 @@ LL + Some(2) => {},
144168
|
145169

146170
error: redundant guard
147-
--> $DIR/redundant_guards.rs:168:28
171+
--> $DIR/redundant_guards.rs:176:28
148172
|
149173
LL | Some(ref x) if matches!(x, &3) => {},
150174
| ^^^^^^^^^^^^^^^
@@ -156,7 +180,7 @@ LL + Some(3) => {},
156180
|
157181

158182
error: redundant guard
159-
--> $DIR/redundant_guards.rs:188:32
183+
--> $DIR/redundant_guards.rs:196:32
160184
|
161185
LL | B { ref c, .. } if c == &1 => {},
162186
| ^^^^^^^
@@ -168,7 +192,7 @@ LL + B { c: 1, .. } => {},
168192
|
169193

170194
error: redundant guard
171-
--> $DIR/redundant_guards.rs:189:32
195+
--> $DIR/redundant_guards.rs:197:32
172196
|
173197
LL | B { ref c, .. } if &1 == c => {},
174198
| ^^^^^^^
@@ -180,7 +204,7 @@ LL + B { c: 1, .. } => {},
180204
|
181205

182206
error: redundant guard
183-
--> $DIR/redundant_guards.rs:190:32
207+
--> $DIR/redundant_guards.rs:198:32
184208
|
185209
LL | B { ref c, .. } if let &1 = c => {},
186210
| ^^^^^^^^^^
@@ -192,7 +216,7 @@ LL + B { c: 1, .. } => {},
192216
|
193217

194218
error: redundant guard
195-
--> $DIR/redundant_guards.rs:191:32
219+
--> $DIR/redundant_guards.rs:199:32
196220
|
197221
LL | B { ref c, .. } if matches!(c, &1) => {},
198222
| ^^^^^^^^^^^^^^^
@@ -204,7 +228,7 @@ LL + B { c: 1, .. } => {},
204228
|
205229

206230
error: redundant guard
207-
--> $DIR/redundant_guards.rs:201:26
231+
--> $DIR/redundant_guards.rs:209:26
208232
|
209233
LL | Some(Some(x)) if x.is_empty() => {},
210234
| ^^^^^^^^^^^^
@@ -216,7 +240,7 @@ LL + Some(Some("")) => {},
216240
|
217241

218242
error: redundant guard
219-
--> $DIR/redundant_guards.rs:212:26
243+
--> $DIR/redundant_guards.rs:220:26
220244
|
221245
LL | Some(Some(x)) if x.is_empty() => {},
222246
| ^^^^^^^^^^^^
@@ -228,7 +252,7 @@ LL + Some(Some([])) => {},
228252
|
229253

230254
error: redundant guard
231-
--> $DIR/redundant_guards.rs:217:26
255+
--> $DIR/redundant_guards.rs:225:26
232256
|
233257
LL | Some(Some(x)) if x.is_empty() => {},
234258
| ^^^^^^^^^^^^
@@ -240,7 +264,7 @@ LL + Some(Some([])) => {},
240264
|
241265

242266
error: redundant guard
243-
--> $DIR/redundant_guards.rs:228:26
267+
--> $DIR/redundant_guards.rs:236:26
244268
|
245269
LL | Some(Some(x)) if x.starts_with(&[]) => {},
246270
| ^^^^^^^^^^^^^^^^^^
@@ -252,7 +276,7 @@ LL + Some(Some([..])) => {},
252276
|
253277

254278
error: redundant guard
255-
--> $DIR/redundant_guards.rs:233:26
279+
--> $DIR/redundant_guards.rs:241:26
256280
|
257281
LL | Some(Some(x)) if x.starts_with(&[1]) => {},
258282
| ^^^^^^^^^^^^^^^^^^^
@@ -264,7 +288,7 @@ LL + Some(Some([1, ..])) => {},
264288
|
265289

266290
error: redundant guard
267-
--> $DIR/redundant_guards.rs:238:26
291+
--> $DIR/redundant_guards.rs:246:26
268292
|
269293
LL | Some(Some(x)) if x.starts_with(&[1, 2]) => {},
270294
| ^^^^^^^^^^^^^^^^^^^^^^
@@ -276,7 +300,7 @@ LL + Some(Some([1, 2, ..])) => {},
276300
|
277301

278302
error: redundant guard
279-
--> $DIR/redundant_guards.rs:243:26
303+
--> $DIR/redundant_guards.rs:251:26
280304
|
281305
LL | Some(Some(x)) if x.ends_with(&[1, 2]) => {},
282306
| ^^^^^^^^^^^^^^^^^^^^
@@ -287,5 +311,5 @@ LL - Some(Some(x)) if x.ends_with(&[1, 2]) => {},
287311
LL + Some(Some([.., 1, 2])) => {},
288312
|
289313

290-
error: aborting due to 24 previous errors
314+
error: aborting due to 26 previous errors
291315

0 commit comments

Comments
 (0)