Clippy lints / Pedantic
Clippy lint · Pedantic · checked with Clippy 0.1.99
clippy::index_refutable_slice: this binding can be a slice pattern to avoid indexing
What it meansThe lint checks for slice bindings in patterns that are only used to access individual slice values. It is in the pedantic group and off by default.
What you see
warning: this binding can be a slice pattern to avoid indexing --> main.rs:5:13 | 5 | if let Some(slice) = slice { | ^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#index_refutable_slice = note: `-W clippy::index-refutable-slice` implied by `-W clippy::pedantic` help: replace the binding and indexed access with a slice pattern | 5 ~ if let Some([slice_0, ..]) = slice { 6 ~ println!("{}", slice_0); |
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?
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.
Stricter lints with some false positives. Off by default; opt in with clippy::pedantic.
How to fix it
Clippy does not fix this one automatically (its suggestion may change behaviour, so it only shows it). Change the code by hand:
Flagged
let slice: Option<&[u32]> = Some(&[1, 2, 3]);
if let Some(slice) = slice {
println!("{}", slice[0]);
}
Write this instead
let slice: Option<&[u32]> = Some(&[1, 2, 3]);
if let Some(&[first, ..]) = slice {
println!("{}", first);
}
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]
index_refutable_slice = "warn"In code
#![warn(clippy::index_refutable_slice)]One run
cargo clippy -- -W clippy::index-refutable-sliceThe [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
max-suggested-slice-pattern-length: When Clippy suggests using a slice pattern, this is the maximum number of elements allowed in the slice pattern that is suggested. If more elements are necessary, the lint is suppressed. For example, [_, _, _, e, ..] is a slice pattern with 4 elements.msrv: The minimum rust version that the project supports. Defaults to the rust-version field in Cargo.toml
Clippy's own documentation
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);
}
Configuration
-
max-suggested-slice-pattern-length: When Clippy suggests using a slice pattern, this is the maximum number of elements allowed in the slice pattern that is suggested. If more elements are necessary, the lint is suppressed. For example,[_, _, _, e, ..]is a slice pattern with 4 elements.(default:
3) -
msrv: The minimum rust version that the project supports. Defaults to therust-versionfield inCargo.toml(default:
current version)
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- too_many_linesChecks for functions with a large amount of lines
- missing_errors_docChecks the doc comments of publicly visible functions that return a Result type and warns if there is no # Errors section
- must_use_candidateChecks for public functions that have no #[must_use] attribute, but return something not already marked must-use, have no mutable arg and mutate no statics
- implicit_cloneimplicitly cloning a `Vec` by calling `to_vec` on its dereferenced type
- implicit_hasherChecks for public impl or fn missing generalization over different hashers and implicitly defaulting to the default hashing algorithm (SipHash)
- inconsistent_struct_constructorstruct constructor field order is inconsistent with struct definition field order
- inefficient_to_stringChecks for usage of .to_string() on an &&T where T implements ToString directly (like &&str or &&String)
- inline_alwaysChecks for items annotated with #[inline(always)], unless the annotated function is empty or simply panics
- into_iter_without_iterThis is the opposite of the iter_without_into_iter lint. It looks for IntoIterator for (&|&mut) Type implementations without an inherent iter or iter_mut method on the type or on any of the types in its Deref chain
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::too_many_linesChecks for functions with a large amount of linesRead it next →Did this answer your question?