Skip to content

Commit 2422594

Browse files
committed
Auto merge of #10831 - disco07:master, r=flip1995
Fix redundant_pattern_match on matches! macro This PR solve this issue #10803 r? `@flip1995` changelog: none
2 parents 05740ad + 0b507c6 commit 2422594

8 files changed

+125
-48
lines changed

clippy_lints/src/matches/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ mod wild_in_or_pats;
2525

2626
use clippy_utils::msrvs::{self, Msrv};
2727
use clippy_utils::source::{snippet_opt, walk_span_to_context};
28-
use clippy_utils::{higher, in_constant, is_span_match, tokenize_with_text};
28+
use clippy_utils::{higher, in_constant, is_direct_expn_of, is_span_match, tokenize_with_text};
2929
use rustc_hir::{Arm, Expr, ExprKind, Local, MatchSource, Pat};
3030
use rustc_lexer::TokenKind;
3131
use rustc_lint::{LateContext, LateLintPass, LintContext};
@@ -974,12 +974,16 @@ impl_lint_pass!(Matches => [
974974

975975
impl<'tcx> LateLintPass<'tcx> for Matches {
976976
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
977-
if in_external_macro(cx.sess(), expr.span) {
977+
if is_direct_expn_of(expr.span, "matches").is_none() && in_external_macro(cx.sess(), expr.span) {
978978
return;
979979
}
980980
let from_expansion = expr.span.from_expansion();
981981

982982
if let ExprKind::Match(ex, arms, source) = expr.kind {
983+
if is_direct_expn_of(expr.span, "matches").is_some() {
984+
redundant_pattern_match::check_match(cx, expr, ex, arms);
985+
}
986+
983987
if source == MatchSource::Normal && !is_span_match(cx, expr.span) {
984988
return;
985989
}

clippy_lints/src/matches/redundant_pattern_match.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use super::REDUNDANT_PATTERN_MATCHING;
2-
use clippy_utils::diagnostics::span_lint_and_then;
2+
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
33
use clippy_utils::source::{snippet, walk_span_to_context};
44
use clippy_utils::sugg::Sugg;
55
use clippy_utils::ty::{is_type_diagnostic_item, needs_ordered_drop};
66
use clippy_utils::visitors::any_temporaries_need_ordered_drop;
7-
use clippy_utils::{higher, is_trait_method};
7+
use clippy_utils::{higher, is_expn_of, is_trait_method};
88
use if_chain::if_chain;
99
use rustc_ast::ast::LitKind;
1010
use rustc_errors::Applicability;
@@ -190,24 +190,19 @@ pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op
190190
let node_pair = (&arms[0].pat.kind, &arms[1].pat.kind);
191191

192192
if let Some(good_method) = found_good_method(cx, arms, node_pair) {
193-
let span = expr.span.to(op.span);
193+
let span = is_expn_of(expr.span, "matches").unwrap_or(expr.span.to(op.span));
194194
let result_expr = match &op.kind {
195195
ExprKind::AddrOf(_, _, borrowed) => borrowed,
196196
_ => op,
197197
};
198-
span_lint_and_then(
198+
span_lint_and_sugg(
199199
cx,
200200
REDUNDANT_PATTERN_MATCHING,
201-
expr.span,
201+
span,
202202
&format!("redundant pattern matching, consider using `{good_method}`"),
203-
|diag| {
204-
diag.span_suggestion(
205-
span,
206-
"try this",
207-
format!("{}.{good_method}", snippet(cx, result_expr.span, "_")),
208-
Applicability::MaybeIncorrect, // snippet
209-
);
210-
},
203+
"try this",
204+
format!("{}.{good_method}", snippet(cx, result_expr.span, "_")),
205+
Applicability::MachineApplicable,
211206
);
212207
}
213208
}

tests/ui/redundant_pattern_matching_option.fixed

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ fn main() {
4747

4848
issue6067();
4949
issue10726();
50+
issue10803();
5051

5152
let _ = if gen_opt().is_some() {
5253
1
@@ -107,3 +108,14 @@ fn issue10726() {
107108
_ => false,
108109
};
109110
}
111+
112+
fn issue10803() {
113+
let x = Some(42);
114+
115+
let _ = x.is_some();
116+
117+
let _ = x.is_none();
118+
119+
// Don't lint
120+
let _ = matches!(x, Some(16));
121+
}

tests/ui/redundant_pattern_matching_option.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ fn main() {
5656

5757
issue6067();
5858
issue10726();
59+
issue10803();
5960

6061
let _ = if let Some(_) = gen_opt() {
6162
1
@@ -134,3 +135,14 @@ fn issue10726() {
134135
_ => false,
135136
};
136137
}
138+
139+
fn issue10803() {
140+
let x = Some(42);
141+
142+
let _ = matches!(x, Some(_));
143+
144+
let _ = matches!(x, None);
145+
146+
// Don't lint
147+
let _ = matches!(x, Some(16));
148+
}

tests/ui/redundant_pattern_matching_option.stderr

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,49 +77,49 @@ LL | let _ = if let Some(_) = opt { true } else { false };
7777
| -------^^^^^^^------ help: try this: `if opt.is_some()`
7878

7979
error: redundant pattern matching, consider using `is_some()`
80-
--> $DIR/redundant_pattern_matching_option.rs:60:20
80+
--> $DIR/redundant_pattern_matching_option.rs:61:20
8181
|
8282
LL | let _ = if let Some(_) = gen_opt() {
8383
| -------^^^^^^^------------ help: try this: `if gen_opt().is_some()`
8484

8585
error: redundant pattern matching, consider using `is_none()`
86-
--> $DIR/redundant_pattern_matching_option.rs:62:19
86+
--> $DIR/redundant_pattern_matching_option.rs:63:19
8787
|
8888
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:68:12
92+
--> $DIR/redundant_pattern_matching_option.rs:69:12
9393
|
9494
LL | if let Some(..) = gen_opt() {}
9595
| -------^^^^^^^^------------ help: try this: `if gen_opt().is_some()`
9696

9797
error: redundant pattern matching, consider using `is_some()`
98-
--> $DIR/redundant_pattern_matching_option.rs:83:12
98+
--> $DIR/redundant_pattern_matching_option.rs:84:12
9999
|
100100
LL | if let Some(_) = Some(42) {}
101101
| -------^^^^^^^----------- help: try this: `if Some(42).is_some()`
102102

103103
error: redundant pattern matching, consider using `is_none()`
104-
--> $DIR/redundant_pattern_matching_option.rs:85:12
104+
--> $DIR/redundant_pattern_matching_option.rs:86:12
105105
|
106106
LL | if let None = None::<()> {}
107107
| -------^^^^------------- help: try this: `if None::<()>.is_none()`
108108

109109
error: redundant pattern matching, consider using `is_some()`
110-
--> $DIR/redundant_pattern_matching_option.rs:87:15
110+
--> $DIR/redundant_pattern_matching_option.rs:88:15
111111
|
112112
LL | while let Some(_) = Some(42) {}
113113
| ----------^^^^^^^----------- help: try this: `while Some(42).is_some()`
114114

115115
error: redundant pattern matching, consider using `is_none()`
116-
--> $DIR/redundant_pattern_matching_option.rs:89:15
116+
--> $DIR/redundant_pattern_matching_option.rs:90:15
117117
|
118118
LL | while let None = None::<()> {}
119119
| ----------^^^^------------- help: try this: `while None::<()>.is_none()`
120120

121121
error: redundant pattern matching, consider using `is_some()`
122-
--> $DIR/redundant_pattern_matching_option.rs:91:5
122+
--> $DIR/redundant_pattern_matching_option.rs:92:5
123123
|
124124
LL | / match Some(42) {
125125
LL | | Some(_) => true,
@@ -128,7 +128,7 @@ LL | | };
128128
| |_____^ help: try this: `Some(42).is_some()`
129129

130130
error: redundant pattern matching, consider using `is_none()`
131-
--> $DIR/redundant_pattern_matching_option.rs:96:5
131+
--> $DIR/redundant_pattern_matching_option.rs:97:5
132132
|
133133
LL | / match None::<()> {
134134
LL | | Some(_) => false,
@@ -137,19 +137,19 @@ LL | | };
137137
| |_____^ help: try this: `None::<()>.is_none()`
138138

139139
error: redundant pattern matching, consider using `is_none()`
140-
--> $DIR/redundant_pattern_matching_option.rs:104:12
140+
--> $DIR/redundant_pattern_matching_option.rs:105:12
141141
|
142142
LL | if let None = *(&None::<()>) {}
143143
| -------^^^^----------------- help: try this: `if (&None::<()>).is_none()`
144144

145145
error: redundant pattern matching, consider using `is_none()`
146-
--> $DIR/redundant_pattern_matching_option.rs:105:12
146+
--> $DIR/redundant_pattern_matching_option.rs:106:12
147147
|
148148
LL | if let None = *&None::<()> {}
149149
| -------^^^^--------------- help: try this: `if (&None::<()>).is_none()`
150150

151151
error: redundant pattern matching, consider using `is_some()`
152-
--> $DIR/redundant_pattern_matching_option.rs:111:5
152+
--> $DIR/redundant_pattern_matching_option.rs:112:5
153153
|
154154
LL | / match x {
155155
LL | | Some(_) => true,
@@ -158,7 +158,7 @@ LL | | };
158158
| |_____^ help: try this: `x.is_some()`
159159

160160
error: redundant pattern matching, consider using `is_none()`
161-
--> $DIR/redundant_pattern_matching_option.rs:116:5
161+
--> $DIR/redundant_pattern_matching_option.rs:117:5
162162
|
163163
LL | / match x {
164164
LL | | None => true,
@@ -167,7 +167,7 @@ LL | | };
167167
| |_____^ help: try this: `x.is_none()`
168168

169169
error: redundant pattern matching, consider using `is_none()`
170-
--> $DIR/redundant_pattern_matching_option.rs:121:5
170+
--> $DIR/redundant_pattern_matching_option.rs:122:5
171171
|
172172
LL | / match x {
173173
LL | | Some(_) => false,
@@ -176,13 +176,25 @@ LL | | };
176176
| |_____^ help: try this: `x.is_none()`
177177

178178
error: redundant pattern matching, consider using `is_some()`
179-
--> $DIR/redundant_pattern_matching_option.rs:126:5
179+
--> $DIR/redundant_pattern_matching_option.rs:127:5
180180
|
181181
LL | / match x {
182182
LL | | None => false,
183183
LL | | _ => true,
184184
LL | | };
185185
| |_____^ help: try this: `x.is_some()`
186186

187-
error: aborting due to 26 previous errors
187+
error: redundant pattern matching, consider using `is_some()`
188+
--> $DIR/redundant_pattern_matching_option.rs:142:13
189+
|
190+
LL | let _ = matches!(x, Some(_));
191+
| ^^^^^^^^^^^^^^^^^^^^ help: try this: `x.is_some()`
192+
193+
error: redundant pattern matching, consider using `is_none()`
194+
--> $DIR/redundant_pattern_matching_option.rs:144:13
195+
|
196+
LL | let _ = matches!(x, None);
197+
| ^^^^^^^^^^^^^^^^^ help: try this: `x.is_none()`
198+
199+
error: aborting due to 28 previous errors
188200

tests/ui/redundant_pattern_matching_result.fixed

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ fn main() {
4444
issue6067();
4545
issue6065();
4646
issue10726();
47+
issue10803();
4748

4849
let _ = if gen_res().is_ok() {
4950
1
@@ -133,3 +134,17 @@ fn issue10726() {
133134
_ => true,
134135
};
135136
}
137+
138+
fn issue10803() {
139+
let x: Result<i32, i32> = Ok(42);
140+
141+
let _ = x.is_ok();
142+
143+
let _ = x.is_err();
144+
145+
// Don't lint
146+
let _ = matches!(x, Ok(16));
147+
148+
// Don't lint
149+
let _ = matches!(x, Err(16));
150+
}

tests/ui/redundant_pattern_matching_result.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ fn main() {
5656
issue6067();
5757
issue6065();
5858
issue10726();
59+
issue10803();
5960

6061
let _ = if let Ok(_) = gen_res() {
6162
1
@@ -163,3 +164,17 @@ fn issue10726() {
163164
_ => true,
164165
};
165166
}
167+
168+
fn issue10803() {
169+
let x: Result<i32, i32> = Ok(42);
170+
171+
let _ = matches!(x, Ok(_));
172+
173+
let _ = matches!(x, Err(_));
174+
175+
// Don't lint
176+
let _ = matches!(x, Ok(16));
177+
178+
// Don't lint
179+
let _ = matches!(x, Err(16));
180+
}

0 commit comments

Comments
 (0)