summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/src/docs/indexing_slicing.txt
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools/clippy/src/docs/indexing_slicing.txt')
-rw-r--r--src/tools/clippy/src/docs/indexing_slicing.txt33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/tools/clippy/src/docs/indexing_slicing.txt b/src/tools/clippy/src/docs/indexing_slicing.txt
new file mode 100644
index 000000000..76ca6ed31
--- /dev/null
+++ b/src/tools/clippy/src/docs/indexing_slicing.txt
@@ -0,0 +1,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);
+``` \ No newline at end of file