@@ -10,7 +10,7 @@ use clippy_utils::msrvs::{self, Msrv};
1010use clippy_utils:: source:: SpanRangeExt ;
1111use clippy_utils:: ty:: is_copy;
1212use clippy_utils:: visitors:: for_each_local_use_after_expr;
13- use clippy_utils:: { get_parent_expr, higher, is_in_test, span_contains_comment, sym } ;
13+ use clippy_utils:: { VEC_METHODS_SHADOWING_SLICE_METHODS , get_parent_expr, higher, is_in_test, span_contains_comment} ;
1414use rustc_errors:: Applicability ;
1515use rustc_hir:: { BorrowKind , Expr , ExprKind , HirId , LetStmt , Mutability , Node , Pat , PatKind } ;
1616use rustc_lint:: { LateContext , LateLintPass } ;
@@ -123,8 +123,16 @@ impl UselessVec {
123123 // allow indexing into a vec and some set of allowed method calls that exist on slices, too
124124 if let Some ( parent) = get_parent_expr ( cx, expr)
125125 && ( adjusts_to_slice ( cx, expr)
126- || matches ! ( parent. kind, ExprKind :: Index ( ..) )
127- || is_allowed_vec_method ( parent) )
126+ || match parent. kind {
127+ ExprKind :: Index ( ..) => true ,
128+ ExprKind :: MethodCall ( path, _, [ ] , _) => {
129+ // If the given expression is a method call to a `Vec` method that also exists on
130+ // slices, it means that this expression does not actually require a `Vec` and could
131+ // just work with an array.
132+ VEC_METHODS_SHADOWING_SLICE_METHODS . contains ( & path. ident . name )
133+ } ,
134+ _ => false ,
135+ } )
128136 {
129137 ControlFlow :: Continue ( ( ) )
130138 } else {
@@ -144,8 +152,9 @@ impl UselessVec {
144152 VecToArray :: Impossible
145153 } ,
146154 // search for `for _ in vec![...]`
147- Node :: Expr ( Expr { span, .. } )
148- if span. is_desugaring ( DesugaringKind :: ForLoop ) && self . msrv . meets ( cx, msrvs:: ARRAY_INTO_ITERATOR ) =>
155+ Node :: Expr ( expr)
156+ if expr. span . is_desugaring ( DesugaringKind :: ForLoop )
157+ && self . msrv . meets ( cx, msrvs:: ARRAY_INTO_ITERATOR ) =>
149158 {
150159 VecToArray :: Possible
151160 } ,
@@ -276,9 +285,8 @@ impl SuggestedType {
276285 assert ! ( args_span. is_none_or( |s| !s. from_expansion( ) ) ) ;
277286 assert ! ( len_span. is_none_or( |s| !s. from_expansion( ) ) ) ;
278287
279- let maybe_args = args_span
280- . map ( |sp| sp. get_source_text ( cx) . expect ( "spans are always crate-local" ) )
281- . map_or ( String :: new ( ) , |x| x. to_owned ( ) ) ;
288+ let maybe_args = args_span. map ( |sp| sp. get_source_text ( cx) . expect ( "spans are always crate-local" ) ) ;
289+ let maybe_args = maybe_args. as_deref ( ) . unwrap_or_default ( ) ;
282290 let maybe_len = len_span
283291 . map ( |sp| sp. get_source_text ( cx) . expect ( "spans are always crate-local" ) )
284292 . map ( |st| format ! ( "; {st}" ) )
@@ -301,17 +309,6 @@ fn adjusts_to_slice(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
301309 matches ! ( cx. typeck_results( ) . expr_ty_adjusted( e) . kind( ) , ty:: Ref ( _, ty, _) if ty. is_slice( ) )
302310}
303311
304- /// Checks if the given expression is a method call to a `Vec` method
305- /// that also exists on slices. If this returns true, it means that
306- /// this expression does not actually require a `Vec` and could just work with an array.
307- pub fn is_allowed_vec_method ( e : & Expr < ' _ > ) -> bool {
308- if let ExprKind :: MethodCall ( path, _, [ ] , _) = e. kind {
309- matches ! ( path. ident. name, sym:: as_ptr | sym:: is_empty | sym:: len)
310- } else {
311- false
312- }
313- }
314-
315312fn suggest_type ( expr : & Expr < ' _ > ) -> SuggestedType {
316313 if let ExprKind :: AddrOf ( BorrowKind :: Ref , mutability, _) = expr. kind {
317314 // `expr` is `&vec![_]`, so suggest `&[_]` (or `&mut[_]` resp.)
0 commit comments