about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/manual_slice_fill.rs
blob: c74ab2225c0a4d712dac011379994b2be1db55a5 (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
#![warn(clippy::manual_slice_fill)]
#![allow(clippy::needless_range_loop, clippy::useless_vec)]

macro_rules! assign_element {
    ($slice:ident, $index:expr) => {
        $slice[$index] = 0;
    };
}

macro_rules! assign_element_2 {
    ($i:expr) => {
        $i = 0;
    };
}

struct NoClone;

fn num() -> usize {
    5
}

fn should_lint() {
    let mut some_slice = [1, 2, 3, 4, 5];

    for i in 0..some_slice.len() {
        //~^ manual_slice_fill
        some_slice[i] = 0;
    }

    let x = 5;
    for i in 0..some_slice.len() {
        //~^ manual_slice_fill
        some_slice[i] = x;
    }

    for i in &mut some_slice {
        //~^ manual_slice_fill
        *i = 0;
    }

    // This should trigger `manual_slice_fill`, but the applicability is `MaybeIncorrect` since comments
    // within the loop might be purely informational.
    for i in 0..some_slice.len() {
        //~^ manual_slice_fill
        some_slice[i] = 0;
        // foo
    }
}

fn should_not_lint() {
    let mut some_slice = [1, 2, 3, 4, 5];

    // Should not lint because we can't determine if the scope of the loop is intended to access all the
    // elements of the slice.
    for i in 0..5 {
        some_slice[i] = 0;
    }

    // Should not lint, as using a function to assign values to elements might be
    // intentional. For example, the contents of `num()` could be temporary and subject to change
    // later.
    for i in 0..some_slice.len() {
        some_slice[i] = num();
    }

    // Should not lint because this loop isn't equivalent to `fill`.
    for i in 0..some_slice.len() {
        some_slice[i] = 0;
        println!("foo");
    }

    // Should not lint because it may be intentional to use a macro to perform an operation equivalent
    // to `fill`.
    for i in 0..some_slice.len() {
        assign_element!(some_slice, i);
    }

    let another_slice = [1, 2, 3];
    // Should not lint because the range is not for `some_slice`.
    for i in 0..another_slice.len() {
        some_slice[i] = 0;
    }

    let mut vec: Vec<Option<NoClone>> = Vec::with_capacity(5);
    // Should not lint because `NoClone` does not have `Clone` trait.
    for i in 0..vec.len() {
        vec[i] = None;
    }

    // Should not lint, as using a function to assign values to elements might be
    // intentional. For example, the contents of `num()` could be temporary and subject to change
    // later.
    for i in &mut some_slice {
        *i = num();
    }

    // Should not lint because this loop isn't equivalent to `fill`.
    for i in &mut some_slice {
        *i = 0;
        println!("foo");
    }

    // Should not lint because it may be intentional to use a macro to perform an operation equivalent
    // to `fill`.
    for i in &mut some_slice {
        assign_element_2!(*i);
    }

    let mut vec: Vec<Option<NoClone>> = Vec::with_capacity(5);
    // Should not lint because `NoClone` does not have `Clone` trait.
    for i in &mut vec {
        *i = None;
    }
}

fn issue_14192() {
    let mut tmp = vec![0; 3];

    for i in 0..tmp.len() {
        tmp[i] = i;
    }

    for i in 0..tmp.len() {
        tmp[i] = 2 + i;
    }

    for i in 0..tmp.len() {
        tmp[0] = i;
    }
}

fn issue14189() {
    // Should not lint because `!*b` is not constant
    let mut tmp = vec![1, 2, 3];
    for b in &mut tmp {
        *b = !*b;
    }
}

mod issue14685 {
    use std::ops::{Index, IndexMut};

    #[derive(Clone)]
    struct ZipList<T>(T);

    impl<T> ZipList<T> {
        fn len(&self) -> usize {
            todo!()
        }

        fn is_empty(&self) -> bool {
            todo!()
        }
    }

    impl<T> Index<usize> for ZipList<T> {
        type Output = T;

        fn index(&self, _: usize) -> &Self::Output {
            todo!()
        }
    }

    impl<T> IndexMut<usize> for ZipList<T> {
        fn index_mut(&mut self, _: usize) -> &mut Self::Output {
            todo!()
        }
    }

    fn index_mut(mut zl: ZipList<usize>) {
        for i in 0..zl.len() {
            zl[i] = 6;
        }
    }
}