@@ -11,8 +11,8 @@ use clippy_utils::{get_parent_expr, higher, is_trait_method};
1111use rustc_errors:: Applicability ;
1212use rustc_hir:: { BorrowKind , Expr , ExprKind , HirId , Mutability , Node , PatKind } ;
1313use rustc_lint:: { LateContext , LateLintPass } ;
14+ use rustc_middle:: ty;
1415use rustc_middle:: ty:: layout:: LayoutOf ;
15- use rustc_middle:: ty:: { self , Ty } ;
1616use rustc_session:: impl_lint_pass;
1717use rustc_span:: { sym, DesugaringKind , Span } ;
1818
@@ -79,7 +79,6 @@ impl<'tcx> LateLintPass<'tcx> for UselessVec {
7979 // this is to avoid compile errors when doing the suggestion here: let _: Vec<_> = vec![..];
8080 && local. ty . is_none ( )
8181 && let PatKind :: Binding ( _, id, ..) = local. pat . kind
82- && is_copy ( cx, vec_type ( cx. typeck_results ( ) . expr_ty_adjusted ( expr. peel_borrows ( ) ) ) )
8382 {
8483 let only_slice_uses = for_each_local_use_after_expr ( cx, id, expr. hir_id , |expr| {
8584 // allow indexing into a vec and some set of allowed method calls that exist on slices, too
@@ -185,6 +184,11 @@ impl UselessVec {
185184 let snippet = match * vec_args {
186185 higher:: VecArgs :: Repeat ( elem, len) => {
187186 if let Some ( Constant :: Int ( len_constant) ) = constant ( cx, cx. typeck_results ( ) , len) {
187+ // vec![ty; N] works when ty is Clone, [ty; N] requires it to be Copy also
188+ if !is_copy ( cx, cx. typeck_results ( ) . expr_ty ( elem) ) {
189+ return ;
190+ }
191+
188192 #[ expect( clippy:: cast_possible_truncation) ]
189193 if len_constant as u64 * size_of ( cx, elem) > self . too_large_for_stack {
190194 return ;
@@ -241,12 +245,3 @@ fn size_of(cx: &LateContext<'_>, expr: &Expr<'_>) -> u64 {
241245 let ty = cx. typeck_results ( ) . expr_ty_adjusted ( expr) ;
242246 cx. layout_of ( ty) . map_or ( 0 , |l| l. size . bytes ( ) )
243247}
244-
245- /// Returns the item type of the vector (i.e., the `T` in `Vec<T>`).
246- fn vec_type ( ty : Ty < ' _ > ) -> Ty < ' _ > {
247- if let ty:: Adt ( _, args) = ty. kind ( ) {
248- args. type_at ( 0 )
249- } else {
250- panic ! ( "The type of `vec!` is a not a struct?" ) ;
251- }
252- }
0 commit comments