Skip to content

improve [match_same_arms] messages, enable rustfix test #12794

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

Merged
merged 1 commit into from
May 14, 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
37 changes: 26 additions & 11 deletions clippy_lints/src/matches/match_same_arms.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clippy_utils::diagnostics::span_lint_hir_and_then;
use clippy_utils::source::snippet;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::{is_lint_allowed, path_to_local, search_same, SpanlessEq, SpanlessHash};
use core::cmp::Ordering;
use core::{iter, slice};
Expand All @@ -9,9 +9,9 @@ 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_lint::{LateContext, LintContext};
use rustc_middle::ty;
use rustc_span::{ErrorGuaranteed, Symbol};
use rustc_span::{ErrorGuaranteed, Span, Symbol};

use super::MATCH_SAME_ARMS;

Expand Down Expand Up @@ -110,20 +110,22 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>]) {
&& check_same_body()
};

let mut appl = Applicability::MaybeIncorrect;
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) {
if !cx.tcx.features().non_exhaustive_omitted_patterns_lint
|| is_lint_allowed(cx, NON_EXHAUSTIVE_OMITTED_PATTERNS, arm2.hir_id)
{
let arm_span = adjusted_arm_span(cx, arm1.span);
span_lint_hir_and_then(
cx,
MATCH_SAME_ARMS,
arm1.hir_id,
arm1.span,
arm_span,
"this match arm has an identical body to the `_` wildcard arm",
|diag| {
diag.span_suggestion(arm1.span, "try removing the arm", "", Applicability::MaybeIncorrect)
diag.span_suggestion(arm_span, "try removing the arm", "", appl)
.help("or try changing either arm body")
.span_note(arm2.span, "`_` wildcard arm here");
},
Expand All @@ -144,23 +146,36 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>]) {
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>");
let move_pat_snip = snippet_with_applicability(cx, move_arm.pat.span, "<pat2>", &mut appl);
let keep_pat_snip = snippet_with_applicability(cx, keep_arm.pat.span, "<pat1>", &mut appl);

diag.span_suggestion(
keep_arm.pat.span,
"try merging the arm patterns",
"or try merging the arm patterns",
format!("{keep_pat_snip} | {move_pat_snip}"),
Applicability::MaybeIncorrect,
appl,
)
.help("or try changing either arm body")
.span_note(move_arm.span, "other arm here");
.span_suggestion(
adjusted_arm_span(cx, move_arm.span),
"and remove this obsolete arm",
"",
appl,
)
.help("try changing either arm body");
},
);
}
}
}

/// Extend arm's span to include the comma and whitespaces after it.
fn adjusted_arm_span(cx: &LateContext<'_>, span: Span) -> Span {
let source_map = cx.sess().source_map();
source_map
.span_extend_while(span, |c| c == ',' || c.is_ascii_whitespace())
.unwrap_or(span)
}

#[derive(Clone, Copy)]
enum NormalizedPat<'a> {
Wild,
Expand Down
122 changes: 65 additions & 57 deletions tests/ui/match_same_arms.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error: this match arm has an identical body to the `_` wildcard arm
--> tests/ui/match_same_arms.rs:12:9
|
LL | Abc::A => 0,
| ^^^^^^^^^^^ help: try removing the arm
| ^^^^^^^^^^^^^ help: try removing the arm
|
= help: or try changing either arm body
note: `_` wildcard arm here
Expand All @@ -17,106 +17,114 @@ error: this match arm has an identical body to another arm
--> tests/ui/match_same_arms.rs:18:9
|
LL | (1, .., 3) => 42,
| ----------^^^^^^
| |
| help: try merging the arm patterns: `(1, .., 3) | (.., 3)`
| ^^^^^^^^^^^^^^^^
|
= help: or try changing either arm body
note: other arm here
--> tests/ui/match_same_arms.rs:19:9
= help: try changing either arm body
help: or try merging the arm patterns
|
LL | (1, .., 3) | (.., 3) => 42,
| ~~~~~~~~~~~~~~~~~~~~
help: and remove this obsolete arm
|
LL - (.., 3) => 42,
|
LL | (.., 3) => 42,
| ^^^^^^^^^^^^^

error: this match arm has an identical body to another arm
--> tests/ui/match_same_arms.rs:25:9
|
LL | 51 => 1,
| --^^^^^
| |
| help: try merging the arm patterns: `51 | 42`
| ^^^^^^^
|
= help: or try changing either arm body
note: other arm here
--> tests/ui/match_same_arms.rs:24:9
= help: try changing either arm body
help: or try merging the arm patterns
|
LL | 51 | 42 => 1,
| ~~~~~~~
help: and remove this obsolete arm
|
LL - 42 => 1,
|
LL | 42 => 1,
| ^^^^^^^

error: this match arm has an identical body to another arm
--> tests/ui/match_same_arms.rs:26:9
|
LL | 41 => 2,
| --^^^^^
| |
| help: try merging the arm patterns: `41 | 52`
| ^^^^^^^
|
= help: or try changing either arm body
note: other arm here
--> tests/ui/match_same_arms.rs:27:9
= help: try changing either arm body
help: or try merging the arm patterns
|
LL | 41 | 52 => 2,
| ~~~~~~~
help: and remove this obsolete arm
|
LL - 52 => 2,
|
LL | 52 => 2,
| ^^^^^^^

error: this match arm has an identical body to another arm
--> tests/ui/match_same_arms.rs:33:9
|
LL | 2 => 2,
| -^^^^^
| |
| help: try merging the arm patterns: `2 | 1`
| ^^^^^^
|
= help: or try changing either arm body
note: other arm here
--> tests/ui/match_same_arms.rs:32:9
= help: try changing either arm body
help: or try merging the arm patterns
|
LL | 2 | 1 => 2,
| ~~~~~
help: and remove this obsolete arm
|
LL - 1 => 2,
|
LL | 1 => 2,
| ^^^^^^

error: this match arm has an identical body to another arm
--> tests/ui/match_same_arms.rs:35:9
|
LL | 3 => 2,
| -^^^^^
| |
| help: try merging the arm patterns: `3 | 1`
| ^^^^^^
|
= help: or try changing either arm body
note: other arm here
--> tests/ui/match_same_arms.rs:32:9
= help: try changing either arm body
help: or try merging the arm patterns
|
LL | 3 | 1 => 2,
| ~~~~~
help: and remove this obsolete arm
|
LL - 1 => 2,
|
LL | 1 => 2,
| ^^^^^^

error: this match arm has an identical body to another arm
--> tests/ui/match_same_arms.rs:33:9
|
LL | 2 => 2,
| -^^^^^
| |
| help: try merging the arm patterns: `2 | 3`
| ^^^^^^
|
= help: or try changing either arm body
note: other arm here
--> tests/ui/match_same_arms.rs:35:9
= help: try changing either arm body
help: or try merging the arm patterns
|
LL | 2 | 3 => 2,
| ~~~~~
help: and remove this obsolete arm
|
LL - 3 => 2,
LL +
|
LL | 3 => 2,
| ^^^^^^

error: this match arm has an identical body to another arm
--> tests/ui/match_same_arms.rs:52:17
|
LL | CommandInfo::External { name, .. } => name.to_string(),
| ----------------------------------^^^^^^^^^^^^^^^^^^^^
| |
| help: try merging the arm patterns: `CommandInfo::External { name, .. } | CommandInfo::BuiltIn { name, .. }`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: or try changing either arm body
note: other arm here
--> tests/ui/match_same_arms.rs:51:17
= help: try changing either arm body
help: or try merging the arm patterns
|
LL | CommandInfo::External { name, .. } | CommandInfo::BuiltIn { name, .. } => name.to_string(),
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
help: and remove this obsolete arm
|
LL - CommandInfo::BuiltIn { name, .. } => name.to_string(),
|
LL | CommandInfo::BuiltIn { name, .. } => name.to_string(),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 8 previous errors

Loading