diff options
Diffstat (limited to 'src/tools/clippy/src/docs/index_refutable_slice.txt')
-rw-r--r-- | src/tools/clippy/src/docs/index_refutable_slice.txt | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/tools/clippy/src/docs/index_refutable_slice.txt b/src/tools/clippy/src/docs/index_refutable_slice.txt new file mode 100644 index 000000000..8a7d52761 --- /dev/null +++ b/src/tools/clippy/src/docs/index_refutable_slice.txt @@ -0,0 +1,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); +} +```
\ No newline at end of file |