Skip to content

Commit cca8092

Browse files
committed
fix: single_range_in_vec_init wrongly unmangles macros
1 parent 95cc198 commit cca8092

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

clippy_lints/src/single_range_in_vec_init.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::higher::VecArgs;
33
use clippy_utils::macros::root_macro_call_first_node;
4-
use clippy_utils::source::SpanRangeExt;
4+
use clippy_utils::source::{SpanRangeExt, snippet_with_context};
55
use clippy_utils::ty::implements_trait;
66
use clippy_utils::{is_no_std_crate, sym};
77
use rustc_ast::{LitIntType, LitKind, UintTy};
@@ -96,9 +96,11 @@ 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.expr.span.get_source_text(cx)
100-
&& let Some(end_snippet) = end.expr.span.get_source_text(cx)
10199
{
100+
let mut applicability = Applicability::MaybeIncorrect;
101+
let (start_snippet, _) = snippet_with_context(cx, start.expr.span, span.ctxt(), "..", &mut applicability);
102+
let (end_snippet, _) = snippet_with_context(cx, end.expr.span, span.ctxt(), "..", &mut applicability);
103+
102104
let should_emit_every_value = if let Some(step_def_id) = cx.tcx.get_diagnostic_item(sym::range_step)
103105
&& implements_trait(cx, ty, step_def_id, &[])
104106
{

tests/ui/single_range_in_vec_init.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,14 @@ fn issue16042() {
7373
let input = vec![Range { start: 0, end: 5 }];
7474
//~^ single_range_in_vec_init
7575
}
76+
77+
fn issue16044() {
78+
macro_rules! as_i32 {
79+
($x:expr) => {
80+
$x as i32
81+
};
82+
}
83+
84+
let input = vec![0..as_i32!(10)];
85+
//~^ single_range_in_vec_init
86+
}

tests/ui/single_range_in_vec_init.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,5 +177,17 @@ LL - let input = vec![Range { start: 0, end: 5 }];
177177
LL + let input = vec![0; 5];
178178
|
179179

180-
error: aborting due to 11 previous errors
180+
error: a `Vec` of `Range` that is only one element
181+
--> tests/ui/single_range_in_vec_init.rs:84:17
182+
|
183+
LL | let input = vec![0..as_i32!(10)];
184+
| ^^^^^^^^^^^^^^^^^^^^
185+
|
186+
help: if you wanted a `Vec` that contains the entire range, try
187+
|
188+
LL - let input = vec![0..as_i32!(10)];
189+
LL + let input = (0..as_i32!(10)).collect::<std::vec::Vec<i32>>();
190+
|
191+
192+
error: aborting due to 12 previous errors
181193

0 commit comments

Comments
 (0)