summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/needless_range_loop.rs
blob: 3f2421953301ebc18dd0c0a4cdd590592fc76265 (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
#![warn(clippy::needless_range_loop)]
#![allow(
    clippy::uninlined_format_args,
    clippy::unnecessary_literal_unwrap,
    clippy::useless_vec
)]
//@no-rustfix
static STATIC: [usize; 4] = [0, 1, 8, 16];
const CONST: [usize; 4] = [0, 1, 8, 16];
const MAX_LEN: usize = 42;

fn main() {
    let mut vec = vec![1, 2, 3, 4];
    let vec2 = vec![1, 2, 3, 4];
    for i in 0..vec.len() {
        //~^ ERROR: the loop variable `i` is only used to index `vec`
        //~| NOTE: `-D clippy::needless-range-loop` implied by `-D warnings`
        println!("{}", vec[i]);
    }

    for i in 0..vec.len() {
        let i = 42; // make a different `i`
        println!("{}", vec[i]); // ok, not the `i` of the for-loop
    }

    for i in 0..vec.len() {
        //~^ ERROR: the loop variable `i` is only used to index `vec`
        let _ = vec[i];
    }

    // ICE #746
    for j in 0..4 {
        //~^ ERROR: the loop variable `j` is only used to index `STATIC`
        println!("{:?}", STATIC[j]);
    }

    for j in 0..4 {
        //~^ ERROR: the loop variable `j` is only used to index `CONST`
        println!("{:?}", CONST[j]);
    }

    for i in 0..vec.len() {
        //~^ ERROR: the loop variable `i` is used to index `vec`
        println!("{} {}", vec[i], i);
    }
    for i in 0..vec.len() {
        // not an error, indexing more than one variable
        println!("{} {}", vec[i], vec2[i]);
    }

    for i in 0..vec.len() {
        //~^ ERROR: the loop variable `i` is only used to index `vec2`
        println!("{}", vec2[i]);
    }

    for i in 5..vec.len() {
        //~^ ERROR: the loop variable `i` is only used to index `vec`
        println!("{}", vec[i]);
    }

    for i in 0..MAX_LEN {
        //~^ ERROR: the loop variable `i` is only used to index `vec`
        println!("{}", vec[i]);
    }

    for i in 0..=MAX_LEN {
        //~^ ERROR: the loop variable `i` is only used to index `vec`
        println!("{}", vec[i]);
    }

    for i in 5..10 {
        //~^ ERROR: the loop variable `i` is only used to index `vec`
        println!("{}", vec[i]);
    }

    for i in 5..=10 {
        //~^ ERROR: the loop variable `i` is only used to index `vec`
        println!("{}", vec[i]);
    }

    for i in 5..vec.len() {
        //~^ ERROR: the loop variable `i` is used to index `vec`
        println!("{} {}", vec[i], i);
    }

    for i in 5..10 {
        //~^ ERROR: the loop variable `i` is used to index `vec`
        println!("{} {}", vec[i], i);
    }

    // #2542
    for i in 0..vec.len() {
        //~^ ERROR: the loop variable `i` is used to index `vec`
        vec[i] = Some(1).unwrap_or_else(|| panic!("error on {}", i));
    }

    // #3788
    let test = Test {
        inner: vec![1, 2, 3, 4],
    };
    for i in 0..2 {
        println!("{}", test[i]);
    }

    // See #601
    for i in 0..10 {
        // no error, id_col does not exist outside the loop
        let mut id_col = [0f64; 10];
        id_col[i] = 1f64;
    }

    fn f<T>(_: &T, _: &T) -> bool {
        unimplemented!()
    }
    fn g<T>(_: &mut [T], _: usize, _: usize) {
        unimplemented!()
    }
    for i in 1..vec.len() {
        if f(&vec[i - 1], &vec[i]) {
            g(&mut vec, i - 1, i);
        }
    }

    for mid in 1..vec.len() {
        let (_, _) = vec.split_at(mid);
    }
}

struct Test {
    inner: Vec<usize>,
}

impl std::ops::Index<usize> for Test {
    type Output = usize;
    fn index(&self, index: usize) -> &Self::Output {
        &self.inner[index]
    }
}

fn partition<T: PartialOrd + Send>(v: &mut [T]) -> usize {
    let pivot = v.len() - 1;
    let mut i = 0;
    for j in 0..pivot {
        if v[j] <= v[pivot] {
            v.swap(i, j);
            i += 1;
        }
    }
    v.swap(i, pivot);
    i
}

pub fn manual_copy_same_destination(dst: &mut [i32], d: usize, s: usize) {
    // Same source and destination - don't trigger lint
    for i in 0..dst.len() {
        dst[d + i] = dst[s + i];
    }
}

mod issue_2496 {
    pub trait Handle {
        fn new_for_index(index: usize) -> Self;
        fn index(&self) -> usize;
    }

    pub fn test<H: Handle>() -> H {
        for x in 0..5 {
            let next_handle = H::new_for_index(x);
            println!("{}", next_handle.index());
        }
        unimplemented!()
    }
}