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