summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/no_effect.rs
blob: c52f4389192fd8a12f7461f48902782b2c46f528 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#![feature(fn_traits, unboxed_closures)]
#![warn(clippy::no_effect_underscore_binding)]
#![allow(dead_code, path_statements)]
#![allow(
    clippy::deref_addrof,
    clippy::redundant_field_names,
    clippy::uninlined_format_args,
    clippy::unnecessary_struct_initialization,
    clippy::useless_vec
)]

struct Unit;
struct Tuple(i32);
struct Struct {
    field: i32,
}
enum Enum {
    Tuple(i32),
    Struct { field: i32 },
}
struct DropUnit;
impl Drop for DropUnit {
    fn drop(&mut self) {}
}
struct DropStruct {
    field: i32,
}
impl Drop for DropStruct {
    fn drop(&mut self) {}
}
struct DropTuple(i32);
impl Drop for DropTuple {
    fn drop(&mut self) {}
}
enum DropEnum {
    Tuple(i32),
    Struct { field: i32 },
}
impl Drop for DropEnum {
    fn drop(&mut self) {}
}
struct FooString {
    s: String,
}
union Union {
    a: u8,
    b: f64,
}

fn get_number() -> i32 {
    0
}
fn get_struct() -> Struct {
    Struct { field: 0 }
}
fn get_drop_struct() -> DropStruct {
    DropStruct { field: 0 }
}

unsafe fn unsafe_fn() -> i32 {
    0
}

struct GreetStruct1;

impl FnOnce<(&str,)> for GreetStruct1 {
    type Output = ();

    extern "rust-call" fn call_once(self, (who,): (&str,)) -> Self::Output {
        println!("hello {}", who);
    }
}

struct GreetStruct2();

impl FnOnce<(&str,)> for GreetStruct2 {
    type Output = ();

    extern "rust-call" fn call_once(self, (who,): (&str,)) -> Self::Output {
        println!("hello {}", who);
    }
}

struct GreetStruct3;

impl FnOnce<(&str,)> for GreetStruct3 {
    type Output = ();

    extern "rust-call" fn call_once(self, (who,): (&str,)) -> Self::Output {
        println!("hello {}", who);
    }
}

fn main() {
    let s = get_struct();
    let s2 = get_struct();

    0;
    //~^ ERROR: statement with no effect
    //~| NOTE: `-D clippy::no-effect` implied by `-D warnings`
    s2;
    //~^ ERROR: statement with no effect
    Unit;
    //~^ ERROR: statement with no effect
    Tuple(0);
    //~^ ERROR: statement with no effect
    Struct { field: 0 };
    //~^ ERROR: statement with no effect
    Struct { ..s };
    //~^ ERROR: statement with no effect
    Union { a: 0 };
    //~^ ERROR: statement with no effect
    Enum::Tuple(0);
    //~^ ERROR: statement with no effect
    Enum::Struct { field: 0 };
    //~^ ERROR: statement with no effect
    5 + 6;
    //~^ ERROR: statement with no effect
    *&42;
    //~^ ERROR: statement with no effect
    &6;
    //~^ ERROR: statement with no effect
    (5, 6, 7);
    //~^ ERROR: statement with no effect
    ..;
    //~^ ERROR: statement with no effect
    5..;
    //~^ ERROR: statement with no effect
    ..5;
    //~^ ERROR: statement with no effect
    5..6;
    //~^ ERROR: statement with no effect
    5..=6;
    //~^ ERROR: statement with no effect
    [42, 55];
    //~^ ERROR: statement with no effect
    [42, 55][1];
    //~^ ERROR: statement with no effect
    (42, 55).1;
    //~^ ERROR: statement with no effect
    [42; 55];
    //~^ ERROR: statement with no effect
    [42; 55][13];
    //~^ ERROR: statement with no effect
    let mut x = 0;
    || x += 5;
    //~^ ERROR: statement with no effect
    let s: String = "foo".into();
    FooString { s: s };
    //~^ ERROR: statement with no effect
    let _unused = 1;
    //~^ ERROR: binding to `_` prefixed variable with no side-effect
    //~| NOTE: `-D clippy::no-effect-underscore-binding` implied by `-D warnings`
    let _penguin = || println!("Some helpful closure");
    //~^ ERROR: binding to `_` prefixed variable with no side-effect
    let _duck = Struct { field: 0 };
    //~^ ERROR: binding to `_` prefixed variable with no side-effect
    let _cat = [2, 4, 6, 8][2];
    //~^ ERROR: binding to `_` prefixed variable with no side-effect

    #[allow(clippy::no_effect)]
    0;

    // Do not warn
    get_number();
    unsafe { unsafe_fn() };
    let _used = get_struct();
    let _x = vec![1];
    DropUnit;
    DropStruct { field: 0 };
    DropTuple(0);
    DropEnum::Tuple(0);
    DropEnum::Struct { field: 0 };
    GreetStruct1("world");
    GreetStruct2()("world");
    GreetStruct3 {}("world");
}