@@ -11,8 +11,8 @@ use clippy_utils::{get_parent_expr, higher, is_trait_method};
11
11
use rustc_errors:: Applicability ;
12
12
use rustc_hir:: { BorrowKind , Expr , ExprKind , HirId , Mutability , Node , PatKind } ;
13
13
use rustc_lint:: { LateContext , LateLintPass } ;
14
+ use rustc_middle:: ty;
14
15
use rustc_middle:: ty:: layout:: LayoutOf ;
15
- use rustc_middle:: ty:: { self , Ty } ;
16
16
use rustc_session:: impl_lint_pass;
17
17
use rustc_span:: { sym, DesugaringKind , Span } ;
18
18
@@ -79,7 +79,6 @@ impl<'tcx> LateLintPass<'tcx> for UselessVec {
79
79
// this is to avoid compile errors when doing the suggestion here: let _: Vec<_> = vec![..];
80
80
&& local. ty . is_none ( )
81
81
&& let PatKind :: Binding ( _, id, ..) = local. pat . kind
82
- && is_copy ( cx, vec_type ( cx. typeck_results ( ) . expr_ty_adjusted ( expr. peel_borrows ( ) ) ) )
83
82
{
84
83
let only_slice_uses = for_each_local_use_after_expr ( cx, id, expr. hir_id , |expr| {
85
84
// allow indexing into a vec and some set of allowed method calls that exist on slices, too
@@ -185,6 +184,11 @@ impl UselessVec {
185
184
let snippet = match * vec_args {
186
185
higher:: VecArgs :: Repeat ( elem, len) => {
187
186
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
+
188
192
#[ expect( clippy:: cast_possible_truncation) ]
189
193
if len_constant as u64 * size_of ( cx, elem) > self . too_large_for_stack {
190
194
return ;
@@ -241,12 +245,3 @@ fn size_of(cx: &LateContext<'_>, expr: &Expr<'_>) -> u64 {
241
245
let ty = cx. typeck_results ( ) . expr_ty_adjusted ( expr) ;
242
246
cx. layout_of ( ty) . map_or ( 0 , |l| l. size . bytes ( ) )
243
247
}
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