Skip to content

Commit cc32305

Browse files
committed
make match_like_matches_macro only apply to matches with a wildcard
1 parent 1740dda commit cc32305

File tree

5 files changed

+29
-16
lines changed

5 files changed

+29
-16
lines changed

clippy_lints/src/assign_ops.rs

-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ fn is_commutative(op: hir::BinOpKind) -> bool {
237237
use rustc_hir::BinOpKind::{
238238
Add, And, BitAnd, BitOr, BitXor, Div, Eq, Ge, Gt, Le, Lt, Mul, Ne, Or, Rem, Shl, Shr, Sub,
239239
};
240-
#[allow(clippy::match_like_matches_macro)]
241240
match op {
242241
Add | Mul | And | Or | BitXor | BitAnd | BitOr | Eq | Ne => true,
243242
Sub | Div | Rem | Shl | Shr | Lt | Le | Ge | Gt => false,

clippy_lints/src/matches.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -446,11 +446,12 @@ declare_clippy_lint! {
446446
}
447447

448448
declare_clippy_lint! {
449-
/// **What it does:** Checks for `match` expressions producing a `bool` that could be written using `matches!`
449+
/// **What it does:** Checks for `match` or `if let` expressions producing a
450+
/// `bool` that could be written using `matches!`
450451
///
451452
/// **Why is this bad?** Readability and needless complexity.
452453
///
453-
/// **Known problems:** This can turn an intentionally exhaustive match into a non-exhaustive one.
454+
/// **Known problems:** None
454455
///
455456
/// **Example:**
456457
/// ```rust
@@ -462,8 +463,14 @@ declare_clippy_lint! {
462463
/// _ => false,
463464
/// };
464465
///
466+
/// let a = if let Some(0) = x {
467+
/// true
468+
/// } else {
469+
/// false
470+
/// }
471+
///
465472
/// // Good
466-
/// let a = matches!(x, Some(5));
473+
/// let a = matches!(x, Some(0));
467474
/// ```
468475
pub MATCH_LIKE_MATCHES_MACRO,
469476
style,
@@ -1068,6 +1075,7 @@ fn find_matches_sugg(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr
10681075
if_chain! {
10691076
if arms.len() == 2;
10701077
if cx.tables().expr_ty(expr).is_bool();
1078+
if is_wild(&arms[1].pat);
10711079
if let Some(first) = find_bool_lit(&arms[0].body.kind, desugared);
10721080
if let Some(second) = find_bool_lit(&arms[1].body.kind, desugared);
10731081
if first != second;

tests/ui/question_mark.fixed

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ pub enum SeemsOption<T> {
2323

2424
impl<T> SeemsOption<T> {
2525
pub fn is_none(&self) -> bool {
26-
matches!(*self, SeemsOption::None)
26+
match *self {
27+
SeemsOption::None => true,
28+
SeemsOption::Some(_) => false,
29+
}
2730
}
2831
}
2932

tests/ui/question_mark.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ pub enum SeemsOption<T> {
2525

2626
impl<T> SeemsOption<T> {
2727
pub fn is_none(&self) -> bool {
28-
matches!(*self, SeemsOption::None)
28+
match *self {
29+
SeemsOption::None => true,
30+
SeemsOption::Some(_) => false,
31+
}
2932
}
3033
}
3134

tests/ui/question_mark.stderr

+10-10
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@ LL | | }
99
= note: `-D clippy::question-mark` implied by `-D warnings`
1010

1111
error: this block may be rewritten with the `?` operator
12-
--> $DIR/question_mark.rs:47:9
12+
--> $DIR/question_mark.rs:50:9
1313
|
1414
LL | / if (self.opt).is_none() {
1515
LL | | return None;
1616
LL | | }
1717
| |_________^ help: replace it with: `(self.opt)?;`
1818

1919
error: this block may be rewritten with the `?` operator
20-
--> $DIR/question_mark.rs:51:9
20+
--> $DIR/question_mark.rs:54:9
2121
|
2222
LL | / if self.opt.is_none() {
2323
LL | | return None
2424
LL | | }
2525
| |_________^ help: replace it with: `self.opt?;`
2626

2727
error: this block may be rewritten with the `?` operator
28-
--> $DIR/question_mark.rs:55:17
28+
--> $DIR/question_mark.rs:58:17
2929
|
3030
LL | let _ = if self.opt.is_none() {
3131
| _________________^
@@ -36,7 +36,7 @@ LL | | };
3636
| |_________^ help: replace it with: `Some(self.opt?)`
3737

3838
error: this if-let-else may be rewritten with the `?` operator
39-
--> $DIR/question_mark.rs:61:17
39+
--> $DIR/question_mark.rs:64:17
4040
|
4141
LL | let _ = if let Some(x) = self.opt {
4242
| _________________^
@@ -47,31 +47,31 @@ LL | | };
4747
| |_________^ help: replace it with: `self.opt?`
4848

4949
error: this block may be rewritten with the `?` operator
50-
--> $DIR/question_mark.rs:78:9
50+
--> $DIR/question_mark.rs:81:9
5151
|
5252
LL | / if self.opt.is_none() {
5353
LL | | return None;
5454
LL | | }
5555
| |_________^ help: replace it with: `self.opt.as_ref()?;`
5656

5757
error: this block may be rewritten with the `?` operator
58-
--> $DIR/question_mark.rs:86:9
58+
--> $DIR/question_mark.rs:89:9
5959
|
6060
LL | / if self.opt.is_none() {
6161
LL | | return None;
6262
LL | | }
6363
| |_________^ help: replace it with: `self.opt.as_ref()?;`
6464

6565
error: this block may be rewritten with the `?` operator
66-
--> $DIR/question_mark.rs:94:9
66+
--> $DIR/question_mark.rs:97:9
6767
|
6868
LL | / if self.opt.is_none() {
6969
LL | | return None;
7070
LL | | }
7171
| |_________^ help: replace it with: `self.opt.as_ref()?;`
7272

7373
error: this if-let-else may be rewritten with the `?` operator
74-
--> $DIR/question_mark.rs:101:26
74+
--> $DIR/question_mark.rs:104:26
7575
|
7676
LL | let v: &Vec<_> = if let Some(ref v) = self.opt {
7777
| __________________________^
@@ -82,7 +82,7 @@ LL | | };
8282
| |_________^ help: replace it with: `self.opt.as_ref()?`
8383

8484
error: this if-let-else may be rewritten with the `?` operator
85-
--> $DIR/question_mark.rs:111:17
85+
--> $DIR/question_mark.rs:114:17
8686
|
8787
LL | let v = if let Some(v) = self.opt {
8888
| _________________^
@@ -93,7 +93,7 @@ LL | | };
9393
| |_________^ help: replace it with: `self.opt?`
9494

9595
error: this block may be rewritten with the `?` operator
96-
--> $DIR/question_mark.rs:126:5
96+
--> $DIR/question_mark.rs:129:5
9797
|
9898
LL | / if f().is_none() {
9999
LL | | return None;

0 commit comments

Comments
 (0)