Skip to content

Commit bfb87b9

Browse files
authored
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.
2 parents 13463cb + e767daa commit bfb87b9

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

clippy_lints/src/indexing_slicing.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,15 @@ declare_clippy_lint! {
4242

4343
declare_clippy_lint! {
4444
/// ### What it does
45-
/// Checks for usage of indexing or slicing. Arrays are special cases, this lint
46-
/// does report on arrays if we can tell that slicing operations are in bounds and does not
47-
/// lint on constant `usize` indexing on arrays because that is handled by rustc's `const_err` lint.
45+
/// Checks for usage of indexing or slicing that may panic at runtime.
46+
///
47+
/// This lint does not report on indexing or slicing operations
48+
/// that always panic, clippy's `out_of_bound_indexing` already
49+
/// handles those cases.
4850
///
4951
/// ### Why restrict this?
5052
/// To avoid implicit panics from indexing and slicing.
53+
///
5154
/// There are “checked” alternatives which do not panic, and can be used with `unwrap()` to make
5255
/// an explicit panic when it is desired.
5356
///
@@ -58,27 +61,31 @@ declare_clippy_lint! {
5861
/// ### Example
5962
/// ```rust,no_run
6063
/// // Vector
61-
/// let x = vec![0; 5];
64+
/// let x = vec![0, 1, 2, 3];
6265
///
6366
/// x[2];
67+
/// x[100];
6468
/// &x[2..100];
6569
///
6670
/// // Array
6771
/// let y = [0, 1, 2, 3];
6872
///
69-
/// &y[10..100];
70-
/// &y[10..];
73+
/// let i = 10; // Could be a runtime value
74+
/// let j = 20;
75+
/// &y[i..j];
7176
/// ```
7277
///
7378
/// Use instead:
7479
/// ```no_run
75-
/// # let x = vec![0; 5];
76-
/// # let y = [0, 1, 2, 3];
80+
/// # let x = vec![0, 1, 2, 3];
7781
/// x.get(2);
82+
/// x.get(100);
7883
/// x.get(2..100);
7984
///
80-
/// y.get(10);
81-
/// y.get(10..100);
85+
/// # let y = [0, 1, 2, 3];
86+
/// let i = 10;
87+
/// let j = 20;
88+
/// y.get(i..j);
8289
/// ```
8390
#[clippy::version = "pre 1.29.0"]
8491
pub INDEXING_SLICING,

0 commit comments

Comments
 (0)