Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 9e535f6

Browse files
committed
fix conflict with matches macro
1 parent d2bbe76 commit 9e535f6

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

clippy_lints/src/matches/match_like_matches.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
use super::REDUNDANT_PATTERN_MATCHING;
12
use clippy_utils::diagnostics::span_lint_and_sugg;
3+
use clippy_utils::is_lint_allowed;
24
use clippy_utils::is_wild;
35
use clippy_utils::source::snippet_with_applicability;
46
use clippy_utils::span_contains_comment;
57
use rustc_ast::{Attribute, LitKind};
68
use rustc_errors::Applicability;
7-
use rustc_hir::{Arm, BorrowKind, Expr, ExprKind, Guard, Pat};
9+
use rustc_hir::{Arm, BorrowKind, Expr, ExprKind, Guard, Pat, PatKind, QPath};
810
use rustc_lint::{LateContext, LintContext};
911
use rustc_middle::ty;
1012
use rustc_span::source_map::Spanned;
@@ -99,6 +101,14 @@ where
99101
}
100102
}
101103

104+
for arm in iter_without_last.clone() {
105+
if let Some(pat) = arm.1 {
106+
if !is_lint_allowed(cx, REDUNDANT_PATTERN_MATCHING, pat.hir_id) && is_some(pat.kind) {
107+
return false;
108+
}
109+
}
110+
}
111+
102112
// The suggestion may be incorrect, because some arms can have `cfg` attributes
103113
// evaluated into `false` and so such arms will be stripped before.
104114
let mut applicability = Applicability::MaybeIncorrect;
@@ -170,3 +180,19 @@ fn find_bool_lit(ex: &ExprKind<'_>) -> Option<bool> {
170180
_ => None,
171181
}
172182
}
183+
184+
fn is_some(path_kind: PatKind<'_>) -> bool {
185+
match path_kind {
186+
PatKind::TupleStruct(ref path_left, patterns, _) if is_wild(&patterns[0]) => match path_left {
187+
QPath::Resolved(_, path) => {
188+
let name = path.segments[0].ident;
189+
if name.as_str() == "Some" {
190+
return true;
191+
}
192+
return false;
193+
},
194+
_ => false,
195+
},
196+
_ => false,
197+
}
198+
}

clippy_lints/src/matches/redundant_pattern_match.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ fn find_sugg_for_if_let<'tcx>(
186186
}
187187

188188
pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op: &Expr<'_>, arms: &[Arm<'_>]) {
189-
//eprintln!("{:#?}", expr);
190189
if arms.len() == 2 {
191190
let node_pair = (&arms[0].pat.kind, &arms[1].pat.kind);
192191
let found_good_method = match node_pair {

tests/ui/match_expr_like_matches_macro.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn main() {
1515
let _y = matches!(x, Some(0));
1616

1717
// Lint
18-
let _w = matches!(x, Some(_));
18+
let _w = x.is_some();
1919

2020
// Turn into is_none
2121
let _z = x.is_none();

tests/ui/match_expr_like_matches_macro.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@ LL | | };
1010
|
1111
= note: `-D clippy::match-like-matches-macro` implied by `-D warnings`
1212

13-
error: match expression looks like `matches!` macro
13+
error: redundant pattern matching, consider using `is_some()`
1414
--> $DIR/match_expr_like_matches_macro.rs:21:14
1515
|
1616
LL | let _w = match x {
1717
| ______________^
1818
LL | | Some(_) => true,
1919
LL | | _ => false,
2020
LL | | };
21-
| |_____^ help: try this: `matches!(x, Some(_))`
21+
| |_____^ help: try this: `x.is_some()`
22+
|
23+
= note: `-D clippy::redundant-pattern-matching` implied by `-D warnings`
2224

2325
error: redundant pattern matching, consider using `is_none()`
2426
--> $DIR/match_expr_like_matches_macro.rs:27:14
@@ -29,8 +31,6 @@ LL | | Some(_) => false,
2931
LL | | None => true,
3032
LL | | };
3133
| |_____^ help: try this: `x.is_none()`
32-
|
33-
= note: `-D clippy::redundant-pattern-matching` implied by `-D warnings`
3434

3535
error: match expression looks like `matches!` macro
3636
--> $DIR/match_expr_like_matches_macro.rs:33:15

0 commit comments

Comments
 (0)