File tree Expand file tree Collapse file tree 3 files changed +25
-49
lines changed Expand file tree Collapse file tree 3 files changed +25
-49
lines changed Original file line number Diff line number Diff line change 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;
3
2
use clippy_utils:: return_ty;
3
+ use clippy_utils:: ty:: { contains_adt_constructor, contains_ty} ;
4
4
use rustc_hir:: { ImplItem , ImplItemKind , Node } ;
5
5
use rustc_lint:: { LateContext , LateLintPass } ;
6
6
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
@@ -64,17 +64,14 @@ impl<'tcx> LateLintPass<'tcx> for SelfNamedConstructor {
64
64
let self_id = cx. tcx. hir( ) . local_def_id_to_hir_id( self_local_did) ;
65
65
if let Some ( Node :: Item ( x) ) = cx. tcx. hir( ) . find( self_id) ;
66
66
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 ;
68
68
69
69
then {
70
- span_lint_and_then (
70
+ span_lint (
71
71
cx,
72
72
SELF_NAMED_CONSTRUCTOR ,
73
73
impl_item. span,
74
74
& 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
- } ,
78
75
) ;
79
76
}
80
77
}
Original file line number Diff line number Diff line change 1
1
#![ warn( clippy:: self_named_constructor) ]
2
2
3
- struct Q ;
3
+ struct ShouldSpawn ;
4
+ struct ShouldNotSpawn ;
4
5
5
- impl Q {
6
- pub fn q ( ) -> Q {
7
- Q
6
+ impl ShouldSpawn {
7
+ pub fn should_spawn ( ) -> ShouldSpawn {
8
+ ShouldSpawn
8
9
}
9
10
10
- fn r ( ) -> R {
11
- R
11
+ fn should_not_spawn ( ) -> ShouldNotSpawn {
12
+ ShouldNotSpawn
12
13
}
13
14
}
14
15
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
28
19
}
29
20
}
30
21
31
- struct U ;
22
+ struct ShouldNotSpawnWithTrait ;
32
23
33
- trait T {
24
+ trait ShouldNotSpawnTrait {
34
25
type Item ;
35
26
}
36
27
37
- impl T for U {
28
+ impl ShouldNotSpawnTrait for ShouldNotSpawnWithTrait {
38
29
type Item = Self ;
39
30
}
40
31
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
44
35
}
45
36
}
46
37
Original file line number Diff line number Diff line change 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
3
3
|
4
- LL | / pub fn q () -> Q {
5
- LL | | Q
4
+ LL | / pub fn should_spawn () -> ShouldSpawn {
5
+ LL | | ShouldSpawn
6
6
LL | | }
7
7
| |_____^
8
8
|
9
9
= 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
- | |_____^
17
10
18
11
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
20
13
|
21
14
LL | #[derive(Default)]
22
15
| ^^^^^^^
23
16
|
24
- help: consider renaming `default()` to `new()`
25
- --> $DIR/self_named_constructor.rs:47:10
26
- |
27
- LL | #[derive(Default)]
28
- | ^^^^^^^
29
17
= note: this error originates in the derive macro `Default` (in Nightly builds, run with -Z macro-backtrace for more info)
30
18
31
19
error: aborting due to 2 previous errors
You can’t perform that action at this time.
0 commit comments