about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/const_is_empty.rs
blob: 8bb4f0e5d975073012df686ec78bc9cac4dfb8e6 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#![warn(clippy::const_is_empty)]
#![allow(clippy::needless_late_init, unused_must_use)]

fn test_literal() {
    if "".is_empty() {
        //~^ const_is_empty
    }
    if "foobar".is_empty() {
        //~^ const_is_empty
    }
}

fn test_byte_literal() {
    if b"".is_empty() {
        //~^ const_is_empty
    }
    if b"foobar".is_empty() {
        //~^ const_is_empty
    }
}

fn test_no_mut() {
    let mut empty = "";
    if empty.is_empty() {
        // No lint because it is mutable
    }
}

fn test_propagated() {
    let empty = "";
    let non_empty = "foobar";
    let empty2 = empty;
    let non_empty2 = non_empty;
    if empty2.is_empty() {
        //~^ const_is_empty
    }
    if non_empty2.is_empty() {
        //~^ const_is_empty
    }
}

const EMPTY_STR: &str = "";
const NON_EMPTY_STR: &str = "foo";
const EMPTY_BSTR: &[u8] = b"";
const NON_EMPTY_BSTR: &[u8] = b"foo";
const EMPTY_U8_SLICE: &[u8] = &[];
const NON_EMPTY_U8_SLICE: &[u8] = &[1, 2];
const EMPTY_SLICE: &[u32] = &[];
const NON_EMPTY_SLICE: &[u32] = &[1, 2];
const NON_EMPTY_SLICE_REPEAT: &[u32] = &[1; 2];
const EMPTY_ARRAY: [u32; 0] = [];
const EMPTY_ARRAY_REPEAT: [u32; 0] = [1; 0];
const NON_EMPTY_ARRAY: [u32; 2] = [1, 2];
const NON_EMPTY_ARRAY_REPEAT: [u32; 2] = [1; 2];
const EMPTY_REF_ARRAY: &[u32; 0] = &[];
const NON_EMPTY_REF_ARRAY: &[u32; 3] = &[1, 2, 3];

fn test_from_const() {
    let _ = EMPTY_STR.is_empty();
    //~^ const_is_empty

    let _ = NON_EMPTY_STR.is_empty();
    //~^ const_is_empty

    let _ = EMPTY_BSTR.is_empty();
    //~^ const_is_empty

    let _ = NON_EMPTY_BSTR.is_empty();
    //~^ const_is_empty

    let _ = EMPTY_ARRAY.is_empty();
    //~^ const_is_empty

    let _ = EMPTY_ARRAY_REPEAT.is_empty();
    //~^ const_is_empty

    let _ = EMPTY_U8_SLICE.is_empty();
    //~^ const_is_empty

    let _ = NON_EMPTY_U8_SLICE.is_empty();
    //~^ const_is_empty

    let _ = NON_EMPTY_ARRAY.is_empty();
    //~^ const_is_empty

    let _ = NON_EMPTY_ARRAY_REPEAT.is_empty();
    //~^ const_is_empty

    let _ = EMPTY_REF_ARRAY.is_empty();
    //~^ const_is_empty

    let _ = NON_EMPTY_REF_ARRAY.is_empty();
    //~^ const_is_empty

    let _ = EMPTY_SLICE.is_empty();
    //~^ const_is_empty

    let _ = NON_EMPTY_SLICE.is_empty();
    //~^ const_is_empty

    let _ = NON_EMPTY_SLICE_REPEAT.is_empty();
    //~^ const_is_empty
}

fn main() {
    let value = "foobar";
    let _ = value.is_empty();
    //~^ const_is_empty

    let x = value;
    let _ = x.is_empty();
    //~^ const_is_empty

    let _ = "".is_empty();
    //~^ const_is_empty

    let _ = b"".is_empty();
    //~^ const_is_empty
}

fn str_from_arg(var: &str) {
    var.is_empty();
    // Do not lint, we know nothiny about var
}

fn update_str() {
    let mut value = "duck";
    value = "penguin";

    let _ = value.is_empty();
    // Do not lint since value is mutable
}

fn macros() {
    // Content from Macro
    let file = include_str!("const_is_empty.rs");
    let _ = file.is_empty();
    // No lint because initializer comes from a macro result

    let var = env!("PATH");
    let _ = var.is_empty();
    // No lint because initializer comes from a macro result
}

fn conditional_value() {
    let value;

    if true {
        value = "hey";
    } else {
        value = "hej";
    }

    let _ = value.is_empty();
    // Do not lint, current constant folding is too simple to detect this
}

fn cfg_conditioned() {
    #[cfg(test)]
    let val = "";
    #[cfg(not(test))]
    let val = "foo";

    let _ = val.is_empty();
    // Do not lint, value depend on a #[cfg(…)] directive
}

fn not_cfg_conditioned() {
    let val = "";
    #[cfg(not(target_os = "inexistent"))]
    let _ = val.is_empty();
    //~^ const_is_empty
}

const fn const_rand() -> &'static str {
    "17"
}

fn const_expressions() {
    let _ = const { if true { "1" } else { "2" } }.is_empty();
    // Do not lint, we do not recurse into boolean expressions

    let _ = const_rand().is_empty();
    // Do not lint, we do not recurse into functions
}

fn constant_from_external_crate() {
    let _ = std::env::consts::EXE_EXTENSION.is_empty();
    // Do not lint, `exe_ext` comes from the `std` crate
}

fn issue_13106() {
    const {
        assert!(!NON_EMPTY_STR.is_empty());
    }

    const {
        assert!(EMPTY_STR.is_empty());
    }

    const {
        EMPTY_STR.is_empty();
        //~^ const_is_empty
    }
}