Skip to content

Commit 7e1730e

Browse files
committed
Fix #[expect] for same_name_method
1 parent a613460 commit 7e1730e

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

clippy_lints/src/same_name_method.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use clippy_utils::diagnostics::span_lint_and_then;
1+
use clippy_utils::diagnostics::span_lint_hir_and_then;
22
use rustc_data_structures::fx::FxHashMap;
33
use rustc_hir::def::{DefKind, Res};
4-
use rustc_hir::{Impl, ItemKind, Node, Path, QPath, TraitRef, TyKind};
4+
use rustc_hir::{HirId, Impl, ItemKind, Node, Path, QPath, TraitRef, TyKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_middle::ty::AssocKind;
77
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -42,7 +42,7 @@ declare_clippy_lint! {
4242
declare_lint_pass!(SameNameMethod => [SAME_NAME_METHOD]);
4343

4444
struct ExistingName {
45-
impl_methods: BTreeMap<Symbol, Span>,
45+
impl_methods: BTreeMap<Symbol, (Span, HirId)>,
4646
trait_methods: BTreeMap<Symbol, Vec<Span>>,
4747
}
4848

@@ -97,10 +97,11 @@ impl<'tcx> LateLintPass<'tcx> for SameNameMethod {
9797
};
9898

9999
let mut check_trait_method = |method_name: Symbol, trait_method_span: Span| {
100-
if let Some(impl_span) = existing_name.impl_methods.get(&method_name) {
101-
span_lint_and_then(
100+
if let Some((impl_span, hir_id)) = existing_name.impl_methods.get(&method_name) {
101+
span_lint_hir_and_then(
102102
cx,
103103
SAME_NAME_METHOD,
104+
*hir_id,
104105
*impl_span,
105106
"method's name is the same as an existing method in a trait",
106107
|diag| {
@@ -136,10 +137,12 @@ impl<'tcx> LateLintPass<'tcx> for SameNameMethod {
136137
}) {
137138
let method_name = impl_item_ref.ident.name;
138139
let impl_span = impl_item_ref.span;
140+
let hir_id = impl_item_ref.id.hir_id();
139141
if let Some(trait_spans) = existing_name.trait_methods.get(&method_name) {
140-
span_lint_and_then(
142+
span_lint_hir_and_then(
141143
cx,
142144
SAME_NAME_METHOD,
145+
hir_id,
143146
impl_span,
144147
"method's name is the same as an existing method in a trait",
145148
|diag| {
@@ -152,7 +155,7 @@ impl<'tcx> LateLintPass<'tcx> for SameNameMethod {
152155
},
153156
);
154157
}
155-
existing_name.impl_methods.insert(method_name, impl_span);
158+
existing_name.impl_methods.insert(method_name, (impl_span, hir_id));
156159
}
157160
},
158161
}

tests/ui/same_name_method.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![feature(lint_reasons)]
12
#![warn(clippy::same_name_method)]
23
#![allow(dead_code, non_camel_case_types)]
34

@@ -108,4 +109,19 @@ mod should_not_lint {
108109
}
109110
}
110111

112+
mod check_expect_suppression {
113+
use crate::T1;
114+
115+
struct S;
116+
117+
impl S {
118+
#[expect(clippy::same_name_method)]
119+
fn foo() {}
120+
}
121+
122+
impl T1 for S {
123+
fn foo() {}
124+
}
125+
}
126+
111127
fn main() {}

tests/ui/same_name_method.stderr

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,61 @@
11
error: method's name is the same as an existing method in a trait
2-
--> $DIR/same_name_method.rs:20:13
2+
--> $DIR/same_name_method.rs:21:13
33
|
44
LL | fn foo() {}
55
| ^^^^^^^^^^^
66
|
77
= note: `-D clippy::same-name-method` implied by `-D warnings`
88
note: existing `foo` defined here
9-
--> $DIR/same_name_method.rs:24:13
9+
--> $DIR/same_name_method.rs:25:13
1010
|
1111
LL | fn foo() {}
1212
| ^^^^^^^^^^^
1313

1414
error: method's name is the same as an existing method in a trait
15-
--> $DIR/same_name_method.rs:34:13
15+
--> $DIR/same_name_method.rs:35:13
1616
|
1717
LL | fn clone() {}
1818
| ^^^^^^^^^^^^^
1919
|
2020
note: existing `clone` defined here
21-
--> $DIR/same_name_method.rs:30:18
21+
--> $DIR/same_name_method.rs:31:18
2222
|
2323
LL | #[derive(Clone)]
2424
| ^^^^^
2525
= note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)
2626

2727
error: method's name is the same as an existing method in a trait
28-
--> $DIR/same_name_method.rs:44:13
28+
--> $DIR/same_name_method.rs:45:13
2929
|
3030
LL | fn foo() {}
3131
| ^^^^^^^^^^^
3232
|
3333
note: existing `foo` defined here
34-
--> $DIR/same_name_method.rs:48:13
34+
--> $DIR/same_name_method.rs:49:13
3535
|
3636
LL | fn foo() {}
3737
| ^^^^^^^^^^^
3838

3939
error: method's name is the same as an existing method in a trait
40-
--> $DIR/same_name_method.rs:58:13
40+
--> $DIR/same_name_method.rs:59:13
4141
|
4242
LL | fn foo() {}
4343
| ^^^^^^^^^^^
4444
|
4545
note: existing `foo` defined here
46-
--> $DIR/same_name_method.rs:61:9
46+
--> $DIR/same_name_method.rs:62:9
4747
|
4848
LL | impl T1 for S {}
4949
| ^^^^^^^^^^^^^^^^
5050

5151
error: method's name is the same as an existing method in a trait
52-
--> $DIR/same_name_method.rs:70:13
52+
--> $DIR/same_name_method.rs:71:13
5353
|
5454
LL | fn foo() {}
5555
| ^^^^^^^^^^^
5656
|
5757
note: existing `foo` defined here
58-
--> $DIR/same_name_method.rs:73:9
58+
--> $DIR/same_name_method.rs:74:9
5959
|
6060
LL | impl T1 for S {}
6161
| ^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)