Skip to content

Commit 06e94a2

Browse files
author
Anthony Huang
committed
Address underscores and test naming
1 parent 1435e8d commit 06e94a2

File tree

3 files changed

+25
-49
lines changed

3 files changed

+25
-49
lines changed

clippy_lints/src/self_named_constructor.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use clippy_utils::diagnostics::span_lint_and_then;
2-
use clippy_utils::ty::{contains_adt_constructor, contains_ty};
1+
use clippy_utils::diagnostics::span_lint;
32
use clippy_utils::return_ty;
3+
use clippy_utils::ty::{contains_adt_constructor, contains_ty};
44
use rustc_hir::{ImplItem, ImplItemKind, Node};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -64,17 +64,14 @@ impl<'tcx> LateLintPass<'tcx> for SelfNamedConstructor {
6464
let self_id = cx.tcx.hir().local_def_id_to_hir_id(self_local_did);
6565
if let Some(Node::Item(x)) = cx.tcx.hir().find(self_id);
6666
let type_name = x.ident.name.as_str().to_lowercase();
67-
if impl_item.ident.name.as_str() == type_name;
67+
if impl_item.ident.name.as_str() == type_name || impl_item.ident.name.as_str().replace("_", "") == type_name;
6868

6969
then {
70-
span_lint_and_then(
70+
span_lint(
7171
cx,
7272
SELF_NAMED_CONSTRUCTOR,
7373
impl_item.span,
7474
&format!("constructor `{}` has the same name as the type", impl_item.ident.name),
75-
|diag| {
76-
diag.span_help(impl_item.span, &format!("consider renaming `{}()` to `new()`", impl_item.ident.name));
77-
},
7875
);
7976
}
8077
}

tests/ui/self_named_constructor.rs

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,37 @@
11
#![warn(clippy::self_named_constructor)]
22

3-
struct Q;
3+
struct ShouldSpawn;
4+
struct ShouldNotSpawn;
45

5-
impl Q {
6-
pub fn q() -> Q {
7-
Q
6+
impl ShouldSpawn {
7+
pub fn should_spawn() -> ShouldSpawn {
8+
ShouldSpawn
89
}
910

10-
fn r() -> R {
11-
R
11+
fn should_not_spawn() -> ShouldNotSpawn {
12+
ShouldNotSpawn
1213
}
1314
}
1415

15-
struct R;
16-
17-
impl R {
18-
pub fn new() -> R {
19-
R
20-
}
21-
}
22-
23-
struct V;
24-
25-
impl V {
26-
pub fn v() -> R {
27-
R
16+
impl ShouldNotSpawn {
17+
pub fn new() -> ShouldNotSpawn {
18+
ShouldNotSpawn
2819
}
2920
}
3021

31-
struct U;
22+
struct ShouldNotSpawnWithTrait;
3223

33-
trait T {
24+
trait ShouldNotSpawnTrait {
3425
type Item;
3526
}
3627

37-
impl T for U {
28+
impl ShouldNotSpawnTrait for ShouldNotSpawnWithTrait {
3829
type Item = Self;
3930
}
4031

41-
impl U {
42-
pub fn u() -> impl T<Item = Self> {
43-
U
32+
impl ShouldNotSpawnWithTrait {
33+
pub fn should_not_spawn_with_trait() -> impl ShouldNotSpawnTrait<Item = Self> {
34+
ShouldNotSpawnWithTrait
4435
}
4536
}
4637

tests/ui/self_named_constructor.stderr

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,19 @@
1-
error: constructor `q` has the same name as the type
2-
--> $DIR/self_named_constructor.rs:6:5
1+
error: constructor `should_spawn` has the same name as the type
2+
--> $DIR/self_named_constructor.rs:7:5
33
|
4-
LL | / pub fn q() -> Q {
5-
LL | | Q
4+
LL | / pub fn should_spawn() -> ShouldSpawn {
5+
LL | | ShouldSpawn
66
LL | | }
77
| |_____^
88
|
99
= note: `-D clippy::self-named-constructor` implied by `-D warnings`
10-
help: consider renaming `q()` to `new()`
11-
--> $DIR/self_named_constructor.rs:6:5
12-
|
13-
LL | / pub fn q() -> Q {
14-
LL | | Q
15-
LL | | }
16-
| |_____^
1710

1811
error: constructor `default` has the same name as the type
19-
--> $DIR/self_named_constructor.rs:47:10
12+
--> $DIR/self_named_constructor.rs:38:10
2013
|
2114
LL | #[derive(Default)]
2215
| ^^^^^^^
2316
|
24-
help: consider renaming `default()` to `new()`
25-
--> $DIR/self_named_constructor.rs:47:10
26-
|
27-
LL | #[derive(Default)]
28-
| ^^^^^^^
2917
= note: this error originates in the derive macro `Default` (in Nightly builds, run with -Z macro-backtrace for more info)
3018

3119
error: aborting due to 2 previous errors

0 commit comments

Comments
 (0)