about summary refs log tree commit diff
diff options
context:
space:
mode:
authordswij <dharmasw@outlook.com>2024-12-14 05:21:29 +0000
committerGitHub <noreply@github.com>2024-12-14 05:21:29 +0000
commitbfb87b9d3448d87c8dd8d833287d817a78292f47 (patch)
treec825d245a3a7d9cbd140b43ff725b5032569e548
parent13463cb072e1ae70677329931b434b4070710e2a (diff)
parente767daa45e041e7c9d9f9aaa4cae4081548a7df5 (diff)
downloadrust-bfb87b9d3448d87c8dd8d833287d817a78292f47.tar.gz
rust-bfb87b9d3448d87c8dd8d833287d817a78292f47.zip
indexing_slicing: Clarify documentation (#13780)
changelog: [`indexing_slicing`]: Clarify the relationship between
indexing_slicing and out_of_bound_indexing, clarify that this lint is
about possible panics based on runtime values, and fix array example to
not trigger the out_of_bound_indexing lint.
-rw-r--r--clippy_lints/src/indexing_slicing.rs27
1 files changed, 17 insertions, 10 deletions
diff --git a/clippy_lints/src/indexing_slicing.rs b/clippy_lints/src/indexing_slicing.rs
index 15203b35b13..58346f78013 100644
--- a/clippy_lints/src/indexing_slicing.rs
+++ b/clippy_lints/src/indexing_slicing.rs
@@ -42,12 +42,15 @@ declare_clippy_lint! {
 
 declare_clippy_lint! {
     /// ### 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.
+    /// Checks for usage of indexing or slicing that may panic at runtime.
+    ///
+    /// This lint does not report on indexing or slicing operations
+    /// that always panic, clippy's `out_of_bound_indexing` already
+    /// handles those cases.
     ///
     /// ### Why restrict this?
     /// To avoid implicit panics from indexing and slicing.
+    ///
     /// There are “checked” alternatives which do not panic, and can be used with `unwrap()` to make
     /// an explicit panic when it is desired.
     ///
@@ -58,27 +61,31 @@ declare_clippy_lint! {
     /// ### Example
     /// ```rust,no_run
     /// // Vector
-    /// let x = vec![0; 5];
+    /// let x = vec![0, 1, 2, 3];
     ///
     /// x[2];
+    /// x[100];
     /// &x[2..100];
     ///
     /// // Array
     /// let y = [0, 1, 2, 3];
     ///
-    /// &y[10..100];
-    /// &y[10..];
+    /// let i = 10; // Could be a runtime value
+    /// let j = 20;
+    /// &y[i..j];
     /// ```
     ///
     /// Use instead:
     /// ```no_run
-    /// # let x = vec![0; 5];
-    /// # let y = [0, 1, 2, 3];
+    /// # let x = vec![0, 1, 2, 3];
     /// x.get(2);
+    /// x.get(100);
     /// x.get(2..100);
     ///
-    /// y.get(10);
-    /// y.get(10..100);
+    /// # let y = [0, 1, 2, 3];
+    /// let i = 10;
+    /// let j = 20;
+    /// y.get(i..j);
     /// ```
     #[clippy::version = "pre 1.29.0"]
     pub INDEXING_SLICING,