Fix #[expect] for same_name_method

This commit is contained in:
xFrednet 2022-06-06 12:36:57 +02:00
parent a613460e8a
commit 7e1730e16c
No known key found for this signature in database
GPG key ID: F5C59D0E669E5302
3 changed files with 36 additions and 17 deletions

View file

@ -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_data_structures::fx::FxHashMap;
use rustc_hir::def::{DefKind, Res}; 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_lint::{LateContext, LateLintPass};
use rustc_middle::ty::AssocKind; use rustc_middle::ty::AssocKind;
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
@ -42,7 +42,7 @@ declare_clippy_lint! {
declare_lint_pass!(SameNameMethod => [SAME_NAME_METHOD]); declare_lint_pass!(SameNameMethod => [SAME_NAME_METHOD]);
struct ExistingName { struct ExistingName {
impl_methods: BTreeMap<Symbol, Span>, impl_methods: BTreeMap<Symbol, (Span, HirId)>,
trait_methods: BTreeMap<Symbol, Vec<Span>>, 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| { let mut check_trait_method = |method_name: Symbol, trait_method_span: Span| {
if let Some(impl_span) = existing_name.impl_methods.get(&method_name) { if let Some((impl_span, hir_id)) = existing_name.impl_methods.get(&method_name) {
span_lint_and_then( span_lint_hir_and_then(
cx, cx,
SAME_NAME_METHOD, SAME_NAME_METHOD,
*hir_id,
*impl_span, *impl_span,
"method's name is the same as an existing method in a trait", "method's name is the same as an existing method in a trait",
|diag| { |diag| {
@ -136,10 +137,12 @@ impl<'tcx> LateLintPass<'tcx> for SameNameMethod {
}) { }) {
let method_name = impl_item_ref.ident.name; let method_name = impl_item_ref.ident.name;
let impl_span = impl_item_ref.span; 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) { if let Some(trait_spans) = existing_name.trait_methods.get(&method_name) {
span_lint_and_then( span_lint_hir_and_then(
cx, cx,
SAME_NAME_METHOD, SAME_NAME_METHOD,
hir_id,
impl_span, impl_span,
"method's name is the same as an existing method in a trait", "method's name is the same as an existing method in a trait",
|diag| { |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));
} }
}, },
} }

View file

@ -1,3 +1,4 @@
#![feature(lint_reasons)]
#![warn(clippy::same_name_method)] #![warn(clippy::same_name_method)]
#![allow(dead_code, non_camel_case_types)] #![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() {} fn main() {}

View file

@ -1,61 +1,61 @@
error: method's name is the same as an existing method in a trait 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() {} LL | fn foo() {}
| ^^^^^^^^^^^ | ^^^^^^^^^^^
| |
= note: `-D clippy::same-name-method` implied by `-D warnings` = note: `-D clippy::same-name-method` implied by `-D warnings`
note: existing `foo` defined here note: existing `foo` defined here
--> $DIR/same_name_method.rs:24:13 --> $DIR/same_name_method.rs:25:13
| |
LL | fn foo() {} LL | fn foo() {}
| ^^^^^^^^^^^ | ^^^^^^^^^^^
error: method's name is the same as an existing method in a trait 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() {} LL | fn clone() {}
| ^^^^^^^^^^^^^ | ^^^^^^^^^^^^^
| |
note: existing `clone` defined here note: existing `clone` defined here
--> $DIR/same_name_method.rs:30:18 --> $DIR/same_name_method.rs:31:18
| |
LL | #[derive(Clone)] LL | #[derive(Clone)]
| ^^^^^ | ^^^^^
= note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info) = 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 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() {} LL | fn foo() {}
| ^^^^^^^^^^^ | ^^^^^^^^^^^
| |
note: existing `foo` defined here note: existing `foo` defined here
--> $DIR/same_name_method.rs:48:13 --> $DIR/same_name_method.rs:49:13
| |
LL | fn foo() {} LL | fn foo() {}
| ^^^^^^^^^^^ | ^^^^^^^^^^^
error: method's name is the same as an existing method in a trait 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() {} LL | fn foo() {}
| ^^^^^^^^^^^ | ^^^^^^^^^^^
| |
note: existing `foo` defined here note: existing `foo` defined here
--> $DIR/same_name_method.rs:61:9 --> $DIR/same_name_method.rs:62:9
| |
LL | impl T1 for S {} LL | impl T1 for S {}
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
error: method's name is the same as an existing method in a trait 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() {} LL | fn foo() {}
| ^^^^^^^^^^^ | ^^^^^^^^^^^
| |
note: existing `foo` defined here note: existing `foo` defined here
--> $DIR/same_name_method.rs:73:9 --> $DIR/same_name_method.rs:74:9
| |
LL | impl T1 for S {} LL | impl T1 for S {}
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^