11use 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} ;
95use rustc_ast:: { LitIntType , LitKind , UintTy } ;
106use rustc_errors:: Applicability ;
@@ -18,9 +14,9 @@ declare_clippy_lint! {
1814 /// Checks for `Vec` or array initializations that contain only one range.
1915 ///
2016 /// ### 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.
2420 ///
2521 /// ### Example
2622 /// ```rust
@@ -75,8 +71,8 @@ impl LateLintPass<'_> for SingleRangeInVecInit {
7571 // ^^^^^^ ^^^^^^^
7672 // span: `vec![0..200]` or `[0..200]`
7773 // ^^^^^^^^^^^^ ^^^^^^^^
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
8076 && !expr. span . from_expansion ( )
8177 {
8278 ( inner_expr, expr. span , SuggestedType :: Array )
@@ -96,13 +92,11 @@ impl LateLintPass<'_> for SingleRangeInVecInit {
9692 && let ty = cx. typeck_results ( ) . expr_ty ( start. expr )
9793 && let Some ( snippet) = snippet_opt ( cx, span)
9894 // `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 )
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 {
@@ -126,23 +120,23 @@ impl LateLintPass<'_> for SingleRangeInVecInit {
126120 cx,
127121 SINGLE_RANGE_IN_VEC_INIT ,
128122 span,
129- & format ! ( "{kind } of `Range` that is only one element" ) ,
123+ & format ! ( "{suggested_type } of `Range` that is only one element" ) ,
130124 |diag| {
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
140134 if should_emit_of_len {
141135 diag. span_suggestion (
142136 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" ) ,
144138 format ! ( "{start_snippet}; {end_snippet}" ) ,
145- app ,
139+ Applicability :: MaybeIncorrect ,
146140 ) ;
147141 }
148142 } ,
0 commit comments