]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/wildcard_imports.rs
Rollup merge of #100396 - chenyukang:fix-100394, r=petrochenkov
[rust.git] / src / tools / clippy / tests / ui / wildcard_imports.rs
1 // edition:2015
2 // run-rustfix
3 // aux-build:wildcard_imports_helper.rs
4
5 // the 2015 edition here is needed because edition 2018 changed the module system
6 // (see https://doc.rust-lang.org/edition-guide/rust-2018/path-changes.html) which means the lint
7 // no longer detects some of the cases starting with Rust 2018.
8 // FIXME: We should likely add another edition 2021 test case for this lint
9
10 #![warn(clippy::wildcard_imports)]
11 #![allow(unused, clippy::unnecessary_wraps, clippy::let_unit_value)]
12 #![warn(unused_imports)]
13
14 extern crate wildcard_imports_helper;
15
16 use crate::fn_mod::*;
17 use crate::mod_mod::*;
18 use crate::multi_fn_mod::*;
19 #[macro_use]
20 use crate::struct_mod::*;
21
22 #[allow(unused_imports)]
23 use wildcard_imports_helper::inner::inner_for_self_import;
24 use wildcard_imports_helper::inner::inner_for_self_import::*;
25 use wildcard_imports_helper::*;
26
27 use std::io::prelude::*;
28 use wildcard_imports_helper::prelude::v1::*;
29
30 struct ReadFoo;
31
32 impl Read for ReadFoo {
33     fn read(&mut self, _buf: &mut [u8]) -> std::io::Result<usize> {
34         Ok(0)
35     }
36 }
37
38 mod fn_mod {
39     pub fn foo() {}
40 }
41
42 mod mod_mod {
43     pub mod inner_mod {
44         pub fn foo() {}
45     }
46 }
47
48 mod multi_fn_mod {
49     pub fn multi_foo() {}
50     pub fn multi_bar() {}
51     pub fn multi_baz() {}
52     pub mod multi_inner_mod {
53         pub fn foo() {}
54     }
55 }
56
57 mod struct_mod {
58     pub struct A;
59     pub struct B;
60     pub mod inner_struct_mod {
61         pub struct C;
62     }
63
64     #[macro_export]
65     macro_rules! double_struct_import_test {
66         () => {
67             let _ = A;
68         };
69     }
70 }
71
72 fn main() {
73     foo();
74     multi_foo();
75     multi_bar();
76     multi_inner_mod::foo();
77     inner_mod::foo();
78     extern_foo();
79     inner_extern_bar();
80
81     let _ = A;
82     let _ = inner_struct_mod::C;
83     let _ = ExternA;
84     let _ = PreludeModAnywhere;
85
86     double_struct_import_test!();
87     double_struct_import_test!();
88 }
89
90 mod in_fn_test {
91     pub use self::inner_exported::*;
92     #[allow(unused_imports)]
93     pub(crate) use self::inner_exported2::*;
94
95     fn test_intern() {
96         use crate::fn_mod::*;
97
98         foo();
99     }
100
101     fn test_extern() {
102         use wildcard_imports_helper::inner::inner_for_self_import::{self, *};
103         use wildcard_imports_helper::*;
104
105         inner_for_self_import::inner_extern_foo();
106         inner_extern_foo();
107
108         extern_foo();
109
110         let _ = ExternA;
111     }
112
113     fn test_inner_nested() {
114         use self::{inner::*, inner2::*};
115
116         inner_foo();
117         inner_bar();
118     }
119
120     fn test_extern_reexported() {
121         use wildcard_imports_helper::*;
122
123         extern_exported();
124         let _ = ExternExportedStruct;
125         let _ = ExternExportedEnum::A;
126     }
127
128     mod inner_exported {
129         pub fn exported() {}
130         pub struct ExportedStruct;
131         pub enum ExportedEnum {
132             A,
133         }
134     }
135
136     mod inner_exported2 {
137         pub(crate) fn exported2() {}
138     }
139
140     mod inner {
141         pub fn inner_foo() {}
142     }
143
144     mod inner2 {
145         pub fn inner_bar() {}
146     }
147 }
148
149 fn test_reexported() {
150     use crate::in_fn_test::*;
151
152     exported();
153     let _ = ExportedStruct;
154     let _ = ExportedEnum::A;
155 }
156
157 #[rustfmt::skip]
158 fn test_weird_formatting() {
159     use crate:: in_fn_test::  * ;
160     use crate:: fn_mod::
161         *;
162
163     exported();
164     foo();
165 }
166
167 mod super_imports {
168     fn foofoo() {}
169
170     mod should_be_replaced {
171         use super::*;
172
173         fn with_super() {
174             let _ = foofoo();
175         }
176     }
177
178     mod test_should_pass {
179         use super::*;
180
181         fn with_super() {
182             let _ = foofoo();
183         }
184     }
185
186     mod test_should_pass_inside_function {
187         fn with_super_inside_function() {
188             use super::*;
189             let _ = foofoo();
190         }
191     }
192
193     mod test_should_pass_further_inside {
194         fn insidefoo() {}
195         mod inner {
196             use super::*;
197             fn with_super() {
198                 let _ = insidefoo();
199             }
200         }
201     }
202
203     mod should_be_replaced_further_inside {
204         fn insidefoo() {}
205         mod inner {
206             use super::*;
207             fn with_super() {
208                 let _ = insidefoo();
209             }
210         }
211     }
212
213     mod use_explicit_should_be_replaced {
214         use super_imports::*;
215
216         fn with_explicit() {
217             let _ = foofoo();
218         }
219     }
220
221     mod use_double_super_should_be_replaced {
222         mod inner {
223             use super::super::*;
224
225             fn with_double_super() {
226                 let _ = foofoo();
227             }
228         }
229     }
230
231     mod use_super_explicit_should_be_replaced {
232         use super::super::super_imports::*;
233
234         fn with_super_explicit() {
235             let _ = foofoo();
236         }
237     }
238
239     mod attestation_should_be_replaced {
240         use super::*;
241
242         fn with_explicit() {
243             let _ = foofoo();
244         }
245     }
246 }