Skip to content

Commit ddc2598

Browse files
committed
Auto merge of #7093 - Jarcho:single_match_fp, r=llogiq
Fix `single_match` fixes: #7038 changelog: Don't suggest an equality check for types which don't implement `PartialEq` in `single_match`
2 parents faa9756 + 9a55c0c commit ddc2598

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

clippy_lints/src/matches.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -738,8 +738,11 @@ fn report_single_match_single_pattern(
738738
let (msg, sugg) = if_chain! {
739739
if let PatKind::Path(_) | PatKind::Lit(_) = pat.kind;
740740
let (ty, ty_ref_count) = peel_mid_ty_refs(cx.typeck_results().expr_ty(ex));
741-
if let Some(trait_id) = cx.tcx.lang_items().structural_peq_trait();
742-
if ty.is_integral() || ty.is_char() || ty.is_str() || implements_trait(cx, ty, trait_id, &[]);
741+
if let Some(spe_trait_id) = cx.tcx.lang_items().structural_peq_trait();
742+
if let Some(pe_trait_id) = cx.tcx.lang_items().eq_trait();
743+
if ty.is_integral() || ty.is_char() || ty.is_str()
744+
|| (implements_trait(cx, ty, spe_trait_id, &[])
745+
&& implements_trait(cx, ty, pe_trait_id, &[ty.into()]));
743746
then {
744747
// scrutinee derives PartialEq and the pattern is a constant.
745748
let pat_ref_count = match pat.kind {

tests/ui/single_match.rs

+8
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ fn if_suggestion() {
135135
Bar::A => println!(),
136136
_ => (),
137137
}
138+
139+
// issue #7038
140+
struct X;
141+
let x = Some(X);
142+
match x {
143+
None => println!(),
144+
_ => (),
145+
};
138146
}
139147

140148
macro_rules! single_match {

tests/ui/single_match.stderr

+10-1
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,14 @@ LL | | _ => (),
119119
LL | | }
120120
| |_____^ help: try this: `if let Bar::A = x { println!() }`
121121

122-
error: aborting due to 12 previous errors
122+
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
123+
--> $DIR/single_match.rs:142:5
124+
|
125+
LL | / match x {
126+
LL | | None => println!(),
127+
LL | | _ => (),
128+
LL | | };
129+
| |_____^ help: try this: `if let None = x { println!() }`
130+
131+
error: aborting due to 13 previous errors
123132

0 commit comments

Comments
 (0)