Skip to content

Commit

Permalink
refactor a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Centri3 committed Jun 12, 2023
1 parent 6702c7a commit 1e24341
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
22 changes: 8 additions & 14 deletions clippy_lints/src/single_range_in_vec_init.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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()
{
Expand All @@ -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, &[])
{
Expand Down Expand Up @@ -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::<std::vec::Vec<{ty}>>()"),
app,
Applicability::MaybeIncorrect,
);
}

Expand All @@ -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,
);
}
},
Expand Down
20 changes: 10 additions & 10 deletions tests/ui/single_range_in_vec_init.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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::<std::vec::Vec<i32>>();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -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::<std::vec::Vec<i32>>();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -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::<std::vec::Vec<u8>>();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -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::<std::vec::Vec<usize>>();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -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::<std::vec::Vec<usize>>();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -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::<std::vec::Vec<u8>>();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -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::<std::vec::Vec<usize>>();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -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::<std::vec::Vec<usize>>();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -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::<std::vec::Vec<isize>>();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -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::<std::vec::Vec<isize>>();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down

0 comments on commit 1e24341

Please sign in to comment.