11use clippy_utils:: diagnostics:: span_lint_and_then;
22use clippy_utils:: higher:: VecArgs ;
33use clippy_utils:: macros:: root_macro_call_first_node;
4- use clippy_utils:: source:: SpanRangeExt ;
4+ use clippy_utils:: source:: { SpanRangeExt , snippet_with_context } ;
55use clippy_utils:: ty:: implements_trait;
66use clippy_utils:: { is_no_std_crate, sym} ;
77use rustc_ast:: { LitIntType , LitKind , UintTy } ;
88use rustc_errors:: Applicability ;
9- use rustc_hir:: { Expr , ExprKind , LangItem , StructTailExpr } ;
9+ use rustc_hir:: { Expr , ExprKind , StructTailExpr } ;
1010use rustc_lint:: { LateContext , LateLintPass } ;
1111use rustc_session:: declare_lint_pass;
1212use rustc_span:: DesugaringKind ;
@@ -87,20 +87,21 @@ impl LateLintPass<'_> for SingleRangeInVecInit {
8787 return ;
8888 } ;
8989
90- let ExprKind :: Struct ( & qpath , [ start, end] , StructTailExpr :: None ) = inner_expr. kind else {
90+ let ExprKind :: Struct ( _ , [ start, end] , StructTailExpr :: None ) = inner_expr. kind else {
9191 return ;
9292 } ;
9393
94- if cx. tcx . qpath_is_lang_item ( qpath, LangItem :: Range )
95- && inner_expr. span . is_desugaring ( DesugaringKind :: RangeExpr )
94+ if inner_expr. span . is_desugaring ( DesugaringKind :: RangeExpr )
9695 && let ty = cx. typeck_results ( ) . expr_ty ( start. expr )
9796 && let Some ( snippet) = span. get_source_text ( cx)
9897 // `is_from_proc_macro` will skip any `vec![]`. Let's not!
9998 && snippet. starts_with ( suggested_type. starts_with ( ) )
10099 && snippet. ends_with ( suggested_type. ends_with ( ) )
101- && let Some ( start_snippet) = start. span . get_source_text ( cx)
102- && let Some ( end_snippet) = end. span . get_source_text ( cx)
103100 {
101+ let mut applicability = Applicability :: MachineApplicable ;
102+ let ( start_snippet, _) = snippet_with_context ( cx, start. expr . span , span. ctxt ( ) , ".." , & mut applicability) ;
103+ let ( end_snippet, _) = snippet_with_context ( cx, end. expr . span , span. ctxt ( ) , ".." , & mut applicability) ;
104+
104105 let should_emit_every_value = if let Some ( step_def_id) = cx. tcx . get_diagnostic_item ( sym:: range_step)
105106 && implements_trait ( cx, ty, step_def_id, & [ ] )
106107 {
@@ -131,7 +132,7 @@ impl LateLintPass<'_> for SingleRangeInVecInit {
131132 span,
132133 "if you wanted a `Vec` that contains the entire range, try" ,
133134 format ! ( "({start_snippet}..{end_snippet}).collect::<std::vec::Vec<{ty}>>()" ) ,
134- Applicability :: MaybeIncorrect ,
135+ applicability ,
135136 ) ;
136137 }
137138
@@ -140,7 +141,7 @@ impl LateLintPass<'_> for SingleRangeInVecInit {
140141 inner_expr. span ,
141142 format ! ( "if you wanted {suggested_type} of len {end_snippet}, try" ) ,
142143 format ! ( "{start_snippet}; {end_snippet}" ) ,
143- Applicability :: MaybeIncorrect ,
144+ applicability ,
144145 ) ;
145146 }
146147 } ,
0 commit comments