summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/src/docs/indexing_slicing.txt
blob: 76ca6ed318b38b88ecbe7320e2c9c5d7eff761c3 (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
### What it does
Checks for usage of indexing or slicing. Arrays are special cases, this lint
does report on arrays if we can tell that slicing operations are in bounds and does not
lint on constant `usize` indexing on arrays because that is handled by rustc's `const_err` lint.

### Why is this bad?
Indexing and slicing can panic at runtime and there are
safe alternatives.

### Example
```
// Vector
let x = vec![0; 5];

x[2];
&x[2..100];

// Array
let y = [0, 1, 2, 3];

&y[10..100];
&y[10..];
```

Use instead:
```

x.get(2);
x.get(2..100);

y.get(10);
y.get(10..100);
```