Clippy lints / Restriction
Clippy lint · Restriction · checked with Clippy 0.1.99
clippy::indexing_slicing: indexing may panic
What it meansChecks for usage of indexing or slicing that may panic at runtime. This lint does not report on indexing or slicing operations that always panic, out_of_bounds_indexing already handles those cases. It is in the restriction group and off by default.
What you see
warning: indexing may panic --> main.rs:6:1 | 6 | x[2]; | ^^^^ | = help: consider using `.get(n)` or `.get_mut(n)` instead = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#indexing_slicing = note: requested on the command line with `-W clippy::indexing-slicing`
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
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.
Lints that forbid language features for specific codebases (no unwrap, no indexing...). Off by default; pick them one by one, never the whole group.
How to fix it
Clippy does not fix this one automatically. Change the code by hand:
Flagged
// Vector
let x = vec![0, 1, 2, 3];
x[2];
x[100];
&x[2..100];
// Array
let y = [0, 1, 2, 3];
let i = 10; // Could be a runtime value
let j = 20;
&y[i..j];
Write this instead
x.get(2);
x.get(100);
x.get(2..100);
let i = 10;
let j = 20;
y.get(i..j);
Turn it off, or on
It is off by default. To use it, set it to warn or deny:
Whole crate (Cargo.toml)
[lints.clippy]
indexing_slicing = "warn"In code
#![warn(clippy::indexing_slicing)]One run
cargo clippy -- -W clippy::indexing-slicingThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Settings in clippy.toml
allow-indexing-slicing-in-tests: Whether indexing_slicing should be allowed in test functions or #[cfg(test)]suppress-restriction-lint-in-const: Whether to suppress a restriction lint in constant code. In same cases the restructured operation might not be unavoidable, as the suggested counterparts are unavailable in constant code. This configuration will cause restriction lints to trigger even if no suggestion can be made.
Clippy's own documentation
What it does
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, out_of_bounds_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.
Limitations
This lint does not check for the usage of indexing or slicing on strings. These are covered
by the more specific string_slice lint.
Example
// Vector
let x = vec![0, 1, 2, 3];
x[2];
x[100];
&x[2..100];
// Array
let y = [0, 1, 2, 3];
let i = 10; // Could be a runtime value
let j = 20;
&y[i..j];
Use instead:
x.get(2);
x.get(100);
x.get(2..100);
let i = 10;
let j = 20;
y.get(i..j);
Configuration
-
allow-indexing-slicing-in-tests: Whetherindexing_slicingshould be allowed in test functions or#[cfg(test)](default:
false) -
suppress-restriction-lint-in-const: Whether to suppress a restriction lint in constant code. In same cases the restructured operation might not be unavoidable, as the suggested counterparts are unavailable in constant code. This configuration will cause restriction lints to trigger even if no suggestion can be made.(default:
false)
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- out_of_bounds_indexingindex is out of bounds
- unwrap_usedChecks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on Options
- module_name_repetitionsDetects public item names that are prefixed or suffixed by the containing public module’s name
- expect_usedChecks for .expect() or .expect_err() calls on Results and .expect() call on Options
- if_then_some_else_noneChecks for if-else that could be written using either bool::then or bool::then_some
- impl_trait_in_paramsLints when impl Trait is being used in a function’s parameters
- implicit_returnmissing `return` statement
- infinite_loopinfinite loop detected
- inline_asm_x86_att_syntaxChecks for usage of AT&T x86 assembly syntax
- inline_asm_x86_intel_syntaxChecks for usage of Intel x86 assembly syntax
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::out_of_bounds_indexingindex is out of boundsRead it next →Did this answer your question?