Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 357a8f0

Browse files
author
Anthony Huang
committed
Add redundant_method_names lint
1 parent 64d74df commit 357a8f0

19 files changed

+253
-65
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2772,6 +2772,7 @@ Released 2018-09-13
27722772
[`same_item_push`]: https://rust-lang.github.io/rust-clippy/master/index.html#same_item_push
27732773
[`search_is_some`]: https://rust-lang.github.io/rust-clippy/master/index.html#search_is_some
27742774
[`self_assignment`]: https://rust-lang.github.io/rust-clippy/master/index.html#self_assignment
2775+
[`self_named_constructor`]: https://rust-lang.github.io/rust-clippy/master/index.html#self_named_constructor
27752776
[`semicolon_if_nothing_returned`]: https://rust-lang.github.io/rust-clippy/master/index.html#semicolon_if_nothing_returned
27762777
[`serde_api_misuse`]: https://rust-lang.github.io/rust-clippy/master/index.html#serde_api_misuse
27772778
[`shadow_reuse`]: https://rust-lang.github.io/rust-clippy/master/index.html#shadow_reuse

clippy_lints/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ mod regex;
330330
mod repeat_once;
331331
mod returns;
332332
mod self_assignment;
333+
mod self_named_constructor;
333334
mod semicolon_if_nothing_returned;
334335
mod serde_api;
335336
mod shadow;
@@ -900,6 +901,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
900901
returns::LET_AND_RETURN,
901902
returns::NEEDLESS_RETURN,
902903
self_assignment::SELF_ASSIGNMENT,
904+
self_named_constructor::SELF_NAMED_CONSTRUCTOR,
903905
semicolon_if_nothing_returned::SEMICOLON_IF_NOTHING_RETURNED,
904906
serde_api::SERDE_API_MISUSE,
905907
shadow::SHADOW_REUSE,
@@ -1406,6 +1408,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
14061408
LintId::of(returns::LET_AND_RETURN),
14071409
LintId::of(returns::NEEDLESS_RETURN),
14081410
LintId::of(self_assignment::SELF_ASSIGNMENT),
1411+
LintId::of(self_named_constructor::SELF_NAMED_CONSTRUCTOR),
14091412
LintId::of(serde_api::SERDE_API_MISUSE),
14101413
LintId::of(single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS),
14111414
LintId::of(size_of_in_element_count::SIZE_OF_IN_ELEMENT_COUNT),
@@ -1559,6 +1562,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
15591562
LintId::of(redundant_static_lifetimes::REDUNDANT_STATIC_LIFETIMES),
15601563
LintId::of(returns::LET_AND_RETURN),
15611564
LintId::of(returns::NEEDLESS_RETURN),
1565+
LintId::of(self_named_constructor::SELF_NAMED_CONSTRUCTOR),
15621566
LintId::of(single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS),
15631567
LintId::of(tabs_in_doc_comments::TABS_IN_DOC_COMMENTS),
15641568
LintId::of(to_digit_is_some::TO_DIGIT_IS_SOME),
@@ -2101,6 +2105,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
21012105
let scripts = conf.allowed_scripts.clone();
21022106
store.register_early_pass(move || box disallowed_script_idents::DisallowedScriptIdents::new(&scripts));
21032107
store.register_late_pass(|| box strlen_on_c_strings::StrlenOnCStrings);
2108+
store.register_late_pass(move || box self_named_constructor::SelfNamedConstructor);
2109+
21042110
}
21052111

21062112
#[rustfmt::skip]
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
use clippy_utils::diagnostics::span_lint;
2+
use clippy_utils::return_ty;
3+
use clippy_utils::ty::{contains_adt_constructor, contains_ty};
4+
use rustc_hir::{Impl, ImplItem, ImplItemKind, ItemKind, Node};
5+
use rustc_lint::{LateContext, LateLintPass};
6+
use rustc_session::{declare_lint_pass, declare_tool_lint};
7+
8+
declare_clippy_lint! {
9+
/// **What it does:** Warns when constructors have the same name as their types.
10+
///
11+
/// **Why is this bad?** Repeating the name of the type is redundant.
12+
///
13+
/// **Known problems:** None.
14+
///
15+
/// **Example:**
16+
///
17+
/// ```rust,ignore
18+
/// struct Foo {}
19+
///
20+
/// impl Foo {
21+
/// pub fn foo() -> Foo {
22+
/// Foo {}
23+
/// }
24+
/// }
25+
/// ```
26+
/// Use instead:
27+
/// ```rust,ignore
28+
/// struct Foo {}
29+
///
30+
/// impl Foo {
31+
/// pub fn new() -> Foo {
32+
/// Foo {}
33+
/// }
34+
/// }
35+
/// ```
36+
pub SELF_NAMED_CONSTRUCTOR,
37+
style,
38+
"method should not have the same name as the type it is implemented for"
39+
}
40+
41+
declare_lint_pass!(SelfNamedConstructor => [SELF_NAMED_CONSTRUCTOR]);
42+
43+
impl<'tcx> LateLintPass<'tcx> for SelfNamedConstructor {
44+
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx ImplItem<'_>) {
45+
match impl_item.kind {
46+
ImplItemKind::Fn(ref sig, _) => {
47+
if sig.decl.implicit_self.has_implicit_self() {
48+
return;
49+
}
50+
},
51+
_ => return,
52+
}
53+
54+
let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id());
55+
let item = cx.tcx.hir().expect_item(parent);
56+
let self_ty = cx.tcx.type_of(item.def_id);
57+
let ret_ty = return_ty(cx, impl_item.hir_id());
58+
59+
// Do not check trait impls
60+
if matches!(item.kind, ItemKind::Impl(Impl { of_trait: Some(_), .. })) {
61+
return;
62+
}
63+
64+
// Ensure method is constructor-like
65+
if let Some(self_adt) = self_ty.ty_adt_def() {
66+
if !contains_adt_constructor(ret_ty, self_adt) {
67+
return;
68+
}
69+
} else if !contains_ty(ret_ty, self_ty) {
70+
return;
71+
}
72+
73+
if_chain! {
74+
if let Some(self_def) = self_ty.ty_adt_def();
75+
if let Some(self_local_did) = self_def.did.as_local();
76+
let self_id = cx.tcx.hir().local_def_id_to_hir_id(self_local_did);
77+
if let Some(Node::Item(x)) = cx.tcx.hir().find(self_id);
78+
let type_name = x.ident.name.as_str().to_lowercase();
79+
if impl_item.ident.name.as_str() == type_name || impl_item.ident.name.as_str().replace("_", "") == type_name;
80+
81+
then {
82+
span_lint(
83+
cx,
84+
SELF_NAMED_CONSTRUCTOR,
85+
impl_item.span,
86+
&format!("constructor `{}` has the same name as the type", impl_item.ident.name),
87+
);
88+
}
89+
}
90+
}
91+
}

tests/ui/crashes/ice-6179.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
struct Foo {}
88

99
impl Foo {
10-
fn foo() -> Self {
10+
fn new() -> Self {
1111
impl Foo {
1212
fn bar() {}
1313
}

tests/ui/issue_4266.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ async fn all_to_one<'a>(a: &'a str, _b: &'a str) -> &'a str {
2525
struct Foo;
2626
impl Foo {
2727
// ok
28-
pub async fn foo(&mut self) {}
28+
pub async fn new(&mut self) -> Self {
29+
Foo {}
30+
}
2931
}
3032

3133
// rust-lang/rust#61115

tests/ui/missing-doc-impl.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ pub trait E: Sized {
5959
}
6060

6161
impl Foo {
62-
pub fn foo() {}
62+
pub fn new() -> Self {
63+
Foo { a: 0, b: 0 }
64+
}
6365
fn bar() {}
6466
}
6567

tests/ui/missing-doc-impl.stderr

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,25 @@ LL | type AssociatedTypeDef = Self;
7878
error: missing documentation for an associated function
7979
--> $DIR/missing-doc-impl.rs:62:5
8080
|
81-
LL | pub fn foo() {}
82-
| ^^^^^^^^^^^^^^^
81+
LL | / pub fn new() -> Self {
82+
LL | | Foo { a: 0, b: 0 }
83+
LL | | }
84+
| |_____^
8385

8486
error: missing documentation for an associated function
85-
--> $DIR/missing-doc-impl.rs:63:5
87+
--> $DIR/missing-doc-impl.rs:65:5
8688
|
8789
LL | fn bar() {}
8890
| ^^^^^^^^^^^
8991

9092
error: missing documentation for an associated function
91-
--> $DIR/missing-doc-impl.rs:67:5
93+
--> $DIR/missing-doc-impl.rs:69:5
9294
|
9395
LL | pub fn foo() {}
9496
| ^^^^^^^^^^^^^^^
9597

9698
error: missing documentation for an associated function
97-
--> $DIR/missing-doc-impl.rs:71:5
99+
--> $DIR/missing-doc-impl.rs:73:5
98100
|
99101
LL | / fn foo2() -> u32 {
100102
LL | | 1

tests/ui/missing_const_for_fn/cant_be_const.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ mod with_drop {
8484

8585
impl A {
8686
// This can not be const because the type implements `Drop`.
87-
pub fn a(self) -> B {
87+
pub fn b(self) -> B {
8888
B
8989
}
9090
}

tests/ui/missing_const_for_fn/could_be_const.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ LL | | }
6060
error: this could be a `const fn`
6161
--> $DIR/could_be_const.rs:68:9
6262
|
63-
LL | / pub fn b(self, a: &A) -> B {
63+
LL | / pub fn new(a: &A) -> B {
6464
LL | | B
6565
LL | | }
6666
| |_________^

tests/ui/needless_bool/fixable.fixed

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
dead_code,
77
clippy::no_effect,
88
clippy::if_same_then_else,
9-
clippy::needless_return
9+
clippy::needless_return,
10+
clippy::self_named_constructor
1011
)]
1112

1213
use std::cell::Cell;

0 commit comments

Comments
 (0)