From 8cd122d286b474ffe29d48e6c2c1203680749f65 Mon Sep 17 00:00:00 2001 From: Centri3 <114838443+Centri3@users.noreply.github.com> Date: Tue, 13 Jun 2023 13:08:03 -0500 Subject: [PATCH] [`match_same_arms`]: don't lint if `non_exhaustive_omitted_patterns` formatting :/ --- clippy_lints/src/matches/match_same_arms.rs | 77 +++++++++++---------- tests/ui/match_same_arms.rs | 30 ++++++++ tests/ui/match_same_arms.stderr | 32 ++++----- 3 files changed, 86 insertions(+), 53 deletions(-) diff --git a/clippy_lints/src/matches/match_same_arms.rs b/clippy_lints/src/matches/match_same_arms.rs index ae8262ace968..776f24a99a2c 100644 --- a/clippy_lints/src/matches/match_same_arms.rs +++ b/clippy_lints/src/matches/match_same_arms.rs @@ -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; @@ -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; @@ -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, ""); - let keep_pat_snip = snippet(cx, keep_arm.pat.span, ""); + 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, ""); + let keep_pat_snip = snippet(cx, keep_arm.pat.span, ""); - 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"); + }, + ); + } } } } diff --git a/tests/ui/match_same_arms.rs b/tests/ui/match_same_arms.rs index 3914b45464c7..2d85e6b2b2d2 100644 --- a/tests/ui/match_same_arms.rs +++ b/tests/ui/match_same_arms.rs @@ -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, diff --git a/tests/ui/match_same_arms.stderr b/tests/ui/match_same_arms.stderr index db85b5964e84..ce019275a699 100644 --- a/tests/ui/match_same_arms.stderr +++ b/tests/ui/match_same_arms.stderr @@ -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, | ----------^^^^^^ @@ -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 | --^^^^^ @@ -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, | --^^^^^ @@ -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 | -^^^^^ @@ -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 | -^^^^^ @@ -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 | -^^^^^ @@ -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(), | ----------------------------------^^^^^^^^^^^^^^^^^^^^ @@ -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(), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^