@@ -90,6 +90,7 @@ mod unwrap_or_else_default;
90
90
mod unwrap_used;
91
91
mod useless_asref;
92
92
mod utils;
93
+ mod vec_resize_to_zero;
93
94
mod wrong_self_convention;
94
95
mod zst_offset;
95
96
@@ -2907,6 +2908,28 @@ declare_clippy_lint! {
2907
2908
"Use of `Vec::sort_by` when `Vec::sort_by_key` or `Vec::sort` would be clearer"
2908
2909
}
2909
2910
2911
+ declare_clippy_lint ! {
2912
+ /// ### What it does
2913
+ /// Finds occurrences of `Vec::resize(0, an_int)`
2914
+ ///
2915
+ /// ### Why is this bad?
2916
+ /// This is probably an argument inversion mistake.
2917
+ ///
2918
+ /// ### Example
2919
+ /// ```rust
2920
+ /// vec!(1, 2, 3, 4, 5).resize(0, 5)
2921
+ /// ```
2922
+ ///
2923
+ /// Use instead:
2924
+ /// ```rust
2925
+ /// vec!(1, 2, 3, 4, 5).clear()
2926
+ /// ```
2927
+ #[ clippy:: version = "1.46.0" ]
2928
+ pub VEC_RESIZE_TO_ZERO ,
2929
+ correctness,
2930
+ "emptying a vector with `resize(0, an_int)` instead of `clear()` is probably an argument inversion mistake"
2931
+ }
2932
+
2910
2933
pub struct Methods {
2911
2934
avoid_breaking_exported_api : bool ,
2912
2935
msrv : Option < RustcVersion > ,
@@ -3026,6 +3049,7 @@ impl_lint_pass!(Methods => [
3026
3049
STABLE_SORT_PRIMITIVE ,
3027
3050
UNIT_HASH ,
3028
3051
UNNECESSARY_SORT_BY ,
3052
+ VEC_RESIZE_TO_ZERO ,
3029
3053
] ) ;
3030
3054
3031
3055
/// Extracts a method call name, args, and `Span` of the method name.
@@ -3420,6 +3444,9 @@ impl Methods {
3420
3444
( "repeat" , [ arg] ) => {
3421
3445
repeat_once:: check ( cx, expr, recv, arg) ;
3422
3446
} ,
3447
+ ( "resize" , [ count_arg, default_arg] ) => {
3448
+ vec_resize_to_zero:: check ( cx, expr, count_arg, default_arg, span) ;
3449
+ } ,
3423
3450
( "sort" , [ ] ) => {
3424
3451
stable_sort_primitive:: check ( cx, expr, recv) ;
3425
3452
} ,
0 commit comments