Skip to content

Commit 4adbd9f

Browse files
committed
Add tests for wildcard_imports lint
1 parent ac795a6 commit 4adbd9f

File tree

4 files changed

+377
-0
lines changed

4 files changed

+377
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
pub use crate::extern_exports::*;
2+
3+
pub fn extern_foo() {}
4+
pub fn extern_bar() {}
5+
6+
pub struct ExternA;
7+
8+
pub mod inner {
9+
pub mod inner_for_self_import {
10+
pub fn inner_extern_foo() {}
11+
pub fn inner_extern_bar() {}
12+
}
13+
}
14+
15+
mod extern_exports {
16+
pub fn extern_exported() {}
17+
pub struct ExternExportedStruct;
18+
pub enum ExternExportedEnum {
19+
A,
20+
}
21+
}

tests/ui/wildcard_imports.fixed

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// run-rustfix
2+
// aux-build:wildcard_imports_helper.rs
3+
4+
#![warn(clippy::wildcard_imports)]
5+
#![allow(unused)]
6+
#![warn(unused_imports)]
7+
8+
extern crate wildcard_imports_helper;
9+
10+
use crate::fn_mod::foo;
11+
use crate::mod_mod::inner_mod;
12+
use crate::multi_fn_mod::{multi_bar, multi_foo, multi_inner_mod};
13+
#[macro_use]
14+
use crate::struct_mod::{A, inner_struct_mod};
15+
16+
#[allow(unused_imports)]
17+
use wildcard_imports_helper::inner::inner_for_self_import;
18+
use wildcard_imports_helper::inner::inner_for_self_import::inner_extern_bar;
19+
use wildcard_imports_helper::{ExternA, extern_foo};
20+
21+
mod fn_mod {
22+
pub fn foo() {}
23+
}
24+
25+
mod mod_mod {
26+
pub mod inner_mod {
27+
pub fn foo() {}
28+
}
29+
}
30+
31+
mod multi_fn_mod {
32+
pub fn multi_foo() {}
33+
pub fn multi_bar() {}
34+
pub fn multi_baz() {}
35+
pub mod multi_inner_mod {
36+
pub fn foo() {}
37+
}
38+
}
39+
40+
mod struct_mod {
41+
pub struct A;
42+
pub struct B;
43+
pub mod inner_struct_mod {
44+
pub struct C;
45+
}
46+
47+
#[macro_export]
48+
macro_rules! double_struct_import_test {
49+
() => {
50+
let _ = A;
51+
};
52+
}
53+
}
54+
55+
fn main() {
56+
foo();
57+
multi_foo();
58+
multi_bar();
59+
multi_inner_mod::foo();
60+
inner_mod::foo();
61+
extern_foo();
62+
inner_extern_bar();
63+
64+
let _ = A;
65+
let _ = inner_struct_mod::C;
66+
let _ = ExternA;
67+
68+
double_struct_import_test!();
69+
double_struct_import_test!();
70+
}
71+
72+
mod in_fn_test {
73+
pub use self::inner_exported::*;
74+
#[allow(unused_imports)]
75+
pub(crate) use self::inner_exported2::*;
76+
77+
fn test_intern() {
78+
use crate::fn_mod::foo;
79+
80+
foo();
81+
}
82+
83+
fn test_extern() {
84+
use wildcard_imports_helper::inner::inner_for_self_import::{self, inner_extern_foo};
85+
use wildcard_imports_helper::{ExternA, extern_foo};
86+
87+
inner_for_self_import::inner_extern_foo();
88+
inner_extern_foo();
89+
90+
extern_foo();
91+
92+
let _ = ExternA;
93+
}
94+
95+
fn test_inner_nested() {
96+
use self::{inner::inner_foo, inner2::inner_bar};
97+
98+
inner_foo();
99+
inner_bar();
100+
}
101+
102+
fn test_extern_reexported() {
103+
use wildcard_imports_helper::{ExternExportedEnum, ExternExportedStruct, extern_exported};
104+
105+
extern_exported();
106+
let _ = ExternExportedStruct;
107+
let _ = ExternExportedEnum::A;
108+
}
109+
110+
mod inner_exported {
111+
pub fn exported() {}
112+
pub struct ExportedStruct;
113+
pub enum ExportedEnum {
114+
A,
115+
}
116+
}
117+
118+
mod inner_exported2 {
119+
pub(crate) fn exported2() {}
120+
}
121+
122+
mod inner {
123+
pub fn inner_foo() {}
124+
}
125+
126+
mod inner2 {
127+
pub fn inner_bar() {}
128+
}
129+
}
130+
131+
fn test_reexported() {
132+
use crate::in_fn_test::{ExportedEnum, ExportedStruct, exported};
133+
134+
exported();
135+
let _ = ExportedStruct;
136+
let _ = ExportedEnum::A;
137+
}

tests/ui/wildcard_imports.rs

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// run-rustfix
2+
// aux-build:wildcard_imports_helper.rs
3+
4+
#![warn(clippy::wildcard_imports)]
5+
#![allow(unused)]
6+
#![warn(unused_imports)]
7+
8+
extern crate wildcard_imports_helper;
9+
10+
use crate::fn_mod::*;
11+
use crate::mod_mod::*;
12+
use crate::multi_fn_mod::*;
13+
#[macro_use]
14+
use crate::struct_mod::*;
15+
16+
#[allow(unused_imports)]
17+
use wildcard_imports_helper::inner::inner_for_self_import;
18+
use wildcard_imports_helper::inner::inner_for_self_import::*;
19+
use wildcard_imports_helper::*;
20+
21+
mod fn_mod {
22+
pub fn foo() {}
23+
}
24+
25+
mod mod_mod {
26+
pub mod inner_mod {
27+
pub fn foo() {}
28+
}
29+
}
30+
31+
mod multi_fn_mod {
32+
pub fn multi_foo() {}
33+
pub fn multi_bar() {}
34+
pub fn multi_baz() {}
35+
pub mod multi_inner_mod {
36+
pub fn foo() {}
37+
}
38+
}
39+
40+
mod struct_mod {
41+
pub struct A;
42+
pub struct B;
43+
pub mod inner_struct_mod {
44+
pub struct C;
45+
}
46+
47+
#[macro_export]
48+
macro_rules! double_struct_import_test {
49+
() => {
50+
let _ = A;
51+
};
52+
}
53+
}
54+
55+
fn main() {
56+
foo();
57+
multi_foo();
58+
multi_bar();
59+
multi_inner_mod::foo();
60+
inner_mod::foo();
61+
extern_foo();
62+
inner_extern_bar();
63+
64+
let _ = A;
65+
let _ = inner_struct_mod::C;
66+
let _ = ExternA;
67+
68+
double_struct_import_test!();
69+
double_struct_import_test!();
70+
}
71+
72+
mod in_fn_test {
73+
pub use self::inner_exported::*;
74+
#[allow(unused_imports)]
75+
pub(crate) use self::inner_exported2::*;
76+
77+
fn test_intern() {
78+
use crate::fn_mod::*;
79+
80+
foo();
81+
}
82+
83+
fn test_extern() {
84+
use wildcard_imports_helper::inner::inner_for_self_import::{self, *};
85+
use wildcard_imports_helper::*;
86+
87+
inner_for_self_import::inner_extern_foo();
88+
inner_extern_foo();
89+
90+
extern_foo();
91+
92+
let _ = ExternA;
93+
}
94+
95+
fn test_inner_nested() {
96+
use self::{inner::*, inner2::*};
97+
98+
inner_foo();
99+
inner_bar();
100+
}
101+
102+
fn test_extern_reexported() {
103+
use wildcard_imports_helper::*;
104+
105+
extern_exported();
106+
let _ = ExternExportedStruct;
107+
let _ = ExternExportedEnum::A;
108+
}
109+
110+
mod inner_exported {
111+
pub fn exported() {}
112+
pub struct ExportedStruct;
113+
pub enum ExportedEnum {
114+
A,
115+
}
116+
}
117+
118+
mod inner_exported2 {
119+
pub(crate) fn exported2() {}
120+
}
121+
122+
mod inner {
123+
pub fn inner_foo() {}
124+
}
125+
126+
mod inner2 {
127+
pub fn inner_bar() {}
128+
}
129+
}
130+
131+
fn test_reexported() {
132+
use crate::in_fn_test::*;
133+
134+
exported();
135+
let _ = ExportedStruct;
136+
let _ = ExportedEnum::A;
137+
}

tests/ui/wildcard_imports.stderr

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
error: usage of wildcard import
2+
--> $DIR/wildcard_imports.rs:10:5
3+
|
4+
LL | use crate::fn_mod::*;
5+
| ^^^^^^^^^^^^^^^^ help: try: `crate::fn_mod::foo`
6+
|
7+
= note: `-D clippy::wildcard-imports` implied by `-D warnings`
8+
9+
error: usage of wildcard import
10+
--> $DIR/wildcard_imports.rs:11:5
11+
|
12+
LL | use crate::mod_mod::*;
13+
| ^^^^^^^^^^^^^^^^^ help: try: `crate::mod_mod::inner_mod`
14+
15+
error: usage of wildcard import
16+
--> $DIR/wildcard_imports.rs:12:5
17+
|
18+
LL | use crate::multi_fn_mod::*;
19+
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `crate::multi_fn_mod::{multi_bar, multi_foo, multi_inner_mod}`
20+
21+
error: usage of wildcard import
22+
--> $DIR/wildcard_imports.rs:14:5
23+
|
24+
LL | use crate::struct_mod::*;
25+
| ^^^^^^^^^^^^^^^^^^^^ help: try: `crate::struct_mod::{A, inner_struct_mod}`
26+
27+
error: usage of wildcard import
28+
--> $DIR/wildcard_imports.rs:18:5
29+
|
30+
LL | use wildcard_imports_helper::inner::inner_for_self_import::*;
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `wildcard_imports_helper::inner::inner_for_self_import::inner_extern_bar`
32+
33+
error: usage of wildcard import
34+
--> $DIR/wildcard_imports.rs:19:5
35+
|
36+
LL | use wildcard_imports_helper::*;
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `wildcard_imports_helper::{ExternA, extern_foo}`
38+
39+
error: usage of wildcard import
40+
--> $DIR/wildcard_imports.rs:78:13
41+
|
42+
LL | use crate::fn_mod::*;
43+
| ^^^^^^^^^^^^^^^^ help: try: `crate::fn_mod::foo`
44+
45+
error: usage of wildcard import
46+
--> $DIR/wildcard_imports.rs:84:75
47+
|
48+
LL | use wildcard_imports_helper::inner::inner_for_self_import::{self, *};
49+
| ^ help: try: `inner_extern_foo`
50+
51+
error: usage of wildcard import
52+
--> $DIR/wildcard_imports.rs:85:13
53+
|
54+
LL | use wildcard_imports_helper::*;
55+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `wildcard_imports_helper::{ExternA, extern_foo}`
56+
57+
error: usage of wildcard import
58+
--> $DIR/wildcard_imports.rs:96:20
59+
|
60+
LL | use self::{inner::*, inner2::*};
61+
| ^^^^^^^^ help: try: `inner::inner_foo`
62+
63+
error: usage of wildcard import
64+
--> $DIR/wildcard_imports.rs:96:30
65+
|
66+
LL | use self::{inner::*, inner2::*};
67+
| ^^^^^^^^^ help: try: `inner2::inner_bar`
68+
69+
error: usage of wildcard import
70+
--> $DIR/wildcard_imports.rs:103:13
71+
|
72+
LL | use wildcard_imports_helper::*;
73+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `wildcard_imports_helper::{ExternExportedEnum, ExternExportedStruct, extern_exported}`
74+
75+
error: usage of wildcard import
76+
--> $DIR/wildcard_imports.rs:132:9
77+
|
78+
LL | use crate::in_fn_test::*;
79+
| ^^^^^^^^^^^^^^^^^^^^ help: try: `crate::in_fn_test::{ExportedEnum, ExportedStruct, exported}`
80+
81+
error: aborting due to 13 previous errors
82+

0 commit comments

Comments
 (0)