Skip to content

Commit 1af9a9e

Browse files
committed
remove ref check from [needless_match] as it causes more troubles than benefits.
1 parent 8fee4f4 commit 1af9a9e

File tree

4 files changed

+49
-82
lines changed

4 files changed

+49
-82
lines changed

clippy_lints/src/matches/needless_match.rs

+9-18
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clippy_utils::ty::is_type_diagnostic_item;
55
use clippy_utils::{eq_expr_value, get_parent_expr, higher, is_else_clause, is_lang_ctor, peel_blocks_with_stmt};
66
use rustc_errors::Applicability;
77
use rustc_hir::LangItem::OptionNone;
8-
use rustc_hir::{Arm, BindingAnnotation, Expr, ExprKind, Pat, PatKind, Path, PathSegment, QPath, UnOp};
8+
use rustc_hir::{Arm, BindingAnnotation, Expr, ExprKind, Pat, PatKind, Path, PathSegment, QPath};
99
use rustc_lint::LateContext;
1010
use rustc_span::sym;
1111

@@ -129,28 +129,19 @@ fn pat_same_as_expr(pat: &Pat<'_>, expr: &Expr<'_>) -> bool {
129129
&& has_same_non_ref_symbols(tuple_params, call_params);
130130
}
131131
},
132-
// Example: `val => val`, or `ref val => *val`
133-
(PatKind::Binding(annot, _, pat_ident, _), _) => {
134-
let new_expr = if let (
135-
BindingAnnotation::Ref | BindingAnnotation::RefMut,
136-
ExprKind::Unary(UnOp::Deref, operand_expr),
137-
) = (annot, &expr.kind)
138-
{
139-
operand_expr
140-
} else {
141-
expr
142-
};
143-
144-
if let ExprKind::Path(QPath::Resolved(
132+
// Example: `val => val`
133+
(
134+
PatKind::Binding(annot, _, pat_ident, _),
135+
ExprKind::Path(QPath::Resolved(
145136
_,
146137
Path {
147138
segments: [first_seg, ..],
148139
..
149140
},
150-
)) = new_expr.kind
151-
{
152-
return pat_ident.name == first_seg.ident.name;
153-
}
141+
)),
142+
) => {
143+
return !matches!(annot, BindingAnnotation::Ref | BindingAnnotation::RefMut)
144+
&& pat_ident.name == first_seg.ident.name;
154145
},
155146
// Example: `Custom::TypeA => Custom::TypeB`, or `None => None`
156147
(PatKind::Path(QPath::Resolved(_, p_path)), ExprKind::Path(QPath::Resolved(_, e_path))) => {

tests/ui/needless_match.fixed

+12-9
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,9 @@ enum Simple {
1111
D,
1212
}
1313

14-
#[allow(unused_mut)]
1514
fn useless_match() {
16-
let mut i = 10;
15+
let i = 10;
1716
let _: i32 = i;
18-
let _: i32 = i;
19-
let mut _i_mut = i;
20-
2117
let s = "test";
2218
let _: &str = s;
2319
}
@@ -56,17 +52,24 @@ fn func_ret_err<T>(err: T) -> Result<i32, T> {
5652
fn result_match() {
5753
let _: Result<i32, i32> = Ok(1);
5854
let _: Result<i32, i32> = func_ret_err(0_i32);
55+
// as ref, don't trigger
56+
let res = &func_ret_err(0_i32);
57+
let _: Result<&i32, &i32> = match *res {
58+
Ok(ref x) => Ok(x),
59+
Err(ref x) => Err(x),
60+
};
5961
}
6062

6163
fn if_let_option() -> Option<i32> {
6264
Some(1)
6365
}
6466

65-
fn if_let_result(x: Result<(), i32>) {
66-
let _: Result<(), i32> = x;
67-
let _: Result<(), i32> = x;
67+
fn if_let_result() {
68+
let x: Result<i32, i32> = Ok(1);
69+
let _: Result<i32, i32> = x;
70+
let _: Result<i32, i32> = x;
6871
// Input type mismatch, don't trigger
69-
let _: Result<(), i32> = if let Err(e) = Ok(1) { Err(e) } else { x };
72+
let _: Result<i32, i32> = if let Err(e) = Ok(1) { Err(e) } else { x };
7073
}
7174

7275
fn if_let_custom_enum(x: Simple) {

tests/ui/needless_match.rs

+12-17
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,14 @@ enum Simple {
1111
D,
1212
}
1313

14-
#[allow(unused_mut)]
1514
fn useless_match() {
16-
let mut i = 10;
15+
let i = 10;
1716
let _: i32 = match i {
1817
0 => 0,
1918
1 => 1,
2019
2 => 2,
2120
_ => i,
2221
};
23-
let _: i32 = match i {
24-
0 => 0,
25-
1 => 1,
26-
ref i => *i,
27-
};
28-
let mut _i_mut = match i {
29-
0 => 0,
30-
1 => 1,
31-
ref mut i => *i,
32-
};
33-
3422
let s = "test";
3523
let _: &str = match s {
3624
"a" => "a",
@@ -87,17 +75,24 @@ fn result_match() {
8775
Err(err) => Err(err),
8876
Ok(a) => Ok(a),
8977
};
78+
// as ref, don't trigger
79+
let res = &func_ret_err(0_i32);
80+
let _: Result<&i32, &i32> = match *res {
81+
Ok(ref x) => Ok(x),
82+
Err(ref x) => Err(x),
83+
};
9084
}
9185

9286
fn if_let_option() -> Option<i32> {
9387
if let Some(a) = Some(1) { Some(a) } else { None }
9488
}
9589

96-
fn if_let_result(x: Result<(), i32>) {
97-
let _: Result<(), i32> = if let Err(e) = x { Err(e) } else { x };
98-
let _: Result<(), i32> = if let Ok(val) = x { Ok(val) } else { x };
90+
fn if_let_result() {
91+
let x: Result<i32, i32> = Ok(1);
92+
let _: Result<i32, i32> = if let Err(e) = x { Err(e) } else { x };
93+
let _: Result<i32, i32> = if let Ok(val) = x { Ok(val) } else { x };
9994
// Input type mismatch, don't trigger
100-
let _: Result<(), i32> = if let Err(e) = Ok(1) { Err(e) } else { x };
95+
let _: Result<i32, i32> = if let Err(e) = Ok(1) { Err(e) } else { x };
10196
}
10297

10398
fn if_let_custom_enum(x: Simple) {

tests/ui/needless_match.stderr

+16-38
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this match expression is unnecessary
2-
--> $DIR/needless_match.rs:17:18
2+
--> $DIR/needless_match.rs:16:18
33
|
44
LL | let _: i32 = match i {
55
| __________________^
@@ -13,29 +13,7 @@ LL | | };
1313
= note: `-D clippy::needless-match` implied by `-D warnings`
1414

1515
error: this match expression is unnecessary
16-
--> $DIR/needless_match.rs:23:18
17-
|
18-
LL | let _: i32 = match i {
19-
| __________________^
20-
LL | | 0 => 0,
21-
LL | | 1 => 1,
22-
LL | | ref i => *i,
23-
LL | | };
24-
| |_____^ help: replace it with: `i`
25-
26-
error: this match expression is unnecessary
27-
--> $DIR/needless_match.rs:28:22
28-
|
29-
LL | let mut _i_mut = match i {
30-
| ______________________^
31-
LL | | 0 => 0,
32-
LL | | 1 => 1,
33-
LL | | ref mut i => *i,
34-
LL | | };
35-
| |_____^ help: replace it with: `i`
36-
37-
error: this match expression is unnecessary
38-
--> $DIR/needless_match.rs:35:19
16+
--> $DIR/needless_match.rs:23:19
3917
|
4018
LL | let _: &str = match s {
4119
| ___________________^
@@ -46,7 +24,7 @@ LL | | };
4624
| |_____^ help: replace it with: `s`
4725

4826
error: this match expression is unnecessary
49-
--> $DIR/needless_match.rs:44:21
27+
--> $DIR/needless_match.rs:32:21
5028
|
5129
LL | let _: Simple = match se {
5230
| _____________________^
@@ -58,7 +36,7 @@ LL | | };
5836
| |_____^ help: replace it with: `se`
5937

6038
error: this match expression is unnecessary
61-
--> $DIR/needless_match.rs:66:26
39+
--> $DIR/needless_match.rs:54:26
6240
|
6341
LL | let _: Option<i32> = match x {
6442
| __________________________^
@@ -68,7 +46,7 @@ LL | | };
6846
| |_____^ help: replace it with: `x`
6947

7048
error: this match expression is unnecessary
71-
--> $DIR/needless_match.rs:82:31
49+
--> $DIR/needless_match.rs:70:31
7250
|
7351
LL | let _: Result<i32, i32> = match Ok(1) {
7452
| _______________________________^
@@ -78,7 +56,7 @@ LL | | };
7856
| |_____^ help: replace it with: `Ok(1)`
7957

8058
error: this match expression is unnecessary
81-
--> $DIR/needless_match.rs:86:31
59+
--> $DIR/needless_match.rs:74:31
8260
|
8361
LL | let _: Result<i32, i32> = match func_ret_err(0_i32) {
8462
| _______________________________^
@@ -88,25 +66,25 @@ LL | | };
8866
| |_____^ help: replace it with: `func_ret_err(0_i32)`
8967

9068
error: this if-let expression is unnecessary
91-
--> $DIR/needless_match.rs:93:5
69+
--> $DIR/needless_match.rs:87:5
9270
|
9371
LL | if let Some(a) = Some(1) { Some(a) } else { None }
9472
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `Some(1)`
9573

9674
error: this if-let expression is unnecessary
97-
--> $DIR/needless_match.rs:97:30
75+
--> $DIR/needless_match.rs:92:31
9876
|
99-
LL | let _: Result<(), i32> = if let Err(e) = x { Err(e) } else { x };
100-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `x`
77+
LL | let _: Result<i32, i32> = if let Err(e) = x { Err(e) } else { x };
78+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `x`
10179

10280
error: this if-let expression is unnecessary
103-
--> $DIR/needless_match.rs:98:30
81+
--> $DIR/needless_match.rs:93:31
10482
|
105-
LL | let _: Result<(), i32> = if let Ok(val) = x { Ok(val) } else { x };
106-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `x`
83+
LL | let _: Result<i32, i32> = if let Ok(val) = x { Ok(val) } else { x };
84+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `x`
10785

10886
error: this if-let expression is unnecessary
109-
--> $DIR/needless_match.rs:104:21
87+
--> $DIR/needless_match.rs:99:21
11088
|
11189
LL | let _: Simple = if let Simple::A = x {
11290
| _____________________^
@@ -119,7 +97,7 @@ LL | | };
11997
| |_____^ help: replace it with: `x`
12098

12199
error: this match expression is unnecessary
122-
--> $DIR/needless_match.rs:143:26
100+
--> $DIR/needless_match.rs:138:26
123101
|
124102
LL | let _: Complex = match ce {
125103
| __________________________^
@@ -131,5 +109,5 @@ LL | | Complex::D(E::VariantB(ea, eb), b) => Complex::D(E::VariantB(
131109
LL | | };
132110
| |_________^ help: replace it with: `ce`
133111

134-
error: aborting due to 13 previous errors
112+
error: aborting due to 11 previous errors
135113

0 commit comments

Comments
 (0)