@@ -90,6 +90,7 @@ mod unwrap_or_else_default;
9090mod unwrap_used;
9191mod useless_asref;
9292mod utils;
93+ mod vec_resize_to_zero;
9394mod wrong_self_convention;
9495mod zst_offset;
9596
@@ -2907,6 +2908,28 @@ declare_clippy_lint! {
29072908 "Use of `Vec::sort_by` when `Vec::sort_by_key` or `Vec::sort` would be clearer"
29082909}
29092910
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+
29102933pub struct Methods {
29112934 avoid_breaking_exported_api : bool ,
29122935 msrv : Option < RustcVersion > ,
@@ -3026,6 +3049,7 @@ impl_lint_pass!(Methods => [
30263049 STABLE_SORT_PRIMITIVE ,
30273050 UNIT_HASH ,
30283051 UNNECESSARY_SORT_BY ,
3052+ VEC_RESIZE_TO_ZERO ,
30293053] ) ;
30303054
30313055/// Extracts a method call name, args, and `Span` of the method name.
@@ -3420,6 +3444,9 @@ impl Methods {
34203444 ( "repeat" , [ arg] ) => {
34213445 repeat_once:: check ( cx, expr, recv, arg) ;
34223446 } ,
3447+ ( "resize" , [ count_arg, default_arg] ) => {
3448+ vec_resize_to_zero:: check ( cx, expr, count_arg, default_arg, span) ;
3449+ } ,
34233450 ( "sort" , [ ] ) => {
34243451 stable_sort_primitive:: check ( cx, expr, recv) ;
34253452 } ,
0 commit comments