diff --git a/clippy_lints/src/single_range_in_vec_init.rs b/clippy_lints/src/single_range_in_vec_init.rs index 2d3f18a62c01..e4aa618bcf5e 100644 --- a/clippy_lints/src/single_range_in_vec_init.rs +++ b/clippy_lints/src/single_range_in_vec_init.rs @@ -1,10 +1,6 @@ use clippy_utils::{ - diagnostics::span_lint_and_then, - get_trait_def_id, - higher::VecArgs, - macros::root_macro_call_first_node, - source::{snippet_opt, snippet_with_applicability}, - ty::implements_trait, + diagnostics::span_lint_and_then, get_trait_def_id, higher::VecArgs, macros::root_macro_call_first_node, + source::snippet_opt, ty::implements_trait, }; use rustc_ast::{LitIntType, LitKind, UintTy}; use rustc_errors::Applicability; @@ -75,7 +71,7 @@ impl LateLintPass<'_> for SingleRangeInVecInit { // ^^^^^^ ^^^^^^^ // span: `vec![0..200]` or `[0..200]` // ^^^^^^^^^^^^ ^^^^^^^^ - // kind: What to print, an array or a `Vec` + // kind: What to print, "an array" or "a `Vec`" let (inner_expr, span, kind) = if let ExprKind::Array([inner_expr]) = expr.kind && !expr.span.from_expansion() { @@ -98,11 +94,9 @@ impl LateLintPass<'_> for SingleRangeInVecInit { // `is_from_proc_macro` will skip any `vec![]`. Let's not! && snippet.starts_with(kind.starts_with()) && snippet.ends_with(kind.ends_with()) + && let Some(start_snippet) = snippet_opt(cx, start.span) + && let Some(end_snippet) = snippet_opt(cx, end.span) { - let mut app = Applicability::MaybeIncorrect; - let start_snippet = snippet_with_applicability(cx, start.span, "...", &mut app); - let end_snippet = snippet_with_applicability(cx, end.span, "...", &mut app); - let should_emit_every_value = if let Some(step_def_id) = get_trait_def_id(cx, &["core", "iter", "Step"]) && implements_trait(cx, ty, step_def_id, &[]) { @@ -131,9 +125,9 @@ impl LateLintPass<'_> for SingleRangeInVecInit { if should_emit_every_value { diag.span_suggestion( span, - "if you wanted a `Vec` that contains every value in the range, try", + "if you wanted a `Vec` that contains the entire range, try", format!("({start_snippet}..{end_snippet}).collect::>()"), - app, + Applicability::MaybeIncorrect, ); } @@ -142,7 +136,7 @@ impl LateLintPass<'_> for SingleRangeInVecInit { inner_expr.span, format!("if you wanted {kind} of len {end_snippet}, try"), format!("{start_snippet}; {end_snippet}"), - app, + Applicability::MaybeIncorrect, ); } }, diff --git a/tests/ui/single_range_in_vec_init.stderr b/tests/ui/single_range_in_vec_init.stderr index fc03b2205e2e..3e3d521f4a50 100644 --- a/tests/ui/single_range_in_vec_init.stderr +++ b/tests/ui/single_range_in_vec_init.stderr @@ -5,7 +5,7 @@ LL | [0..200]; | ^^^^^^^^ | = note: `-D clippy::single-range-in-vec-init` implied by `-D warnings` -help: if you wanted a `Vec` that contains every value in the range, try +help: if you wanted a `Vec` that contains the entire range, try | LL | (0..200).collect::>(); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -20,7 +20,7 @@ error: a `Vec` of `Range` that is only one element LL | vec![0..200]; | ^^^^^^^^^^^^ | -help: if you wanted a `Vec` that contains every value in the range, try +help: if you wanted a `Vec` that contains the entire range, try | LL | (0..200).collect::>(); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -35,7 +35,7 @@ error: an array of `Range` that is only one element LL | [0u8..200]; | ^^^^^^^^^^ | -help: if you wanted a `Vec` that contains every value in the range, try +help: if you wanted a `Vec` that contains the entire range, try | LL | (0u8..200).collect::>(); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -50,7 +50,7 @@ error: an array of `Range` that is only one element LL | [0usize..200]; | ^^^^^^^^^^^^^ | -help: if you wanted a `Vec` that contains every value in the range, try +help: if you wanted a `Vec` that contains the entire range, try | LL | (0usize..200).collect::>(); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -65,7 +65,7 @@ error: an array of `Range` that is only one element LL | [0..200usize]; | ^^^^^^^^^^^^^ | -help: if you wanted a `Vec` that contains every value in the range, try +help: if you wanted a `Vec` that contains the entire range, try | LL | (0..200usize).collect::>(); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -80,7 +80,7 @@ error: a `Vec` of `Range` that is only one element LL | vec![0u8..200]; | ^^^^^^^^^^^^^^ | -help: if you wanted a `Vec` that contains every value in the range, try +help: if you wanted a `Vec` that contains the entire range, try | LL | (0u8..200).collect::>(); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -95,7 +95,7 @@ error: a `Vec` of `Range` that is only one element LL | vec![0usize..200]; | ^^^^^^^^^^^^^^^^^ | -help: if you wanted a `Vec` that contains every value in the range, try +help: if you wanted a `Vec` that contains the entire range, try | LL | (0usize..200).collect::>(); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -110,7 +110,7 @@ error: a `Vec` of `Range` that is only one element LL | vec![0..200usize]; | ^^^^^^^^^^^^^^^^^ | -help: if you wanted a `Vec` that contains every value in the range, try +help: if you wanted a `Vec` that contains the entire range, try | LL | (0..200usize).collect::>(); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -125,7 +125,7 @@ error: an array of `Range` that is only one element LL | [0..200isize]; | ^^^^^^^^^^^^^ | -help: if you wanted a `Vec` that contains every value in the range, try +help: if you wanted a `Vec` that contains the entire range, try | LL | (0..200isize).collect::>(); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -136,7 +136,7 @@ error: a `Vec` of `Range` that is only one element LL | vec![0..200isize]; | ^^^^^^^^^^^^^^^^^ | -help: if you wanted a `Vec` that contains every value in the range, try +help: if you wanted a `Vec` that contains the entire range, try | LL | (0..200isize).collect::>(); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~