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_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_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 @@ fn check_crate_post(&mut self, cx: &LateContext<'tcx>) {
};
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 @@ fn check_crate_post(&mut self, cx: &LateContext<'tcx>) {
}) {
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 @@ fn check_crate_post(&mut self, cx: &LateContext<'tcx>) {
},
);
}
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)]
#![allow(dead_code, non_camel_case_types)]
@ -108,4 +109,19 @@ impl T3 for S {
}
}
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() {}

View File

@ -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 {}
| ^^^^^^^^^^^^^^^^