Skip to content

Commit c7ac557

Browse files
committed
Test and document mut for avoid_slice_indexing
1 parent 29d99a9 commit c7ac557

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

clippy_lints/src/avoidable_slice_indexing.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ use std::convert::TryInto;
1919

2020
declare_clippy_lint! {
2121
/// ### What it does
22-
/// The lint checks for slices that come from value deconstruction and are only used to access individual slice values.
22+
/// The lint checks for slices that come from value deconstruction and
23+
/// are only used to access individual slice values.
2324
///
2425
/// ### Why is this bad?
26+
/// Accessing slice values using indices can lead to panics.
27+
///
28+
/// ### Limitations
29+
/// This lint currently only checks for immutable access.
2530
///
2631
/// ### Example
2732
/// ```rust

tests/ui-toml/avoidable_slice_indexing_limit/avoidable_slice_indexing_limit.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ LL | #![deny(clippy::avoidable_slice_indexing)]
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212
help: try destructing the slice with a pattern here
1313
|
14-
LL | if let Some(&[_, _, _, _, _, _, _, slice_7, ..]) = slice {
15-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14+
LL | if let Some([_, _, _, _, _, _, _, slice_7, ..]) = slice {
15+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1616
help: and replacing the index expressions here
1717
|
1818
LL | println!("{}", slice_7);

tests/ui/avoidable_slice_indexing/if_let_slice_destruction.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fn lintable_examples() {
5757
println!("{:?}", slice);
5858
}
5959

60-
fn slice_too_long() {
60+
fn slice_index_above_limit() {
6161
let slice: Option<&[u32]> = Some(&[1, 2, 3]);
6262

6363
if let Some(slice) = slice {
@@ -78,6 +78,25 @@ fn slice_is_used() {
7878
}
7979
}
8080

81+
/// This would be a nice additional feature to have in the future, but adding it
82+
/// now would make the PR too large. This is therefore only a test that we don't
83+
/// lint cases we can't make a reasonable suggestion for
84+
fn mutable_slice_index() {
85+
// Mut access
86+
let mut slice: Option<[String; 1]> = Some([String::from("Penguin")]);
87+
if let Some(ref mut slice) = slice {
88+
slice[0] = String::from("Mr. Penguin");
89+
}
90+
println!("Use after modification: {:?}", slice);
91+
92+
// Mut access on reference
93+
let mut slice: Option<[String; 1]> = Some([String::from("Cat")]);
94+
if let Some(slice) = &mut slice {
95+
slice[0] = String::from("Lord Meow Meow");
96+
}
97+
println!("Use after modification: {:?}", slice);
98+
}
99+
81100
fn complex_binding() {
82101
let slice: Option<&[u32]> = Some(&[1, 2, 3]);
83102
if let Some(slice @ [_, _, _]) = slice {

0 commit comments

Comments
 (0)