Skip to content

Commit 05c2d72

Browse files
committed
fix: match_ref_pats FP on match scrutinee of never type
1 parent 306d4e3 commit 05c2d72

File tree

4 files changed

+80
-7
lines changed

4 files changed

+80
-7
lines changed

clippy_lints/src/matches/match_ref_pats.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ where
1717
return;
1818
}
1919

20+
// `!` cannot be deref-ed
21+
if cx.typeck_results().expr_ty(scrutinee).is_never() {
22+
return;
23+
}
24+
2025
let (first_sugg, msg, title);
2126
let ctxt = expr.span.ctxt();
2227
let mut app = Applicability::Unspecified;

tests/ui/match_ref_pats.fixed

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
#![warn(clippy::match_ref_pats)]
22
#![allow(dead_code, unused_variables)]
3-
#![allow(clippy::enum_variant_names, clippy::equatable_if_let, clippy::uninlined_format_args)]
3+
#![allow(
4+
clippy::enum_variant_names,
5+
clippy::equatable_if_let,
6+
clippy::uninlined_format_args,
7+
clippy::empty_loop,
8+
clippy::diverging_sub_expression
9+
)]
410

511
fn ref_pats() {
612
{
@@ -120,4 +126,32 @@ mod issue_7740 {
120126
}
121127
}
122128

129+
mod issue15378 {
130+
fn never_in_match() {
131+
match unimplemented!() {
132+
&_ => {},
133+
&&&42 => {
134+
todo!()
135+
},
136+
_ => {},
137+
}
138+
139+
match panic!() {
140+
&_ => {},
141+
&&&42 => {
142+
todo!()
143+
},
144+
_ => {},
145+
}
146+
147+
match loop {} {
148+
&_ => {},
149+
&&&42 => {
150+
todo!()
151+
},
152+
_ => {},
153+
}
154+
}
155+
}
156+
123157
fn main() {}

tests/ui/match_ref_pats.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
#![warn(clippy::match_ref_pats)]
22
#![allow(dead_code, unused_variables)]
3-
#![allow(clippy::enum_variant_names, clippy::equatable_if_let, clippy::uninlined_format_args)]
3+
#![allow(
4+
clippy::enum_variant_names,
5+
clippy::equatable_if_let,
6+
clippy::uninlined_format_args,
7+
clippy::empty_loop,
8+
clippy::diverging_sub_expression
9+
)]
410

511
fn ref_pats() {
612
{
@@ -120,4 +126,32 @@ mod issue_7740 {
120126
}
121127
}
122128

129+
mod issue15378 {
130+
fn never_in_match() {
131+
match unimplemented!() {
132+
&_ => {},
133+
&&&42 => {
134+
todo!()
135+
},
136+
_ => {},
137+
}
138+
139+
match panic!() {
140+
&_ => {},
141+
&&&42 => {
142+
todo!()
143+
},
144+
_ => {},
145+
}
146+
147+
match loop {} {
148+
&_ => {},
149+
&&&42 => {
150+
todo!()
151+
},
152+
_ => {},
153+
}
154+
}
155+
}
156+
123157
fn main() {}

tests/ui/match_ref_pats.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: you don't need to add `&` to all patterns
2-
--> tests/ui/match_ref_pats.rs:8:9
2+
--> tests/ui/match_ref_pats.rs:14:9
33
|
44
LL | / match v {
55
LL | |
@@ -19,7 +19,7 @@ LL ~ None => println!("none"),
1919
|
2020

2121
error: you don't need to add `&` to both the expression and the patterns
22-
--> tests/ui/match_ref_pats.rs:26:5
22+
--> tests/ui/match_ref_pats.rs:32:5
2323
|
2424
LL | / match &w {
2525
LL | |
@@ -37,7 +37,7 @@ LL ~ None => println!("none"),
3737
|
3838

3939
error: redundant pattern matching, consider using `is_none()`
40-
--> tests/ui/match_ref_pats.rs:39:12
40+
--> tests/ui/match_ref_pats.rs:45:12
4141
|
4242
LL | if let &None = a {
4343
| -------^^^^^---- help: try: `if a.is_none()`
@@ -46,13 +46,13 @@ LL | if let &None = a {
4646
= help: to override `-D warnings` add `#[allow(clippy::redundant_pattern_matching)]`
4747

4848
error: redundant pattern matching, consider using `is_none()`
49-
--> tests/ui/match_ref_pats.rs:45:12
49+
--> tests/ui/match_ref_pats.rs:51:12
5050
|
5151
LL | if let &None = &b {
5252
| -------^^^^^----- help: try: `if b.is_none()`
5353

5454
error: you don't need to add `&` to all patterns
55-
--> tests/ui/match_ref_pats.rs:106:9
55+
--> tests/ui/match_ref_pats.rs:112:9
5656
|
5757
LL | / match foobar_variant!(0) {
5858
LL | |

0 commit comments

Comments
 (0)