1
1
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,
8
4
} ;
9
5
use rustc_ast:: { LitIntType , LitKind , UintTy } ;
10
6
use rustc_errors:: Applicability ;
@@ -18,9 +14,9 @@ declare_clippy_lint! {
18
14
/// Checks for `Vec` or array initializations that contain only one range.
19
15
///
20
16
/// ### Why is this bad?
21
- /// This is almost always incorrect, as it will result in a `Vec` that has only element. Almost
22
- /// always, the programmer intended for it to include all elements in the range or for the end
23
- /// of the range to be the length instead.
17
+ /// This is almost always incorrect, as it will result in a `Vec` that has only one element.
18
+ /// Almost always, the programmer intended for it to include all elements in the range or for
19
+ /// the end of the range to be the length instead.
24
20
///
25
21
/// ### Example
26
22
/// ```rust
@@ -75,8 +71,8 @@ impl LateLintPass<'_> for SingleRangeInVecInit {
75
71
// ^^^^^^ ^^^^^^^
76
72
// span: `vec![0..200]` or `[0..200]`
77
73
// ^^^^^^^^^^^^ ^^^^^^^^
78
- // kind : What to print, an array or a `Vec`
79
- let ( inner_expr, span, kind ) = if let ExprKind :: Array ( [ inner_expr] ) = expr. kind
74
+ // suggested_type : What to print, " an array" or " a `Vec`"
75
+ let ( inner_expr, span, suggested_type ) = if let ExprKind :: Array ( [ inner_expr] ) = expr. kind
80
76
&& !expr. span . from_expansion ( )
81
77
{
82
78
( inner_expr, expr. span , SuggestedType :: Array )
@@ -96,13 +92,11 @@ impl LateLintPass<'_> for SingleRangeInVecInit {
96
92
&& let ty = cx. typeck_results ( ) . expr_ty ( start. expr )
97
93
&& let Some ( snippet) = snippet_opt ( cx, span)
98
94
// `is_from_proc_macro` will skip any `vec![]`. Let's not!
99
- && snippet. starts_with ( kind. starts_with ( ) )
100
- && snippet. ends_with ( kind. ends_with ( ) )
95
+ && snippet. starts_with ( suggested_type. starts_with ( ) )
96
+ && snippet. ends_with ( suggested_type. ends_with ( ) )
97
+ && let Some ( start_snippet) = snippet_opt ( cx, start. span )
98
+ && let Some ( end_snippet) = snippet_opt ( cx, end. span )
101
99
{
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
-
106
100
let should_emit_every_value = if let Some ( step_def_id) = get_trait_def_id ( cx, & [ "core" , "iter" , "Step" ] )
107
101
&& implements_trait ( cx, ty, step_def_id, & [ ] )
108
102
{
@@ -126,23 +120,23 @@ impl LateLintPass<'_> for SingleRangeInVecInit {
126
120
cx,
127
121
SINGLE_RANGE_IN_VEC_INIT ,
128
122
span,
129
- & format ! ( "{kind } of `Range` that is only one element" ) ,
123
+ & format ! ( "{suggested_type } of `Range` that is only one element" ) ,
130
124
|diag| {
131
125
if should_emit_every_value {
132
126
diag. span_suggestion (
133
127
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" ,
135
129
format ! ( "({start_snippet}..{end_snippet}).collect::<std::vec::Vec<{ty}>>()" ) ,
136
- app ,
130
+ Applicability :: MaybeIncorrect ,
137
131
) ;
138
132
}
139
133
140
134
if should_emit_of_len {
141
135
diag. span_suggestion (
142
136
inner_expr. span ,
143
- format ! ( "if you wanted {kind } of len {end_snippet}, try" ) ,
137
+ format ! ( "if you wanted {suggested_type } of len {end_snippet}, try" ) ,
144
138
format ! ( "{start_snippet}; {end_snippet}" ) ,
145
- app ,
139
+ Applicability :: MaybeIncorrect ,
146
140
) ;
147
141
}
148
142
} ,
0 commit comments