Skip to content

Commit fab9000

Browse files
committed
Do not lint when imported item contains underscore
1 parent bf4c998 commit fab9000

9 files changed

+207
-48
lines changed

clippy_lints/src/wildcard_imports.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ impl LateLintPass<'_> for WildcardImports {
132132
if self.warn_on_all || !self.check_exceptions(item, use_path.segments);
133133
let used_imports = cx.tcx.names_imported_by_glob_use(item.owner_id.def_id);
134134
if !used_imports.is_empty(); // Already handled by `unused_imports`
135+
if !used_imports.contains(&kw::Underscore);
135136
then {
136137
let mut applicability = Applicability::MachineApplicable;
137138
let import_source_snippet = snippet_with_applicability(cx, use_path.span, "..", &mut applicability);

tests/ui/wildcard_imports.fixed

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,34 @@ mod struct_mod {
6969
}
7070
}
7171

72+
// issue 9942
73+
mod underscore_mod {
74+
// allow use of `deref` so that `clippy --fix` includes `Deref`.
75+
#![allow(noop_method_call)]
76+
77+
mod exports_underscore {
78+
pub use std::ops::Deref as _;
79+
pub fn dummy() {}
80+
}
81+
82+
mod exports_underscore_ish {
83+
pub use std::ops::Deref as _Deref;
84+
pub fn dummy() {}
85+
}
86+
87+
fn does_not_lint() {
88+
use self::exports_underscore::*;
89+
let _ = (&0).deref();
90+
dummy();
91+
}
92+
93+
fn does_lint() {
94+
use self::exports_underscore_ish::{_Deref, dummy};
95+
let _ = (&0).deref();
96+
dummy();
97+
}
98+
}
99+
72100
fn main() {
73101
foo();
74102
multi_foo();

tests/ui/wildcard_imports.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,34 @@ mod struct_mod {
6969
}
7070
}
7171

72+
// issue 9942
73+
mod underscore_mod {
74+
// allow use of `deref` so that `clippy --fix` includes `Deref`.
75+
#![allow(noop_method_call)]
76+
77+
mod exports_underscore {
78+
pub use std::ops::Deref as _;
79+
pub fn dummy() {}
80+
}
81+
82+
mod exports_underscore_ish {
83+
pub use std::ops::Deref as _Deref;
84+
pub fn dummy() {}
85+
}
86+
87+
fn does_not_lint() {
88+
use self::exports_underscore::*;
89+
let _ = (&0).deref();
90+
dummy();
91+
}
92+
93+
fn does_lint() {
94+
use self::exports_underscore_ish::*;
95+
let _ = (&0).deref();
96+
dummy();
97+
}
98+
}
99+
72100
fn main() {
73101
foo();
74102
multi_foo();

tests/ui/wildcard_imports.stderr

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,96 +38,102 @@ LL | use wildcard_imports_helper::*;
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `wildcard_imports_helper::{ExternA, extern_foo}`
3939

4040
error: usage of wildcard import
41-
--> $DIR/wildcard_imports.rs:97:13
41+
--> $DIR/wildcard_imports.rs:94:13
42+
|
43+
LL | use self::exports_underscore_ish::*;
44+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `self::exports_underscore_ish::{_Deref, dummy}`
45+
46+
error: usage of wildcard import
47+
--> $DIR/wildcard_imports.rs:125:13
4248
|
4349
LL | use crate::fn_mod::*;
4450
| ^^^^^^^^^^^^^^^^ help: try: `crate::fn_mod::foo`
4551

4652
error: usage of wildcard import
47-
--> $DIR/wildcard_imports.rs:103:75
53+
--> $DIR/wildcard_imports.rs:131:75
4854
|
4955
LL | use wildcard_imports_helper::inner::inner_for_self_import::{self, *};
5056
| ^ help: try: `inner_extern_foo`
5157

5258
error: usage of wildcard import
53-
--> $DIR/wildcard_imports.rs:104:13
59+
--> $DIR/wildcard_imports.rs:132:13
5460
|
5561
LL | use wildcard_imports_helper::*;
5662
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `wildcard_imports_helper::{ExternA, extern_foo}`
5763

5864
error: usage of wildcard import
59-
--> $DIR/wildcard_imports.rs:116:20
65+
--> $DIR/wildcard_imports.rs:144:20
6066
|
6167
LL | use self::{inner::*, inner2::*};
6268
| ^^^^^^^^ help: try: `inner::inner_foo`
6369

6470
error: usage of wildcard import
65-
--> $DIR/wildcard_imports.rs:116:30
71+
--> $DIR/wildcard_imports.rs:144:30
6672
|
6773
LL | use self::{inner::*, inner2::*};
6874
| ^^^^^^^^^ help: try: `inner2::inner_bar`
6975

7076
error: usage of wildcard import
71-
--> $DIR/wildcard_imports.rs:123:13
77+
--> $DIR/wildcard_imports.rs:151:13
7278
|
7379
LL | use wildcard_imports_helper::*;
7480
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `wildcard_imports_helper::{ExternExportedEnum, ExternExportedStruct, extern_exported}`
7581

7682
error: usage of wildcard import
77-
--> $DIR/wildcard_imports.rs:152:9
83+
--> $DIR/wildcard_imports.rs:180:9
7884
|
7985
LL | use crate::in_fn_test::*;
8086
| ^^^^^^^^^^^^^^^^^^^^ help: try: `crate::in_fn_test::{ExportedEnum, ExportedStruct, exported}`
8187

8288
error: usage of wildcard import
83-
--> $DIR/wildcard_imports.rs:161:9
89+
--> $DIR/wildcard_imports.rs:189:9
8490
|
8591
LL | use crate:: in_fn_test:: * ;
8692
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `crate:: in_fn_test::exported`
8793

8894
error: usage of wildcard import
89-
--> $DIR/wildcard_imports.rs:162:9
95+
--> $DIR/wildcard_imports.rs:190:9
9096
|
9197
LL | use crate:: fn_mod::
9298
| _________^
9399
LL | | *;
94100
| |_________^ help: try: `crate:: fn_mod::foo`
95101

96102
error: usage of wildcard import
97-
--> $DIR/wildcard_imports.rs:173:13
103+
--> $DIR/wildcard_imports.rs:201:13
98104
|
99105
LL | use super::*;
100106
| ^^^^^^^^ help: try: `super::foofoo`
101107

102108
error: usage of wildcard import
103-
--> $DIR/wildcard_imports.rs:208:17
109+
--> $DIR/wildcard_imports.rs:236:17
104110
|
105111
LL | use super::*;
106112
| ^^^^^^^^ help: try: `super::insidefoo`
107113

108114
error: usage of wildcard import
109-
--> $DIR/wildcard_imports.rs:216:13
115+
--> $DIR/wildcard_imports.rs:244:13
110116
|
111117
LL | use crate::super_imports::*;
112118
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `crate::super_imports::foofoo`
113119

114120
error: usage of wildcard import
115-
--> $DIR/wildcard_imports.rs:225:17
121+
--> $DIR/wildcard_imports.rs:253:17
116122
|
117123
LL | use super::super::*;
118124
| ^^^^^^^^^^^^^^^ help: try: `super::super::foofoo`
119125

120126
error: usage of wildcard import
121-
--> $DIR/wildcard_imports.rs:234:13
127+
--> $DIR/wildcard_imports.rs:262:13
122128
|
123129
LL | use super::super::super_imports::*;
124130
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `super::super::super_imports::foofoo`
125131

126132
error: usage of wildcard import
127-
--> $DIR/wildcard_imports.rs:242:13
133+
--> $DIR/wildcard_imports.rs:270:13
128134
|
129135
LL | use super::*;
130136
| ^^^^^^^^ help: try: `super::foofoo`
131137

132-
error: aborting due to 21 previous errors
138+
error: aborting due to 22 previous errors
133139

tests/ui/wildcard_imports_2021.edition2018.fixed

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,34 @@ mod struct_mod {
6464
}
6565
}
6666

67+
// issue 9942
68+
mod underscore_mod {
69+
// allow use of `deref` so that `clippy --fix` includes `Deref`.
70+
#![allow(noop_method_call)]
71+
72+
mod exports_underscore {
73+
pub use std::ops::Deref as _;
74+
pub fn dummy() {}
75+
}
76+
77+
mod exports_underscore_ish {
78+
pub use std::ops::Deref as _Deref;
79+
pub fn dummy() {}
80+
}
81+
82+
fn does_not_lint() {
83+
use exports_underscore::*;
84+
let _ = (&0).deref();
85+
dummy();
86+
}
87+
88+
fn does_lint() {
89+
use exports_underscore_ish::{_Deref, dummy};
90+
let _ = (&0).deref();
91+
dummy();
92+
}
93+
}
94+
6795
fn main() {
6896
foo();
6997
multi_foo();

tests/ui/wildcard_imports_2021.edition2018.stderr

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,96 +38,102 @@ LL | use wildcard_imports_helper::*;
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `wildcard_imports_helper::{ExternA, extern_foo}`
3939

4040
error: usage of wildcard import
41-
--> $DIR/wildcard_imports_2021.rs:91:13
41+
--> $DIR/wildcard_imports_2021.rs:89:13
42+
|
43+
LL | use exports_underscore_ish::*;
44+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `exports_underscore_ish::{_Deref, dummy}`
45+
46+
error: usage of wildcard import
47+
--> $DIR/wildcard_imports_2021.rs:119:13
4248
|
4349
LL | use crate::fn_mod::*;
4450
| ^^^^^^^^^^^^^^^^ help: try: `crate::fn_mod::foo`
4551

4652
error: usage of wildcard import
47-
--> $DIR/wildcard_imports_2021.rs:97:75
53+
--> $DIR/wildcard_imports_2021.rs:125:75
4854
|
4955
LL | use wildcard_imports_helper::inner::inner_for_self_import::{self, *};
5056
| ^ help: try: `inner_extern_foo`
5157

5258
error: usage of wildcard import
53-
--> $DIR/wildcard_imports_2021.rs:98:13
59+
--> $DIR/wildcard_imports_2021.rs:126:13
5460
|
5561
LL | use wildcard_imports_helper::*;
5662
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `wildcard_imports_helper::{ExternA, extern_foo}`
5763

5864
error: usage of wildcard import
59-
--> $DIR/wildcard_imports_2021.rs:110:20
65+
--> $DIR/wildcard_imports_2021.rs:138:20
6066
|
6167
LL | use self::{inner::*, inner2::*};
6268
| ^^^^^^^^ help: try: `inner::inner_foo`
6369

6470
error: usage of wildcard import
65-
--> $DIR/wildcard_imports_2021.rs:110:30
71+
--> $DIR/wildcard_imports_2021.rs:138:30
6672
|
6773
LL | use self::{inner::*, inner2::*};
6874
| ^^^^^^^^^ help: try: `inner2::inner_bar`
6975

7076
error: usage of wildcard import
71-
--> $DIR/wildcard_imports_2021.rs:117:13
77+
--> $DIR/wildcard_imports_2021.rs:145:13
7278
|
7379
LL | use wildcard_imports_helper::*;
7480
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `wildcard_imports_helper::{ExternExportedEnum, ExternExportedStruct, extern_exported}`
7581

7682
error: usage of wildcard import
77-
--> $DIR/wildcard_imports_2021.rs:146:9
83+
--> $DIR/wildcard_imports_2021.rs:174:9
7884
|
7985
LL | use crate::in_fn_test::*;
8086
| ^^^^^^^^^^^^^^^^^^^^ help: try: `crate::in_fn_test::{ExportedEnum, ExportedStruct, exported}`
8187

8288
error: usage of wildcard import
83-
--> $DIR/wildcard_imports_2021.rs:155:9
89+
--> $DIR/wildcard_imports_2021.rs:183:9
8490
|
8591
LL | use crate:: in_fn_test:: * ;
8692
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `crate:: in_fn_test::exported`
8793

8894
error: usage of wildcard import
89-
--> $DIR/wildcard_imports_2021.rs:156:9
95+
--> $DIR/wildcard_imports_2021.rs:184:9
9096
|
9197
LL | use crate:: fn_mod::
9298
| _________^
9399
LL | | *;
94100
| |_________^ help: try: `crate:: fn_mod::foo`
95101

96102
error: usage of wildcard import
97-
--> $DIR/wildcard_imports_2021.rs:167:13
103+
--> $DIR/wildcard_imports_2021.rs:195:13
98104
|
99105
LL | use super::*;
100106
| ^^^^^^^^ help: try: `super::foofoo`
101107

102108
error: usage of wildcard import
103-
--> $DIR/wildcard_imports_2021.rs:202:17
109+
--> $DIR/wildcard_imports_2021.rs:230:17
104110
|
105111
LL | use super::*;
106112
| ^^^^^^^^ help: try: `super::insidefoo`
107113

108114
error: usage of wildcard import
109-
--> $DIR/wildcard_imports_2021.rs:210:13
115+
--> $DIR/wildcard_imports_2021.rs:238:13
110116
|
111117
LL | use crate::super_imports::*;
112118
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `crate::super_imports::foofoo`
113119

114120
error: usage of wildcard import
115-
--> $DIR/wildcard_imports_2021.rs:219:17
121+
--> $DIR/wildcard_imports_2021.rs:247:17
116122
|
117123
LL | use super::super::*;
118124
| ^^^^^^^^^^^^^^^ help: try: `super::super::foofoo`
119125

120126
error: usage of wildcard import
121-
--> $DIR/wildcard_imports_2021.rs:228:13
127+
--> $DIR/wildcard_imports_2021.rs:256:13
122128
|
123129
LL | use super::super::super_imports::*;
124130
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `super::super::super_imports::foofoo`
125131

126132
error: usage of wildcard import
127-
--> $DIR/wildcard_imports_2021.rs:236:13
133+
--> $DIR/wildcard_imports_2021.rs:264:13
128134
|
129135
LL | use super::*;
130136
| ^^^^^^^^ help: try: `super::foofoo`
131137

132-
error: aborting due to 21 previous errors
138+
error: aborting due to 22 previous errors
133139

tests/ui/wildcard_imports_2021.edition2021.fixed

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,34 @@ mod struct_mod {
6464
}
6565
}
6666

67+
// issue 9942
68+
mod underscore_mod {
69+
// allow use of `deref` so that `clippy --fix` includes `Deref`.
70+
#![allow(noop_method_call)]
71+
72+
mod exports_underscore {
73+
pub use std::ops::Deref as _;
74+
pub fn dummy() {}
75+
}
76+
77+
mod exports_underscore_ish {
78+
pub use std::ops::Deref as _Deref;
79+
pub fn dummy() {}
80+
}
81+
82+
fn does_not_lint() {
83+
use exports_underscore::*;
84+
let _ = (&0).deref();
85+
dummy();
86+
}
87+
88+
fn does_lint() {
89+
use exports_underscore_ish::{_Deref, dummy};
90+
let _ = (&0).deref();
91+
dummy();
92+
}
93+
}
94+
6795
fn main() {
6896
foo();
6997
multi_foo();

0 commit comments

Comments
 (0)