Skip to content

Commit 1e24341

Browse files
committed
refactor a bit
1 parent 6702c7a commit 1e24341

File tree

2 files changed

+18
-24
lines changed

2 files changed

+18
-24
lines changed

clippy_lints/src/single_range_in_vec_init.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
use clippy_utils::{
2-
diagnostics::span_lint_and_then,
3-
get_trait_def_id,
4-
higher::VecArgs,
5-
macros::root_macro_call_first_node,
6-
source::{snippet_opt, snippet_with_applicability},
7-
ty::implements_trait,
2+
diagnostics::span_lint_and_then, get_trait_def_id, higher::VecArgs, macros::root_macro_call_first_node,
3+
source::snippet_opt, ty::implements_trait,
84
};
95
use rustc_ast::{LitIntType, LitKind, UintTy};
106
use rustc_errors::Applicability;
@@ -75,7 +71,7 @@ impl LateLintPass<'_> for SingleRangeInVecInit {
7571
// ^^^^^^ ^^^^^^^
7672
// span: `vec![0..200]` or `[0..200]`
7773
// ^^^^^^^^^^^^ ^^^^^^^^
78-
// kind: What to print, an array or a `Vec`
74+
// kind: What to print, "an array" or "a `Vec`"
7975
let (inner_expr, span, kind) = if let ExprKind::Array([inner_expr]) = expr.kind
8076
&& !expr.span.from_expansion()
8177
{
@@ -98,11 +94,9 @@ impl LateLintPass<'_> for SingleRangeInVecInit {
9894
// `is_from_proc_macro` will skip any `vec![]`. Let's not!
9995
&& snippet.starts_with(kind.starts_with())
10096
&& snippet.ends_with(kind.ends_with())
97+
&& let Some(start_snippet) = snippet_opt(cx, start.span)
98+
&& let Some(end_snippet) = snippet_opt(cx, end.span)
10199
{
102-
let mut app = Applicability::MaybeIncorrect;
103-
let start_snippet = snippet_with_applicability(cx, start.span, "...", &mut app);
104-
let end_snippet = snippet_with_applicability(cx, end.span, "...", &mut app);
105-
106100
let should_emit_every_value = if let Some(step_def_id) = get_trait_def_id(cx, &["core", "iter", "Step"])
107101
&& implements_trait(cx, ty, step_def_id, &[])
108102
{
@@ -131,9 +125,9 @@ impl LateLintPass<'_> for SingleRangeInVecInit {
131125
if should_emit_every_value {
132126
diag.span_suggestion(
133127
span,
134-
"if you wanted a `Vec` that contains every value in the range, try",
128+
"if you wanted a `Vec` that contains the entire range, try",
135129
format!("({start_snippet}..{end_snippet}).collect::<std::vec::Vec<{ty}>>()"),
136-
app,
130+
Applicability::MaybeIncorrect,
137131
);
138132
}
139133

@@ -142,7 +136,7 @@ impl LateLintPass<'_> for SingleRangeInVecInit {
142136
inner_expr.span,
143137
format!("if you wanted {kind} of len {end_snippet}, try"),
144138
format!("{start_snippet}; {end_snippet}"),
145-
app,
139+
Applicability::MaybeIncorrect,
146140
);
147141
}
148142
},

tests/ui/single_range_in_vec_init.stderr

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | [0..200];
55
| ^^^^^^^^
66
|
77
= note: `-D clippy::single-range-in-vec-init` implied by `-D warnings`
8-
help: if you wanted a `Vec` that contains every value in the range, try
8+
help: if you wanted a `Vec` that contains the entire range, try
99
|
1010
LL | (0..200).collect::<std::vec::Vec<i32>>();
1111
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -20,7 +20,7 @@ error: a `Vec` of `Range` that is only one element
2020
LL | vec![0..200];
2121
| ^^^^^^^^^^^^
2222
|
23-
help: if you wanted a `Vec` that contains every value in the range, try
23+
help: if you wanted a `Vec` that contains the entire range, try
2424
|
2525
LL | (0..200).collect::<std::vec::Vec<i32>>();
2626
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -35,7 +35,7 @@ error: an array of `Range` that is only one element
3535
LL | [0u8..200];
3636
| ^^^^^^^^^^
3737
|
38-
help: if you wanted a `Vec` that contains every value in the range, try
38+
help: if you wanted a `Vec` that contains the entire range, try
3939
|
4040
LL | (0u8..200).collect::<std::vec::Vec<u8>>();
4141
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -50,7 +50,7 @@ error: an array of `Range` that is only one element
5050
LL | [0usize..200];
5151
| ^^^^^^^^^^^^^
5252
|
53-
help: if you wanted a `Vec` that contains every value in the range, try
53+
help: if you wanted a `Vec` that contains the entire range, try
5454
|
5555
LL | (0usize..200).collect::<std::vec::Vec<usize>>();
5656
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -65,7 +65,7 @@ error: an array of `Range` that is only one element
6565
LL | [0..200usize];
6666
| ^^^^^^^^^^^^^
6767
|
68-
help: if you wanted a `Vec` that contains every value in the range, try
68+
help: if you wanted a `Vec` that contains the entire range, try
6969
|
7070
LL | (0..200usize).collect::<std::vec::Vec<usize>>();
7171
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -80,7 +80,7 @@ error: a `Vec` of `Range` that is only one element
8080
LL | vec![0u8..200];
8181
| ^^^^^^^^^^^^^^
8282
|
83-
help: if you wanted a `Vec` that contains every value in the range, try
83+
help: if you wanted a `Vec` that contains the entire range, try
8484
|
8585
LL | (0u8..200).collect::<std::vec::Vec<u8>>();
8686
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -95,7 +95,7 @@ error: a `Vec` of `Range` that is only one element
9595
LL | vec![0usize..200];
9696
| ^^^^^^^^^^^^^^^^^
9797
|
98-
help: if you wanted a `Vec` that contains every value in the range, try
98+
help: if you wanted a `Vec` that contains the entire range, try
9999
|
100100
LL | (0usize..200).collect::<std::vec::Vec<usize>>();
101101
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -110,7 +110,7 @@ error: a `Vec` of `Range` that is only one element
110110
LL | vec![0..200usize];
111111
| ^^^^^^^^^^^^^^^^^
112112
|
113-
help: if you wanted a `Vec` that contains every value in the range, try
113+
help: if you wanted a `Vec` that contains the entire range, try
114114
|
115115
LL | (0..200usize).collect::<std::vec::Vec<usize>>();
116116
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -125,7 +125,7 @@ error: an array of `Range` that is only one element
125125
LL | [0..200isize];
126126
| ^^^^^^^^^^^^^
127127
|
128-
help: if you wanted a `Vec` that contains every value in the range, try
128+
help: if you wanted a `Vec` that contains the entire range, try
129129
|
130130
LL | (0..200isize).collect::<std::vec::Vec<isize>>();
131131
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -136,7 +136,7 @@ error: a `Vec` of `Range` that is only one element
136136
LL | vec![0..200isize];
137137
| ^^^^^^^^^^^^^^^^^
138138
|
139-
help: if you wanted a `Vec` that contains every value in the range, try
139+
help: if you wanted a `Vec` that contains the entire range, try
140140
|
141141
LL | (0..200isize).collect::<std::vec::Vec<isize>>();
142142
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)