Skip to content

Commit 08334fc

Browse files
author
Anthony Huang
committed
Don't check trait impls
1 parent 06e94a2 commit 08334fc

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

clippy_lints/src/self_named_constructor.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint;
22
use clippy_utils::return_ty;
33
use clippy_utils::ty::{contains_adt_constructor, contains_ty};
4-
use rustc_hir::{ImplItem, ImplItemKind, Node};
4+
use rustc_hir::{Impl, ImplItem, ImplItemKind, ItemKind, Node};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
77

@@ -51,13 +51,21 @@ impl<'tcx> LateLintPass<'tcx> for SelfNamedConstructor {
5151
let item = cx.tcx.hir().expect_item(parent);
5252
let self_ty = cx.tcx.type_of(item.def_id);
5353
let ret_ty = return_ty(cx, impl_item.hir_id());
54+
55+
// Do not check trait impls
56+
if matches!(item.kind, ItemKind::Impl(Impl { of_trait: Some(_), .. })) {
57+
return;
58+
}
59+
60+
// Ensure method is constructor-like
5461
if let Some(self_adt) = self_ty.ty_adt_def() {
5562
if !contains_adt_constructor(ret_ty, self_adt) {
5663
return;
5764
}
5865
} else if !contains_ty(ret_ty, self_ty) {
5966
return;
6067
}
68+
6169
if_chain! {
6270
if let Some(self_def) = self_ty.ty_adt_def();
6371
if let Some(self_local_did) = self_def.did.as_local();

tests/ui/self_named_constructor.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,17 @@ impl ShouldNotSpawnWithTrait {
3535
}
3636
}
3737

38+
// Same trait name and same type name should not spawn the lint
3839
#[derive(Default)]
3940
pub struct Default;
4041

42+
trait TraitSameTypeName {
43+
fn should_not_spawn() -> Self;
44+
}
45+
impl TraitSameTypeName for ShouldNotSpawn {
46+
fn should_not_spawn() -> Self {
47+
ShouldNotSpawn
48+
}
49+
}
50+
4151
fn main() {}

tests/ui/self_named_constructor.stderr

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,5 @@ LL | | }
88
|
99
= note: `-D clippy::self-named-constructor` implied by `-D warnings`
1010

11-
error: constructor `default` has the same name as the type
12-
--> $DIR/self_named_constructor.rs:38:10
13-
|
14-
LL | #[derive(Default)]
15-
| ^^^^^^^
16-
|
17-
= note: this error originates in the derive macro `Default` (in Nightly builds, run with -Z macro-backtrace for more info)
18-
19-
error: aborting due to 2 previous errors
11+
error: aborting due to previous error
2012

0 commit comments

Comments
 (0)