summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/src/docs/index_refutable_slice.txt
blob: 8a7d52761af816657c32bee2884c333b81b153e1 (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
### What it does
The lint checks for slice bindings in patterns that are only used to
access individual slice values.

### Why is this bad?
Accessing slice values using indices can lead to panics. Using refutable
patterns can avoid these. Binding to individual values also improves the
readability as they can be named.

### Limitations
This lint currently only checks for immutable access inside `if let`
patterns.

### Example
```
let slice: Option<&[u32]> = Some(&[1, 2, 3]);

if let Some(slice) = slice {
    println!("{}", slice[0]);
}
```
Use instead:
```
let slice: Option<&[u32]> = Some(&[1, 2, 3]);

if let Some(&[first, ..]) = slice {
    println!("{}", first);
}
```