Skip to content

Commit 9075c9e

Browse files
xFrednetcamsteffen
andcommitted
Update documentation and message for avoidable_slice_indexing
Co-authored-by: camsteffen <[email protected]>
1 parent 79406a4 commit 9075c9e

File tree

3 files changed

+34
-33
lines changed

3 files changed

+34
-33
lines changed

clippy_lints/src/avoidable_slice_indexing.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ 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
23-
/// are only used to access individual slice values.
22+
/// The lint checks for slice bindings in patterns that are only used to
23+
/// access individual slice values.
2424
///
2525
/// ### Why is this bad?
26-
/// Accessing slice values using indices can lead to panics.
26+
/// Accessing slice values using indices can lead to panics. Using a refutable
27+
/// pattern and binding to the individual values can avoid these.
2728
///
2829
/// ### Limitations
29-
/// This lint currently only checks for immutable access.
30+
/// This lint currently only checks for immutable access inside `if let`
31+
/// patterns.
3032
///
3133
/// ### Example
3234
/// ```rust
@@ -64,7 +66,7 @@ impl AvoidableSliceIndexing {
6466
}
6567
}
6668

67-
impl_lint_pass!(AvoidableSliceIndexing => [AVOIDABLE_SLICE_INDEXING]);
69+
impl_lint_pass!(AvoidableSliceIndexing => [AVOIDABLE_SLICE_INDEXING]);
6870

6971
impl LateLintPass<'_> for AvoidableSliceIndexing {
7072
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
@@ -157,10 +159,10 @@ fn lint_slice(cx: &LateContext<'_>, slice: &SliceLintInformation) {
157159
cx,
158160
AVOIDABLE_SLICE_INDEXING,
159161
slice.ident.span,
160-
"this slice can be deconstructured to avoid indexing",
162+
"this binding can be a slice pattern to avoid indexing",
161163
|diag| {
162164
diag.multipart_suggestion(
163-
"try destructing the slice with a pattern here",
165+
"try using a slice pattern here",
164166
slice
165167
.pattern_spans
166168
.iter()
@@ -180,10 +182,9 @@ fn lint_slice(cx: &LateContext<'_>, slice: &SliceLintInformation) {
180182
);
181183

182184
// I thought about adding a note to the lint message to inform the user that these
183-
// refactorings will remove the expressions that determine which access should be
184-
// used. However, I decided against this, as `filter_lintable_slices` will only
185-
// return slices where all access indices are known at compile time. They will
186-
// therefore not have any side effects when they are removed.
185+
// refactorings will remove the index expression. However, I decided against this,
186+
// as `filter_lintable_slices` will only return slices where all access indices are
187+
// known at compile time. The removal should therefore not have any side effects.
187188
},
188189
);
189190
}

tests/ui/avoidable_slice_indexing/if_let_slice_destruction.stderr

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: this slice can be deconstructured to avoid indexing
1+
error: this binding can be a slice pattern to avoid indexing
22
--> $DIR/if_let_slice_destruction.rs:13:17
33
|
44
LL | if let Some(slice) = slice {
@@ -9,7 +9,7 @@ note: the lint level is defined here
99
|
1010
LL | #![deny(clippy::avoidable_slice_indexing)]
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12-
help: try destructing the slice with a pattern here
12+
help: try using a slice pattern here
1313
|
1414
LL | if let Some([slice_0, ..]) = slice {
1515
| ~~~~~~~~~~~~~
@@ -18,13 +18,13 @@ help: and replacing the index expressions here
1818
LL | println!("{}", slice_0);
1919
| ~~~~~~~
2020

21-
error: this slice can be deconstructured to avoid indexing
21+
error: this binding can be a slice pattern to avoid indexing
2222
--> $DIR/if_let_slice_destruction.rs:19:17
2323
|
2424
LL | if let Some(slice) = slice {
2525
| ^^^^^
2626
|
27-
help: try destructing the slice with a pattern here
27+
help: try using a slice pattern here
2828
|
2929
LL | if let Some([ref slice_0, ..]) = slice {
3030
| ~~~~~~~~~~~~~~~~~
@@ -33,13 +33,13 @@ help: and replacing the index expressions here
3333
LL | println!("{}", slice_0);
3434
| ~~~~~~~
3535

36-
error: this slice can be deconstructured to avoid indexing
36+
error: this binding can be a slice pattern to avoid indexing
3737
--> $DIR/if_let_slice_destruction.rs:25:17
3838
|
3939
LL | if let Some(slice) = slice {
4040
| ^^^^^
4141
|
42-
help: try destructing the slice with a pattern here
42+
help: try using a slice pattern here
4343
|
4444
LL | if let Some([ref slice_0, _, ref slice_2, ..]) = slice {
4545
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -49,13 +49,13 @@ LL ~ println!("{}", slice_2);
4949
LL ~ println!("{}", slice_0);
5050
|
5151

52-
error: this slice can be deconstructured to avoid indexing
52+
error: this binding can be a slice pattern to avoid indexing
5353
--> $DIR/if_let_slice_destruction.rs:32:26
5454
|
5555
LL | if let SomeEnum::One(slice) | SomeEnum::Three(slice) = slice_wrapped {
5656
| ^^^^^
5757
|
58-
help: try destructing the slice with a pattern here
58+
help: try using a slice pattern here
5959
|
6060
LL | if let SomeEnum::One([ref slice_0, ..]) | SomeEnum::Three([ref slice_0, ..]) = slice_wrapped {
6161
| ~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~
@@ -64,13 +64,13 @@ help: and replacing the index expressions here
6464
LL | println!("{}", slice_0);
6565
| ~~~~~~~
6666

67-
error: this slice can be deconstructured to avoid indexing
67+
error: this binding can be a slice pattern to avoid indexing
6868
--> $DIR/if_let_slice_destruction.rs:39:29
6969
|
7070
LL | if let (SomeEnum::Three(a), Some(b)) = (a_wrapped, b_wrapped) {
7171
| ^
7272
|
73-
help: try destructing the slice with a pattern here
73+
help: try using a slice pattern here
7474
|
7575
LL | if let (SomeEnum::Three([_, _, ref a_2, ..]), Some(b)) = (a_wrapped, b_wrapped) {
7676
| ~~~~~~~~~~~~~~~~~~~
@@ -79,13 +79,13 @@ help: and replacing the index expressions here
7979
LL | println!("{} -> {}", a_2, b[1]);
8080
| ~~~
8181

82-
error: this slice can be deconstructured to avoid indexing
82+
error: this binding can be a slice pattern to avoid indexing
8383
--> $DIR/if_let_slice_destruction.rs:39:38
8484
|
8585
LL | if let (SomeEnum::Three(a), Some(b)) = (a_wrapped, b_wrapped) {
8686
| ^
8787
|
88-
help: try destructing the slice with a pattern here
88+
help: try using a slice pattern here
8989
|
9090
LL | if let (SomeEnum::Three(a), Some([_, ref b_1, ..])) = (a_wrapped, b_wrapped) {
9191
| ~~~~~~~~~~~~~~~~
@@ -94,13 +94,13 @@ help: and replacing the index expressions here
9494
LL | println!("{} -> {}", a[2], b_1);
9595
| ~~~
9696

97-
error: this slice can be deconstructured to avoid indexing
97+
error: this binding can be a slice pattern to avoid indexing
9898
--> $DIR/if_let_slice_destruction.rs:46:21
9999
|
100100
LL | if let Some(ref slice) = slice {
101101
| ^^^^^
102102
|
103-
help: try destructing the slice with a pattern here
103+
help: try using a slice pattern here
104104
|
105105
LL | if let Some([_, ref slice_1, ..]) = slice {
106106
| ~~~~~~~~~~~~~~~~~~~~
@@ -109,13 +109,13 @@ help: and replacing the index expressions here
109109
LL | println!("{:?}", slice_1);
110110
| ~~~~~~~
111111

112-
error: this slice can be deconstructured to avoid indexing
112+
error: this binding can be a slice pattern to avoid indexing
113113
--> $DIR/if_let_slice_destruction.rs:54:17
114114
|
115115
LL | if let Some(slice) = &slice {
116116
| ^^^^^
117117
|
118-
help: try destructing the slice with a pattern here
118+
help: try using a slice pattern here
119119
|
120120
LL | if let Some([ref slice_0, ..]) = &slice {
121121
| ~~~~~~~~~~~~~~~~~
@@ -124,13 +124,13 @@ help: and replacing the index expressions here
124124
LL | println!("{:?}", slice_0);
125125
| ~~~~~~~
126126

127-
error: this slice can be deconstructured to avoid indexing
127+
error: this binding can be a slice pattern to avoid indexing
128128
--> $DIR/if_let_slice_destruction.rs:123:17
129129
|
130130
LL | if let Some(slice) = wrap.inner {
131131
| ^^^^^
132132
|
133-
help: try destructing the slice with a pattern here
133+
help: try using a slice pattern here
134134
|
135135
LL | if let Some([ref slice_0, ..]) = wrap.inner {
136136
| ~~~~~~~~~~~~~~~~~
@@ -139,13 +139,13 @@ help: and replacing the index expressions here
139139
LL | println!("This is awesome! {}", slice_0);
140140
| ~~~~~~~
141141

142-
error: this slice can be deconstructured to avoid indexing
142+
error: this binding can be a slice pattern to avoid indexing
143143
--> $DIR/if_let_slice_destruction.rs:130:17
144144
|
145145
LL | if let Some(slice) = wrap.inner {
146146
| ^^^^^
147147
|
148-
help: try destructing the slice with a pattern here
148+
help: try using a slice pattern here
149149
|
150150
LL | if let Some([ref slice_0, ..]) = wrap.inner {
151151
| ~~~~~~~~~~~~~~~~~

tests/ui/avoidable_slice_indexing/slice_indexing_in_macro.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: this slice can be deconstructured to avoid indexing
1+
error: this binding can be a slice pattern to avoid indexing
22
--> $DIR/slice_indexing_in_macro.rs:23:21
33
|
44
LL | if let Some(slice) = slice;
@@ -9,7 +9,7 @@ note: the lint level is defined here
99
|
1010
LL | #![deny(clippy::avoidable_slice_indexing)]
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12-
help: try destructing the slice with a pattern here
12+
help: try using a slice pattern here
1313
|
1414
LL | if let Some([slice_0, ..]) = slice;
1515
| ~~~~~~~~~~~~~

0 commit comments

Comments
 (0)