]> git.lizzy.rs Git - rust.git/blob - src/test/ui/lint/lint-unused-imports.rs
5bb2ab75c53fd42a8fadd9b43920b94e87e0d216
[rust.git] / src / test / ui / lint / lint-unused-imports.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![deny(unused_imports)]
12 #![allow(dead_code)]
13
14 use bar::c::cc as cal;
15
16 use std::mem::*;            // shouldn't get errors for not using
17                             // everything imported
18 use std::fmt::{};
19 //~^ ERROR unused import: `use std::fmt::{};`
20
21 // Should get errors for both 'Some' and 'None'
22 use std::option::Option::{Some, None};
23 //~^ ERROR unused imports: `None`, `Some`
24
25 use test::A;       //~ ERROR unused import: `test::A`
26 // Be sure that if we just bring some methods into scope that they're also
27 // counted as being used.
28 use test::B;
29 // But only when actually used: do not get confused by the method with the same name.
30 use test::B2; //~ ERROR unused import: `test::B2`
31
32 // Make sure this import is warned about when at least one of its imported names
33 // is unused
34 use test2::{foo, bar}; //~ ERROR unused import: `bar`
35
36 mod test2 {
37     pub fn foo() {}
38     pub fn bar() {}
39 }
40
41 mod test {
42     pub trait A { fn a(&self) {} }
43     pub trait B { fn b(&self) {} }
44     pub trait B2 { fn b(&self) {} }
45     pub struct C;
46     impl A for C {}
47     impl B for C {}
48 }
49
50 mod foo {
51     pub struct Point{pub x: isize, pub y: isize}
52     pub struct Square{pub p: Point, pub h: usize, pub w: usize}
53 }
54
55 mod bar {
56     // Don't ignore on 'pub use' because we're not sure if it's used or not
57     pub use std::cmp::PartialEq;
58     pub struct Square;
59
60     pub mod c {
61         use foo::Point;
62         use foo::Square; //~ ERROR unused import: `foo::Square`
63         pub fn cc(_p: Point) -> super::Square {
64             fn f() -> super::Square {
65                 super::Square
66             }
67             f()
68         }
69     }
70
71     #[allow(unused_imports)]
72     mod foo {
73         use std::cmp::PartialEq;
74     }
75 }
76
77 fn g() {
78     use self::g; //~ ERROR unused import: `self::g`
79     fn f() {
80         self::g();
81     }
82 }
83
84 // c.f. issue #35135
85 #[allow(unused_variables)]
86 fn h() {
87     use test2::foo; //~ ERROR unused import: `test2::foo`
88     let foo = 0;
89 }
90
91 fn main() {
92     cal(foo::Point{x:3, y:9});
93     let mut a = 3;
94     let mut b = 4;
95     swap(&mut a, &mut b);
96     test::C.b();
97     let _a = foo();
98 }