Skip to content

Commit 95cc198

Browse files
committed
fix: single_range_in_vec_init wrongly showed field name when using Range
1 parent a456519 commit 95cc198

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

clippy_lints/src/single_range_in_vec_init.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ impl LateLintPass<'_> for SingleRangeInVecInit {
9696
// `is_from_proc_macro` will skip any `vec![]`. Let's not!
9797
&& snippet.starts_with(suggested_type.starts_with())
9898
&& snippet.ends_with(suggested_type.ends_with())
99-
&& let Some(start_snippet) = start.span.get_source_text(cx)
100-
&& let Some(end_snippet) = end.span.get_source_text(cx)
99+
&& let Some(start_snippet) = start.expr.span.get_source_text(cx)
100+
&& let Some(end_snippet) = end.expr.span.get_source_text(cx)
101101
{
102102
let should_emit_every_value = if let Some(step_def_id) = cx.tcx.get_diagnostic_item(sym::range_step)
103103
&& implements_trait(cx, ty, step_def_id, &[])

tests/ui/single_range_in_vec_init.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,10 @@ fn main() {
6666
vec![0..200];
6767
}
6868
}
69+
70+
fn issue16042() {
71+
use std::ops::Range;
72+
73+
let input = vec![Range { start: 0, end: 5 }];
74+
//~^ single_range_in_vec_init
75+
}

tests/ui/single_range_in_vec_init.stderr

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,5 +160,22 @@ LL - vec![0..200isize];
160160
LL + (0..200isize).collect::<std::vec::Vec<isize>>();
161161
|
162162

163-
error: aborting due to 10 previous errors
163+
error: a `Vec` of `Range` that is only one element
164+
--> tests/ui/single_range_in_vec_init.rs:73:17
165+
|
166+
LL | let input = vec![Range { start: 0, end: 5 }];
167+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
168+
|
169+
help: if you wanted a `Vec` that contains the entire range, try
170+
|
171+
LL - let input = vec![Range { start: 0, end: 5 }];
172+
LL + let input = (0..5).collect::<std::vec::Vec<i32>>();
173+
|
174+
help: if you wanted a `Vec` of len 5, try
175+
|
176+
LL - let input = vec![Range { start: 0, end: 5 }];
177+
LL + let input = vec![0; 5];
178+
|
179+
180+
error: aborting due to 11 previous errors
164181

0 commit comments

Comments
 (0)