Skip to content

Commit

Permalink
[match_same_arms]: don't lint if non_exhaustive_omitted_patterns
Browse files Browse the repository at this point in the history
formatting :/
  • Loading branch information
Centri3 committed Jun 13, 2023
1 parent 72332b2 commit 8cd122d
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 53 deletions.
77 changes: 40 additions & 37 deletions clippy_lints/src/matches/match_same_arms.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::snippet;
use clippy_utils::{path_to_local, search_same, SpanlessEq, SpanlessHash};
use clippy_utils::{is_lint_allowed, path_to_local, search_same, SpanlessEq, SpanlessHash};
use core::cmp::Ordering;
use core::iter;
use core::slice;
Expand All @@ -9,6 +9,7 @@ use rustc_ast::ast::LitKind;
use rustc_errors::Applicability;
use rustc_hir::def_id::DefId;
use rustc_hir::{Arm, Expr, ExprKind, HirId, HirIdMap, HirIdMapEntry, HirIdSet, Pat, PatKind, RangeEnd};
use rustc_lint::builtin::NON_EXHAUSTIVE_OMITTED_PATTERNS;
use rustc_lint::LateContext;
use rustc_middle::ty;
use rustc_span::Symbol;
Expand Down Expand Up @@ -102,45 +103,47 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>]) {

let indexed_arms: Vec<(usize, &Arm<'_>)> = arms.iter().enumerate().collect();
for (&(i, arm1), &(j, arm2)) in search_same(&indexed_arms, hash, eq) {
if matches!(arm2.pat.kind, PatKind::Wild) {
span_lint_and_then(
cx,
MATCH_SAME_ARMS,
arm1.span,
"this match arm has an identical body to the `_` wildcard arm",
|diag| {
diag.span_suggestion(arm1.span, "try removing the arm", "", Applicability::MaybeIncorrect)
.help("or try changing either arm body")
.span_note(arm2.span, "`_` wildcard arm here");
},
);
} else {
let back_block = backwards_blocking_idxs[j];
let (keep_arm, move_arm) = if back_block < i || (back_block == 0 && forwards_blocking_idxs[i] <= j) {
(arm1, arm2)
if is_lint_allowed(cx, NON_EXHAUSTIVE_OMITTED_PATTERNS, arm2.hir_id) {
if matches!(arm2.pat.kind, PatKind::Wild) {
span_lint_and_then(
cx,
MATCH_SAME_ARMS,
arm1.span,
"this match arm has an identical body to the `_` wildcard arm",
|diag| {
diag.span_suggestion(arm1.span, "try removing the arm", "", Applicability::MaybeIncorrect)
.help("or try changing either arm body")
.span_note(arm2.span, "`_` wildcard arm here");
},
);
} else {
(arm2, arm1)
};
let back_block = backwards_blocking_idxs[j];
let (keep_arm, move_arm) = if back_block < i || (back_block == 0 && forwards_blocking_idxs[i] <= j) {
(arm1, arm2)
} else {
(arm2, arm1)
};

span_lint_and_then(
cx,
MATCH_SAME_ARMS,
keep_arm.span,
"this match arm has an identical body to another arm",
|diag| {
let move_pat_snip = snippet(cx, move_arm.pat.span, "<pat2>");
let keep_pat_snip = snippet(cx, keep_arm.pat.span, "<pat1>");
span_lint_and_then(
cx,
MATCH_SAME_ARMS,
keep_arm.span,
"this match arm has an identical body to another arm",
|diag| {
let move_pat_snip = snippet(cx, move_arm.pat.span, "<pat2>");
let keep_pat_snip = snippet(cx, keep_arm.pat.span, "<pat1>");

diag.span_suggestion(
keep_arm.pat.span,
"try merging the arm patterns",
format!("{keep_pat_snip} | {move_pat_snip}"),
Applicability::MaybeIncorrect,
)
.help("or try changing either arm body")
.span_note(move_arm.span, "other arm here");
},
);
diag.span_suggestion(
keep_arm.pat.span,
"try merging the arm patterns",
format!("{keep_pat_snip} | {move_pat_snip}"),
Applicability::MaybeIncorrect,
)
.help("or try changing either arm body")
.span_note(move_arm.span, "other arm here");
},
);
}
}
}
}
Expand Down
30 changes: 30 additions & 0 deletions tests/ui/match_same_arms.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
#![feature(non_exhaustive_omitted_patterns_lint)]
#![warn(clippy::match_same_arms)]

use std::sync::atomic::Ordering; // #[non_exhaustive] enum

pub fn f(x: Ordering) {
match x {
Ordering::Relaxed => println!("relaxed"),
Ordering::Release => println!("release"),
Ordering::Acquire => println!("acquire"),
Ordering::AcqRel | Ordering::SeqCst => panic!(),
#[deny(non_exhaustive_omitted_patterns)]
_ => panic!(),
}
}

mod f {
#![deny(non_exhaustive_omitted_patterns)]

use super::*;

pub fn f(x: Ordering) {
match x {
Ordering::Relaxed => println!("relaxed"),
Ordering::Release => println!("release"),
Ordering::Acquire => println!("acquire"),
Ordering::AcqRel | Ordering::SeqCst => panic!(),
_ => panic!(),
}
}
}

pub enum Abc {
A,
B,
Expand Down
32 changes: 16 additions & 16 deletions tests/ui/match_same_arms.stderr
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
error: this match arm has an identical body to the `_` wildcard arm
--> $DIR/match_same_arms.rs:11:9
--> $DIR/match_same_arms.rs:41:9
|
LL | Abc::A => 0,
| ^^^^^^^^^^^ help: try removing the arm
|
= help: or try changing either arm body
note: `_` wildcard arm here
--> $DIR/match_same_arms.rs:13:9
--> $DIR/match_same_arms.rs:43:9
|
LL | _ => 0, //~ ERROR match arms have same body
| ^^^^^^
= note: `-D clippy::match-same-arms` implied by `-D warnings`

error: this match arm has an identical body to another arm
--> $DIR/match_same_arms.rs:17:9
--> $DIR/match_same_arms.rs:47:9
|
LL | (1, .., 3) => 42,
| ----------^^^^^^
Expand All @@ -22,13 +22,13 @@ LL | (1, .., 3) => 42,
|
= help: or try changing either arm body
note: other arm here
--> $DIR/match_same_arms.rs:18:9
--> $DIR/match_same_arms.rs:48:9
|
LL | (.., 3) => 42, //~ ERROR match arms have same body
| ^^^^^^^^^^^^^

error: this match arm has an identical body to another arm
--> $DIR/match_same_arms.rs:24:9
--> $DIR/match_same_arms.rs:54:9
|
LL | 51 => 1, //~ ERROR match arms have same body
| --^^^^^
Expand All @@ -37,13 +37,13 @@ LL | 51 => 1, //~ ERROR match arms have same body
|
= help: or try changing either arm body
note: other arm here
--> $DIR/match_same_arms.rs:23:9
--> $DIR/match_same_arms.rs:53:9
|
LL | 42 => 1,
| ^^^^^^^

error: this match arm has an identical body to another arm
--> $DIR/match_same_arms.rs:25:9
--> $DIR/match_same_arms.rs:55:9
|
LL | 41 => 2,
| --^^^^^
Expand All @@ -52,13 +52,13 @@ LL | 41 => 2,
|
= help: or try changing either arm body
note: other arm here
--> $DIR/match_same_arms.rs:26:9
--> $DIR/match_same_arms.rs:56:9
|
LL | 52 => 2, //~ ERROR match arms have same body
| ^^^^^^^

error: this match arm has an identical body to another arm
--> $DIR/match_same_arms.rs:32:9
--> $DIR/match_same_arms.rs:62:9
|
LL | 2 => 2, //~ ERROR 2nd matched arms have same body
| -^^^^^
Expand All @@ -67,13 +67,13 @@ LL | 2 => 2, //~ ERROR 2nd matched arms have same body
|
= help: or try changing either arm body
note: other arm here
--> $DIR/match_same_arms.rs:31:9
--> $DIR/match_same_arms.rs:61:9
|
LL | 1 => 2,
| ^^^^^^

error: this match arm has an identical body to another arm
--> $DIR/match_same_arms.rs:33:9
--> $DIR/match_same_arms.rs:63:9
|
LL | 3 => 2, //~ ERROR 3rd matched arms have same body
| -^^^^^
Expand All @@ -82,13 +82,13 @@ LL | 3 => 2, //~ ERROR 3rd matched arms have same body
|
= help: or try changing either arm body
note: other arm here
--> $DIR/match_same_arms.rs:31:9
--> $DIR/match_same_arms.rs:61:9
|
LL | 1 => 2,
| ^^^^^^

error: this match arm has an identical body to another arm
--> $DIR/match_same_arms.rs:32:9
--> $DIR/match_same_arms.rs:62:9
|
LL | 2 => 2, //~ ERROR 2nd matched arms have same body
| -^^^^^
Expand All @@ -97,13 +97,13 @@ LL | 2 => 2, //~ ERROR 2nd matched arms have same body
|
= help: or try changing either arm body
note: other arm here
--> $DIR/match_same_arms.rs:33:9
--> $DIR/match_same_arms.rs:63:9
|
LL | 3 => 2, //~ ERROR 3rd matched arms have same body
| ^^^^^^

error: this match arm has an identical body to another arm
--> $DIR/match_same_arms.rs:50:17
--> $DIR/match_same_arms.rs:80:17
|
LL | CommandInfo::External { name, .. } => name.to_string(),
| ----------------------------------^^^^^^^^^^^^^^^^^^^^
Expand All @@ -112,7 +112,7 @@ LL | CommandInfo::External { name, .. } => name.to_string(),
|
= help: or try changing either arm body
note: other arm here
--> $DIR/match_same_arms.rs:49:17
--> $DIR/match_same_arms.rs:79:17
|
LL | CommandInfo::BuiltIn { name, .. } => name.to_string(),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down

0 comments on commit 8cd122d

Please sign in to comment.