about summary refs log tree commit diff
diff options
context:
space:
mode:
authorxFrednet <xFrednet@gmail.com>2022-06-06 12:36:57 +0200
committerxFrednet <xFrednet@gmail.com>2022-06-06 12:36:57 +0200
commit7e1730e16c779f688392e0edcd553036b2afe1f1 (patch)
tree709385689e806686ba93d11de7dc42ac214b3b3c
parenta613460e8ade73dc387c19b0a45588681de46fef (diff)
downloadrust-7e1730e16c779f688392e0edcd553036b2afe1f1.tar.gz
rust-7e1730e16c779f688392e0edcd553036b2afe1f1.zip
Fix `#[expect]` for `same_name_method`
-rw-r--r--clippy_lints/src/same_name_method.rs17
-rw-r--r--tests/ui/same_name_method.rs16
-rw-r--r--tests/ui/same_name_method.stderr20
3 files changed, 36 insertions, 17 deletions
diff --git a/clippy_lints/src/same_name_method.rs b/clippy_lints/src/same_name_method.rs
index 9158cbcc04e..04d9ee7d912 100644
--- a/clippy_lints/src/same_name_method.rs
+++ b/clippy_lints/src/same_name_method.rs
@@ -1,7 +1,7 @@
-use clippy_utils::diagnostics::span_lint_and_then;
+use clippy_utils::diagnostics::span_lint_hir_and_then;
 use rustc_data_structures::fx::FxHashMap;
 use rustc_hir::def::{DefKind, Res};
-use rustc_hir::{Impl, ItemKind, Node, Path, QPath, TraitRef, TyKind};
+use rustc_hir::{HirId, Impl, ItemKind, Node, Path, QPath, TraitRef, TyKind};
 use rustc_lint::{LateContext, LateLintPass};
 use rustc_middle::ty::AssocKind;
 use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -42,7 +42,7 @@ declare_clippy_lint! {
 declare_lint_pass!(SameNameMethod => [SAME_NAME_METHOD]);
 
 struct ExistingName {
-    impl_methods: BTreeMap<Symbol, Span>,
+    impl_methods: BTreeMap<Symbol, (Span, HirId)>,
     trait_methods: BTreeMap<Symbol, Vec<Span>>,
 }
 
@@ -97,10 +97,11 @@ impl<'tcx> LateLintPass<'tcx> for SameNameMethod {
                         };
 
                         let mut check_trait_method = |method_name: Symbol, trait_method_span: Span| {
-                            if let Some(impl_span) = existing_name.impl_methods.get(&method_name) {
-                                span_lint_and_then(
+                            if let Some((impl_span, hir_id)) = existing_name.impl_methods.get(&method_name) {
+                                span_lint_hir_and_then(
                                     cx,
                                     SAME_NAME_METHOD,
+                                    *hir_id,
                                     *impl_span,
                                     "method's name is the same as an existing method in a trait",
                                     |diag| {
@@ -136,10 +137,12 @@ impl<'tcx> LateLintPass<'tcx> for SameNameMethod {
                         }) {
                             let method_name = impl_item_ref.ident.name;
                             let impl_span = impl_item_ref.span;
+                            let hir_id = impl_item_ref.id.hir_id();
                             if let Some(trait_spans) = existing_name.trait_methods.get(&method_name) {
-                                span_lint_and_then(
+                                span_lint_hir_and_then(
                                     cx,
                                     SAME_NAME_METHOD,
+                                    hir_id,
                                     impl_span,
                                     "method's name is the same as an existing method in a trait",
                                     |diag| {
@@ -152,7 +155,7 @@ impl<'tcx> LateLintPass<'tcx> for SameNameMethod {
                                     },
                                 );
                             }
-                            existing_name.impl_methods.insert(method_name, impl_span);
+                            existing_name.impl_methods.insert(method_name, (impl_span, hir_id));
                         }
                     },
                 }
diff --git a/tests/ui/same_name_method.rs b/tests/ui/same_name_method.rs
index 12e10ba6c49..9562b47f0c4 100644
--- a/tests/ui/same_name_method.rs
+++ b/tests/ui/same_name_method.rs
@@ -1,3 +1,4 @@
+#![feature(lint_reasons)]
 #![warn(clippy::same_name_method)]
 #![allow(dead_code, non_camel_case_types)]
 
@@ -108,4 +109,19 @@ mod should_not_lint {
     }
 }
 
+mod check_expect_suppression {
+    use crate::T1;
+
+    struct S;
+
+    impl S {
+        #[expect(clippy::same_name_method)]
+        fn foo() {}
+    }
+
+    impl T1 for S {
+        fn foo() {}
+    }
+}
+
 fn main() {}
diff --git a/tests/ui/same_name_method.stderr b/tests/ui/same_name_method.stderr
index cf06eb32e0c..f55ec9f3cc6 100644
--- a/tests/ui/same_name_method.stderr
+++ b/tests/ui/same_name_method.stderr
@@ -1,61 +1,61 @@
 error: method's name is the same as an existing method in a trait
-  --> $DIR/same_name_method.rs:20:13
+  --> $DIR/same_name_method.rs:21:13
    |
 LL |             fn foo() {}
    |             ^^^^^^^^^^^
    |
    = note: `-D clippy::same-name-method` implied by `-D warnings`
 note: existing `foo` defined here
-  --> $DIR/same_name_method.rs:24:13
+  --> $DIR/same_name_method.rs:25:13
    |
 LL |             fn foo() {}
    |             ^^^^^^^^^^^
 
 error: method's name is the same as an existing method in a trait
-  --> $DIR/same_name_method.rs:34:13
+  --> $DIR/same_name_method.rs:35:13
    |
 LL |             fn clone() {}
    |             ^^^^^^^^^^^^^
    |
 note: existing `clone` defined here
-  --> $DIR/same_name_method.rs:30:18
+  --> $DIR/same_name_method.rs:31:18
    |
 LL |         #[derive(Clone)]
    |                  ^^^^^
    = note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)
 
 error: method's name is the same as an existing method in a trait
-  --> $DIR/same_name_method.rs:44:13
+  --> $DIR/same_name_method.rs:45:13
    |
 LL |             fn foo() {}
    |             ^^^^^^^^^^^
    |
 note: existing `foo` defined here
-  --> $DIR/same_name_method.rs:48:13
+  --> $DIR/same_name_method.rs:49:13
    |
 LL |             fn foo() {}
    |             ^^^^^^^^^^^
 
 error: method's name is the same as an existing method in a trait
-  --> $DIR/same_name_method.rs:58:13
+  --> $DIR/same_name_method.rs:59:13
    |
 LL |             fn foo() {}
    |             ^^^^^^^^^^^
    |
 note: existing `foo` defined here
-  --> $DIR/same_name_method.rs:61:9
+  --> $DIR/same_name_method.rs:62:9
    |
 LL |         impl T1 for S {}
    |         ^^^^^^^^^^^^^^^^
 
 error: method's name is the same as an existing method in a trait
-  --> $DIR/same_name_method.rs:70:13
+  --> $DIR/same_name_method.rs:71:13
    |
 LL |             fn foo() {}
    |             ^^^^^^^^^^^
    |
 note: existing `foo` defined here
-  --> $DIR/same_name_method.rs:73:9
+  --> $DIR/same_name_method.rs:74:9
    |
 LL |         impl T1 for S {}
    |         ^^^^^^^^^^^^^^^^