Skip to content

Commit 79406a4

Browse files
committed
Added more tests for avoidable_slice_indexing
1 parent cb3cbdb commit 79406a4

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

tests/ui/avoidable_slice_indexing/if_let_slice_destruction.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,63 @@ fn slice_is_used() {
7676
if let Some(slice) = slice {
7777
println!("{:?}", slice.to_vec());
7878
}
79+
80+
let opt: Option<[String; 2]> = Some([String::from("Hello"), String::from("world")]);
81+
if let Some(slice) = opt {
82+
if !slice.is_empty() {
83+
println!("first: {}", slice[0]);
84+
}
85+
}
86+
}
87+
88+
/// The slice is used by an external function and should therefore not be linted
89+
fn check_slice_as_arg() {
90+
fn is_interesting<T>(slice: &[T; 2]) -> bool {
91+
!slice.is_empty()
92+
}
93+
94+
let slice_wrapped: Option<[String; 2]> = Some([String::from("Hello"), String::from("world")]);
95+
if let Some(slice) = &slice_wrapped {
96+
if is_interesting(slice) {
97+
println!("This is interesting {}", slice[0]);
98+
}
99+
}
100+
println!("{:?}", slice_wrapped);
101+
}
102+
103+
fn check_slice_in_struct() {
104+
#[derive(Debug)]
105+
struct Wrapper<'a> {
106+
inner: Option<&'a [String]>,
107+
is_awesome: bool,
108+
}
109+
110+
impl<'a> Wrapper<'a> {
111+
fn is_super_awesome(&self) -> bool {
112+
self.is_awesome
113+
}
114+
}
115+
116+
let inner = &[String::from("New"), String::from("World")];
117+
let wrap = Wrapper {
118+
inner: Some(inner),
119+
is_awesome: true,
120+
};
121+
122+
// Test 1: Field access
123+
if let Some(slice) = wrap.inner {
124+
if wrap.is_awesome {
125+
println!("This is awesome! {}", slice[0]);
126+
}
127+
}
128+
129+
// Test 2: function access
130+
if let Some(slice) = wrap.inner {
131+
if wrap.is_super_awesome() {
132+
println!("This is super awesome! {}", slice[0]);
133+
}
134+
}
135+
println!("Complete wrap: {:?}", wrap);
79136
}
80137

81138
/// This would be a nice additional feature to have in the future, but adding it

tests/ui/avoidable_slice_indexing/if_let_slice_destruction.stderr

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,35 @@ help: and replacing the index expressions here
124124
LL | println!("{:?}", slice_0);
125125
| ~~~~~~~
126126

127-
error: aborting due to 8 previous errors
127+
error: this slice can be deconstructured to avoid indexing
128+
--> $DIR/if_let_slice_destruction.rs:123:17
129+
|
130+
LL | if let Some(slice) = wrap.inner {
131+
| ^^^^^
132+
|
133+
help: try destructing the slice with a pattern here
134+
|
135+
LL | if let Some([ref slice_0, ..]) = wrap.inner {
136+
| ~~~~~~~~~~~~~~~~~
137+
help: and replacing the index expressions here
138+
|
139+
LL | println!("This is awesome! {}", slice_0);
140+
| ~~~~~~~
141+
142+
error: this slice can be deconstructured to avoid indexing
143+
--> $DIR/if_let_slice_destruction.rs:130:17
144+
|
145+
LL | if let Some(slice) = wrap.inner {
146+
| ^^^^^
147+
|
148+
help: try destructing the slice with a pattern here
149+
|
150+
LL | if let Some([ref slice_0, ..]) = wrap.inner {
151+
| ~~~~~~~~~~~~~~~~~
152+
help: and replacing the index expressions here
153+
|
154+
LL | println!("This is super awesome! {}", slice_0);
155+
| ~~~~~~~
156+
157+
error: aborting due to 10 previous errors
128158

0 commit comments

Comments
 (0)