Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[single_match]: Fix duplicate diagnostics #12448

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 7 additions & 24 deletions clippy_lints/src/matches/single_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,15 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr:
};

let ty = cx.typeck_results().expr_ty(ex);
if *ty.kind() != ty::Bool || is_lint_allowed(cx, MATCH_BOOL, ex.hir_id) {
check_single_pattern(cx, ex, arms, expr, els);
check_opt_like(cx, ex, arms, expr, ty, els);
if (*ty.kind() != ty::Bool || is_lint_allowed(cx, MATCH_BOOL, ex.hir_id)) &&
(check_single_pattern(arms) || check_opt_like(cx, arms, ty)) {
report_single_pattern(cx, ex, arms, expr, els);
}
}
}

fn check_single_pattern(
cx: &LateContext<'_>,
ex: &Expr<'_>,
arms: &[Arm<'_>],
expr: &Expr<'_>,
els: Option<&Expr<'_>>,
) {
if is_wild(arms[1].pat) {
report_single_pattern(cx, ex, arms, expr, els);
}
fn check_single_pattern(arms: &[Arm<'_>]) -> bool {
is_wild(arms[1].pat)
}

fn report_single_pattern(
Expand Down Expand Up @@ -140,19 +132,10 @@ fn report_single_pattern(
span_lint_and_sugg(cx, lint, expr.span, msg, "try", sugg, app);
}

fn check_opt_like<'a>(
cx: &LateContext<'a>,
ex: &Expr<'_>,
arms: &[Arm<'_>],
expr: &Expr<'_>,
ty: Ty<'a>,
els: Option<&Expr<'_>>,
) {
fn check_opt_like<'a>(cx: &LateContext<'a>, arms: &[Arm<'_>], ty: Ty<'a>) -> bool {
// We don't want to lint if the second arm contains an enum which could
// have more variants in the future.
if form_exhaustive_matches(cx, ty, arms[0].pat, arms[1].pat) {
report_single_pattern(cx, ex, arms, expr, els);
}
form_exhaustive_matches(cx, ty, arms[0].pat, arms[1].pat)
}

/// Returns `true` if all of the types in the pattern are enums which we know
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/single_match.fixed
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//@compile-flags: -Zdeduplicate-diagnostics=yes

#![warn(clippy::single_match)]
#![allow(
unused,
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/single_match.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//@compile-flags: -Zdeduplicate-diagnostics=yes

#![warn(clippy::single_match)]
#![allow(
unused,
Expand Down
36 changes: 18 additions & 18 deletions tests/ui/single_match.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
--> tests/ui/single_match.rs:17:5
--> tests/ui/single_match.rs:15:5
|
LL | / match x {
LL | | Some(y) => {
Expand All @@ -19,7 +19,7 @@ LL ~ };
|

error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
--> tests/ui/single_match.rs:25:5
--> tests/ui/single_match.rs:23:5
|
LL | / match x {
LL | | // Note the missing block braces.
Expand All @@ -31,7 +31,7 @@ LL | | }
| |_____^ help: try: `if let Some(y) = x { println!("{:?}", y) }`

error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
--> tests/ui/single_match.rs:34:5
--> tests/ui/single_match.rs:32:5
|
LL | / match z {
LL | | (2..=3, 7..=9) => dummy(),
Expand All @@ -40,7 +40,7 @@ LL | | };
| |_____^ help: try: `if let (2..=3, 7..=9) = z { dummy() }`

error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
--> tests/ui/single_match.rs:63:5
--> tests/ui/single_match.rs:61:5
|
LL | / match x {
LL | | Some(y) => dummy(),
Expand All @@ -49,7 +49,7 @@ LL | | };
| |_____^ help: try: `if let Some(y) = x { dummy() }`

error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
--> tests/ui/single_match.rs:68:5
--> tests/ui/single_match.rs:66:5
|
LL | / match y {
LL | | Ok(y) => dummy(),
Expand All @@ -58,7 +58,7 @@ LL | | };
| |_____^ help: try: `if let Ok(y) = y { dummy() }`

error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
--> tests/ui/single_match.rs:75:5
--> tests/ui/single_match.rs:73:5
|
LL | / match c {
LL | | Cow::Borrowed(..) => dummy(),
Expand All @@ -67,7 +67,7 @@ LL | | };
| |_____^ help: try: `if let Cow::Borrowed(..) = c { dummy() }`

error: you seem to be trying to use `match` for an equality check. Consider using `if`
--> tests/ui/single_match.rs:96:5
--> tests/ui/single_match.rs:94:5
|
LL | / match x {
LL | | "test" => println!(),
Expand All @@ -76,7 +76,7 @@ LL | | }
| |_____^ help: try: `if x == "test" { println!() }`

error: you seem to be trying to use `match` for an equality check. Consider using `if`
--> tests/ui/single_match.rs:109:5
--> tests/ui/single_match.rs:107:5
|
LL | / match x {
LL | | Foo::A => println!(),
Expand All @@ -85,7 +85,7 @@ LL | | }
| |_____^ help: try: `if x == Foo::A { println!() }`

error: you seem to be trying to use `match` for an equality check. Consider using `if`
--> tests/ui/single_match.rs:115:5
--> tests/ui/single_match.rs:113:5
|
LL | / match x {
LL | | FOO_C => println!(),
Expand All @@ -94,7 +94,7 @@ LL | | }
| |_____^ help: try: `if x == FOO_C { println!() }`

error: you seem to be trying to use `match` for an equality check. Consider using `if`
--> tests/ui/single_match.rs:120:5
--> tests/ui/single_match.rs:118:5
|
LL | / match &&x {
LL | | Foo::A => println!(),
Expand All @@ -103,7 +103,7 @@ LL | | }
| |_____^ help: try: `if x == Foo::A { println!() }`

error: you seem to be trying to use `match` for an equality check. Consider using `if`
--> tests/ui/single_match.rs:126:5
--> tests/ui/single_match.rs:124:5
|
LL | / match &x {
LL | | Foo::A => println!(),
Expand All @@ -112,7 +112,7 @@ LL | | }
| |_____^ help: try: `if x == &Foo::A { println!() }`

error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
--> tests/ui/single_match.rs:143:5
--> tests/ui/single_match.rs:141:5
|
LL | / match x {
LL | | Bar::A => println!(),
Expand All @@ -121,7 +121,7 @@ LL | | }
| |_____^ help: try: `if let Bar::A = x { println!() }`

error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
--> tests/ui/single_match.rs:151:5
--> tests/ui/single_match.rs:149:5
|
LL | / match x {
LL | | None => println!(),
Expand All @@ -130,7 +130,7 @@ LL | | };
| |_____^ help: try: `if let None = x { println!() }`

error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
--> tests/ui/single_match.rs:173:5
--> tests/ui/single_match.rs:171:5
|
LL | / match x {
LL | | (Some(_), _) => {},
Expand All @@ -139,7 +139,7 @@ LL | | }
| |_____^ help: try: `if let (Some(_), _) = x {}`

error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
--> tests/ui/single_match.rs:179:5
--> tests/ui/single_match.rs:177:5
|
LL | / match x {
LL | | (Some(E::V), _) => todo!(),
Expand All @@ -148,7 +148,7 @@ LL | | }
| |_____^ help: try: `if let (Some(E::V), _) = x { todo!() }`

error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
--> tests/ui/single_match.rs:185:5
--> tests/ui/single_match.rs:183:5
|
LL | / match (Some(42), Some(E::V), Some(42)) {
LL | | (.., Some(E::V), _) => {},
Expand All @@ -157,7 +157,7 @@ LL | | }
| |_____^ help: try: `if let (.., Some(E::V), _) = (Some(42), Some(E::V), Some(42)) {}`

error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
--> tests/ui/single_match.rs:257:5
--> tests/ui/single_match.rs:255:5
|
LL | / match bar {
LL | | Some(v) => unsafe {
Expand All @@ -177,7 +177,7 @@ LL + } }
|

error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
--> tests/ui/single_match.rs:265:5
--> tests/ui/single_match.rs:263:5
|
LL | / match bar {
LL | | #[rustfmt::skip]
Expand Down
1 change: 0 additions & 1 deletion tests/ui/single_match_else.fixed
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//@aux-build: proc_macros.rs
//@compile-flags: -Zdeduplicate-diagnostics=yes

#![warn(clippy::single_match_else)]
#![allow(unused, clippy::needless_return, clippy::no_effect, clippy::uninlined_format_args)]
Expand Down
1 change: 0 additions & 1 deletion tests/ui/single_match_else.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//@aux-build: proc_macros.rs
//@compile-flags: -Zdeduplicate-diagnostics=yes

#![warn(clippy::single_match_else)]
#![allow(unused, clippy::needless_return, clippy::no_effect, clippy::uninlined_format_args)]
Expand Down
18 changes: 9 additions & 9 deletions tests/ui/single_match_else.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
--> tests/ui/single_match_else.rs:18:13
--> tests/ui/single_match_else.rs:17:13
|
LL | let _ = match ExprNode::Butterflies {
| _____________^
Expand All @@ -22,7 +22,7 @@ LL ~ };
|

error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
--> tests/ui/single_match_else.rs:83:5
--> tests/ui/single_match_else.rs:82:5
|
LL | / match Some(1) {
LL | | Some(a) => println!("${:?}", a),
Expand All @@ -42,7 +42,7 @@ LL + }
|

error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
--> tests/ui/single_match_else.rs:92:5
--> tests/ui/single_match_else.rs:91:5
|
LL | / match Some(1) {
LL | | Some(a) => println!("${:?}", a),
Expand All @@ -62,7 +62,7 @@ LL + }
|

error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
--> tests/ui/single_match_else.rs:102:5
--> tests/ui/single_match_else.rs:101:5
|
LL | / match Result::<i32, Infallible>::Ok(1) {
LL | | Ok(a) => println!("${:?}", a),
Expand All @@ -82,7 +82,7 @@ LL + }
|

error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
--> tests/ui/single_match_else.rs:111:5
--> tests/ui/single_match_else.rs:110:5
|
LL | / match Cow::from("moo") {
LL | | Cow::Owned(a) => println!("${:?}", a),
Expand All @@ -102,7 +102,7 @@ LL + }
|

error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
--> tests/ui/single_match_else.rs:121:5
--> tests/ui/single_match_else.rs:120:5
|
LL | / match bar {
LL | | Some(v) => unsafe {
Expand All @@ -125,7 +125,7 @@ LL + }
|

error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
--> tests/ui/single_match_else.rs:132:5
--> tests/ui/single_match_else.rs:131:5
|
LL | / match bar {
LL | | Some(v) => {
Expand All @@ -149,7 +149,7 @@ LL + } }
|

error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
--> tests/ui/single_match_else.rs:144:5
--> tests/ui/single_match_else.rs:143:5
|
LL | / match bar {
LL | | Some(v) => unsafe {
Expand All @@ -173,7 +173,7 @@ LL + } }
|

error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
--> tests/ui/single_match_else.rs:156:5
--> tests/ui/single_match_else.rs:155:5
|
LL | / match bar {
LL | | #[rustfmt::skip]
Expand Down
Loading