summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/missing_asserts_for_indexing.rs
blob: 0b4b883acf8ac38e4d9efec66d7f9944878995e1 (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
#![allow(unused)]
#![warn(clippy::missing_asserts_for_indexing)]

// ok
fn sum_with_assert(v: &[u8]) -> u8 {
    assert!(v.len() > 4);
    v[0] + v[1] + v[2] + v[3] + v[4]
}

// ok
fn sum_with_assert_other_way(v: &[u8]) -> u8 {
    assert!(5 <= v.len());
    v[0] + v[1] + v[2] + v[3] + v[4]
}

// ok
fn sum_with_assert_ge(v: &[u8]) -> u8 {
    assert!(v.len() >= 5);
    v[0] + v[1] + v[2] + v[3] + v[4]
}

// ok
fn sum_with_assert_ge_other_way(v: &[u8]) -> u8 {
    assert!(4 < v.len());
    v[0] + v[1] + v[2] + v[3] + v[4]
}

fn sum_with_assert_lt(v: &[u8]) -> u8 {
    assert!(v.len() < 5);
    v[0] + v[1] + v[2] + v[3] + v[4]
    //~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
}

fn sum_with_assert_le(v: &[u8]) -> u8 {
    assert!(v.len() <= 5);
    v[0] + v[1] + v[2] + v[3] + v[4]
    //~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
}

fn sum_with_incorrect_assert_len(v: &[u8]) -> u8 {
    assert!(v.len() > 3);
    v[0] + v[1] + v[2] + v[3] + v[4]
    //~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
}

fn sum_with_incorrect_assert_len2(v: &[u8]) -> u8 {
    assert!(v.len() >= 4);
    v[0] + v[1] + v[2] + v[3] + v[4]
    //~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
}

// ok, don't lint for single array access
fn single_access(v: &[u8]) -> u8 {
    v[0]
}

// ok
fn subslice_ok(v: &[u8]) {
    assert!(v.len() > 3);
    let _ = v[0];
    let _ = v[1..4];
}

fn subslice_bad(v: &[u8]) {
    assert!(v.len() >= 3);
    let _ = v[0];
    //~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
    let _ = v[1..4];
}

// ok
fn subslice_inclusive_ok(v: &[u8]) {
    assert!(v.len() > 4);
    let _ = v[0];
    let _ = v[1..=4];
}

fn subslice_inclusive_bad(v: &[u8]) {
    assert!(v.len() >= 4);
    let _ = v[0];
    //~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
    let _ = v[1..=4];
}

fn index_different_slices_ok(v1: &[u8], v2: &[u8]) {
    assert!(v1.len() > 12);
    assert!(v2.len() > 15);
    let _ = v1[0] + v1[12];
    let _ = v2[5] + v2[15];
}

fn index_different_slices_wrong_len(v1: &[u8], v2: &[u8]) {
    assert!(v1.len() >= 12);
    assert!(v2.len() >= 15);
    let _ = v1[0] + v1[12];
    //~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
    let _ = v2[5] + v2[15];
    //~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
}
fn index_different_slices_one_wrong_len(v1: &[u8], v2: &[u8]) {
    assert!(v1.len() >= 12);
    assert!(v2.len() > 15);
    let _ = v1[0] + v1[12];
    //~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
    let _ = v2[5] + v2[15];
}

fn side_effect() -> &'static [u8] {
    &[]
}

fn index_side_effect_expr() {
    let _ = side_effect()[0] + side_effect()[1];
}

// ok, single access for different slices
fn index_different_slice_in_same_expr(v1: &[u8], v2: &[u8]) {
    let _ = v1[0] + v2[1];
}

fn main() {}