1
- use clippy_utils:: diagnostics:: span_lint_and_sugg;
1
+ use clippy_utils:: diagnostics:: { span_lint_and_help , span_lint_and_sugg} ;
2
2
use clippy_utils:: in_macro;
3
3
use rustc_ast:: { ptr:: P , Crate , Item , ItemKind , ModKind , UseTreeKind } ;
4
4
use rustc_errors:: Applicability ;
@@ -66,15 +66,27 @@ fn check_mod(cx: &EarlyContext<'_>, items: &[P<Item>]) {
66
66
67
67
for single_use in & single_use_usages {
68
68
if !imports_reused_with_self. contains ( & single_use. 0 ) {
69
- span_lint_and_sugg (
70
- cx,
71
- SINGLE_COMPONENT_PATH_IMPORTS ,
72
- single_use. 1 ,
73
- "this import is redundant" ,
74
- "remove it entirely" ,
75
- String :: new ( ) ,
76
- Applicability :: MachineApplicable ,
77
- ) ;
69
+ let can_suggest = single_use. 2 ;
70
+ if can_suggest {
71
+ span_lint_and_sugg (
72
+ cx,
73
+ SINGLE_COMPONENT_PATH_IMPORTS ,
74
+ single_use. 1 ,
75
+ "this import is redundant" ,
76
+ "remove it entirely" ,
77
+ String :: new ( ) ,
78
+ Applicability :: MachineApplicable ,
79
+ ) ;
80
+ } else {
81
+ span_lint_and_help (
82
+ cx,
83
+ SINGLE_COMPONENT_PATH_IMPORTS ,
84
+ single_use. 1 ,
85
+ "this import is redundant" ,
86
+ None ,
87
+ "remove this import" ,
88
+ ) ;
89
+ }
78
90
}
79
91
}
80
92
}
@@ -83,7 +95,7 @@ fn track_uses(
83
95
cx : & EarlyContext < ' _ > ,
84
96
item : & Item ,
85
97
imports_reused_with_self : & mut Vec < Symbol > ,
86
- single_use_usages : & mut Vec < ( Symbol , Span ) > ,
98
+ single_use_usages : & mut Vec < ( Symbol , Span , bool ) > ,
87
99
) {
88
100
if in_macro ( item. span ) || item. vis . kind . is_pub ( ) {
89
101
return ;
@@ -100,25 +112,40 @@ fn track_uses(
100
112
if segments. len ( ) == 1 {
101
113
if let UseTreeKind :: Simple ( None , _, _) = use_tree. kind {
102
114
let ident = & segments[ 0 ] . ident ;
103
- single_use_usages. push ( ( ident. name , item. span ) ) ;
115
+ single_use_usages. push ( ( ident. name , item. span , true ) ) ;
104
116
}
105
117
return ;
106
118
}
107
119
108
- // keep track of `use self::some_module` usages
109
- if segments[ 0 ] . ident . name == kw:: SelfLower {
110
- // simple case such as `use self::module::SomeStruct`
111
- if segments. len ( ) > 1 {
112
- imports_reused_with_self. push ( segments[ 1 ] . ident . name ) ;
113
- return ;
114
- }
115
-
116
- // nested case such as `use self::{module1::Struct1, module2::Struct2}`
120
+ if segments. is_empty ( ) {
121
+ // keep track of `use {some_module, some_other_module};` usages
117
122
if let UseTreeKind :: Nested ( trees) = & use_tree. kind {
118
123
for tree in trees {
119
124
let segments = & tree. 0 . prefix . segments ;
120
- if !segments. is_empty ( ) {
121
- imports_reused_with_self. push ( segments[ 0 ] . ident . name ) ;
125
+ if segments. len ( ) == 1 {
126
+ if let UseTreeKind :: Simple ( None , _, _) = tree. 0 . kind {
127
+ let ident = & segments[ 0 ] . ident ;
128
+ single_use_usages. push ( ( ident. name , tree. 0 . span , false ) ) ;
129
+ }
130
+ }
131
+ }
132
+ }
133
+ } else {
134
+ // keep track of `use self::some_module` usages
135
+ if segments[ 0 ] . ident . name == kw:: SelfLower {
136
+ // simple case such as `use self::module::SomeStruct`
137
+ if segments. len ( ) > 1 {
138
+ imports_reused_with_self. push ( segments[ 1 ] . ident . name ) ;
139
+ return ;
140
+ }
141
+
142
+ // nested case such as `use self::{module1::Struct1, module2::Struct2}`
143
+ if let UseTreeKind :: Nested ( trees) = & use_tree. kind {
144
+ for tree in trees {
145
+ let segments = & tree. 0 . prefix . segments ;
146
+ if !segments. is_empty ( ) {
147
+ imports_reused_with_self. push ( segments[ 0 ] . ident . name ) ;
148
+ }
122
149
}
123
150
}
124
151
}
0 commit comments