Skip to content

Commit 30db6ed

Browse files
committed
Auto merge of #10707 - y21:redudant_pattern_matching_rest_pat, r=Manishearth
check for `..` pattern in `redundant_pattern_matching` The `redundant_pattern_matching` lint currently checks for `if let Some(_) = ...`, but not for `if let Some(..) = ...`. This PR makes sure to also check for the `..` pattern in tuple structs. It also found one such instance in clippy itself so that shows it's worth checking for this pattern as well 😅 changelog: [`redundant_pattern_matching`]: check for `..` pattern in tuple structs
2 parents 4b6fdb4 + e8726b2 commit 30db6ed

5 files changed

+25
-12
lines changed

clippy_lints/src/indexing_slicing.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
170170
return;
171171
}
172172
// Index is a constant uint.
173-
if let Some(..) = constant(cx, cx.typeck_results(), index) {
173+
if constant(cx, cx.typeck_results(), index).is_some() {
174174
// Let rustc's `const_err` lint handle constant `usize` indexing on arrays.
175175
return;
176176
}

clippy_lints/src/matches/redundant_pattern_match.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@ fn find_sugg_for_if_let<'tcx>(
6363
// Determine which function should be used, and the type contained by the corresponding
6464
// variant.
6565
let (good_method, inner_ty) = match check_pat.kind {
66-
PatKind::TupleStruct(ref qpath, [sub_pat], _) => {
67-
if let PatKind::Wild = sub_pat.kind {
66+
PatKind::TupleStruct(ref qpath, args, rest) => {
67+
let is_wildcard = matches!(args.first().map(|p| &p.kind), Some(PatKind::Wild));
68+
let is_rest = matches!((args, rest.as_opt_usize()), ([], Some(_)));
69+
70+
if is_wildcard || is_rest {
6871
let res = cx.typeck_results().qpath_res(qpath, check_pat.hir_id);
6972
let Some(id) = res.opt_def_id().map(|ctor_id| cx.tcx.parent(ctor_id)) else { return };
7073
let lang_items = cx.tcx.lang_items();

tests/ui/redundant_pattern_matching_option.fixed

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ fn main() {
5454
} else {
5555
3
5656
};
57+
58+
if gen_opt().is_some() {}
5759
}
5860

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

tests/ui/redundant_pattern_matching_option.rs

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ fn main() {
6363
} else {
6464
3
6565
};
66+
67+
if let Some(..) = gen_opt() {}
6668
}
6769

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

tests/ui/redundant_pattern_matching_option.stderr

+15-9
Original file line numberDiff line numberDiff line change
@@ -89,31 +89,37 @@ LL | } else if let None = gen_opt() {
8989
| -------^^^^------------ help: try this: `if gen_opt().is_none()`
9090

9191
error: redundant pattern matching, consider using `is_some()`
92-
--> $DIR/redundant_pattern_matching_option.rs:80:12
92+
--> $DIR/redundant_pattern_matching_option.rs:67:12
93+
|
94+
LL | if let Some(..) = gen_opt() {}
95+
| -------^^^^^^^^------------ help: try this: `if gen_opt().is_some()`
96+
97+
error: redundant pattern matching, consider using `is_some()`
98+
--> $DIR/redundant_pattern_matching_option.rs:82: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:82:12
104+
--> $DIR/redundant_pattern_matching_option.rs:84: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:84:15
110+
--> $DIR/redundant_pattern_matching_option.rs:86: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:86:15
116+
--> $DIR/redundant_pattern_matching_option.rs:88: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:88:5
122+
--> $DIR/redundant_pattern_matching_option.rs:90:5
117123
|
118124
LL | / match Some(42) {
119125
LL | | Some(_) => true,
@@ -122,7 +128,7 @@ 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:93:5
131+
--> $DIR/redundant_pattern_matching_option.rs:95:5
126132
|
127133
LL | / match None::<()> {
128134
LL | | Some(_) => false,
@@ -131,16 +137,16 @@ LL | | };
131137
| |_____^ help: try this: `None::<()>.is_none()`
132138

133139
error: redundant pattern matching, consider using `is_none()`
134-
--> $DIR/redundant_pattern_matching_option.rs:101:12
140+
--> $DIR/redundant_pattern_matching_option.rs:103:12
135141
|
136142
LL | if let None = *(&None::<()>) {}
137143
| -------^^^^----------------- help: try this: `if (&None::<()>).is_none()`
138144

139145
error: redundant pattern matching, consider using `is_none()`
140-
--> $DIR/redundant_pattern_matching_option.rs:102:12
146+
--> $DIR/redundant_pattern_matching_option.rs:104:12
141147
|
142148
LL | if let None = *&None::<()> {}
143149
| -------^^^^--------------- help: try this: `if (&None::<()>).is_none()`
144150

145-
error: aborting due to 21 previous errors
151+
error: aborting due to 22 previous errors
146152

0 commit comments

Comments
 (0)