Skip to content

Commit 69203c0

Browse files
committed
Move VecResizeToZero into Methods lint pass
1 parent a8dcac7 commit 69203c0

9 files changed

+88
-78
lines changed

clippy_lints/src/lib.register_all.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
217217
LintId::of(methods::UNNECESSARY_TO_OWNED),
218218
LintId::of(methods::UNWRAP_OR_ELSE_DEFAULT),
219219
LintId::of(methods::USELESS_ASREF),
220+
LintId::of(methods::VEC_RESIZE_TO_ZERO),
220221
LintId::of(methods::WRONG_SELF_CONVENTION),
221222
LintId::of(methods::ZST_OFFSET),
222223
LintId::of(minmax::MIN_MAX),
@@ -343,7 +344,6 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
343344
LintId::of(useless_conversion::USELESS_CONVERSION),
344345
LintId::of(vec::USELESS_VEC),
345346
LintId::of(vec_init_then_push::VEC_INIT_THEN_PUSH),
346-
LintId::of(vec_resize_to_zero::VEC_RESIZE_TO_ZERO),
347347
LintId::of(write::PRINTLN_EMPTY_STRING),
348348
LintId::of(write::PRINT_LITERAL),
349349
LintId::of(write::PRINT_WITH_NEWLINE),

clippy_lints/src/lib.register_correctness.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ store.register_group(true, "clippy::correctness", Some("clippy_correctness"), ve
4343
LintId::of(methods::SUSPICIOUS_SPLITN),
4444
LintId::of(methods::UNINIT_ASSUMED_INIT),
4545
LintId::of(methods::UNIT_HASH),
46+
LintId::of(methods::VEC_RESIZE_TO_ZERO),
4647
LintId::of(methods::ZST_OFFSET),
4748
LintId::of(minmax::MIN_MAX),
4849
LintId::of(non_octal_unix_permissions::NON_OCTAL_UNIX_PERMISSIONS),
@@ -74,5 +75,4 @@ store.register_group(true, "clippy::correctness", Some("clippy_correctness"), ve
7475
LintId::of(unnamed_address::VTABLE_ADDRESS_COMPARISONS),
7576
LintId::of(unused_io_amount::UNUSED_IO_AMOUNT),
7677
LintId::of(unwrap::PANICKING_UNWRAP),
77-
LintId::of(vec_resize_to_zero::VEC_RESIZE_TO_ZERO),
7878
])

clippy_lints/src/lib.register_lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ store.register_lints(&[
363363
methods::UNWRAP_OR_ELSE_DEFAULT,
364364
methods::UNWRAP_USED,
365365
methods::USELESS_ASREF,
366+
methods::VEC_RESIZE_TO_ZERO,
366367
methods::WRONG_SELF_CONVENTION,
367368
methods::ZST_OFFSET,
368369
minmax::MIN_MAX,
@@ -573,7 +574,6 @@ store.register_lints(&[
573574
useless_conversion::USELESS_CONVERSION,
574575
vec::USELESS_VEC,
575576
vec_init_then_push::VEC_INIT_THEN_PUSH,
576-
vec_resize_to_zero::VEC_RESIZE_TO_ZERO,
577577
verbose_file_reads::VERBOSE_FILE_READS,
578578
wildcard_imports::ENUM_GLOB_USE,
579579
wildcard_imports::WILDCARD_IMPORTS,

clippy_lints/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,6 @@ mod use_self;
387387
mod useless_conversion;
388388
mod vec;
389389
mod vec_init_then_push;
390-
mod vec_resize_to_zero;
391390
mod verbose_file_reads;
392391
mod wildcard_imports;
393392
mod write;
@@ -787,7 +786,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
787786
store.register_late_pass(|| Box::new(if_not_else::IfNotElse));
788787
store.register_late_pass(|| Box::new(equatable_if_let::PatternEquality));
789788
store.register_late_pass(|| Box::new(manual_async_fn::ManualAsyncFn));
790-
store.register_late_pass(|| Box::new(vec_resize_to_zero::VecResizeToZero));
791789
store.register_late_pass(|| Box::new(panic_in_result_fn::PanicInResultFn));
792790
let single_char_binding_names_threshold = conf.single_char_binding_names_threshold;
793791
store.register_early_pass(move || {

clippy_lints/src/methods/mod.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ mod unwrap_or_else_default;
8888
mod unwrap_used;
8989
mod useless_asref;
9090
mod utils;
91+
mod vec_resize_to_zero;
9192
mod wrong_self_convention;
9293
mod zst_offset;
9394

@@ -2796,6 +2797,28 @@ declare_clippy_lint! {
27962797
"Use of `Vec::sort_by` when `Vec::sort_by_key` or `Vec::sort` would be clearer"
27972798
}
27982799

2800+
declare_clippy_lint! {
2801+
/// ### What it does
2802+
/// Finds occurrences of `Vec::resize(0, an_int)`
2803+
///
2804+
/// ### Why is this bad?
2805+
/// This is probably an argument inversion mistake.
2806+
///
2807+
/// ### Example
2808+
/// ```rust
2809+
/// vec!(1, 2, 3, 4, 5).resize(0, 5)
2810+
/// ```
2811+
///
2812+
/// Use instead:
2813+
/// ```rust
2814+
/// vec!(1, 2, 3, 4, 5).clear()
2815+
/// ```
2816+
#[clippy::version = "1.46.0"]
2817+
pub VEC_RESIZE_TO_ZERO,
2818+
correctness,
2819+
"emptying a vector with `resize(0, an_int)` instead of `clear()` is probably an argument inversion mistake"
2820+
}
2821+
27992822
pub struct Methods {
28002823
avoid_breaking_exported_api: bool,
28012824
msrv: Option<RustcVersion>,
@@ -2912,6 +2935,7 @@ impl_lint_pass!(Methods => [
29122935
STABLE_SORT_PRIMITIVE,
29132936
UNIT_HASH,
29142937
UNNECESSARY_SORT_BY,
2938+
VEC_RESIZE_TO_ZERO,
29152939
]);
29162940

29172941
/// Extracts a method call name, args, and `Span` of the method name.
@@ -3302,6 +3326,9 @@ impl Methods {
33023326
("repeat", [arg]) => {
33033327
repeat_once::check(cx, expr, recv, arg);
33043328
},
3329+
("resize", [count_arg, default_arg]) => {
3330+
vec_resize_to_zero::check(cx, expr, count_arg, default_arg, span);
3331+
},
33053332
("sort", []) => {
33063333
stable_sort_primitive::check(cx, expr, recv);
33073334
},
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use clippy_utils::diagnostics::span_lint_and_then;
2+
use clippy_utils::ty::is_type_diagnostic_item;
3+
use if_chain::if_chain;
4+
use rustc_ast::LitKind;
5+
use rustc_errors::Applicability;
6+
use rustc_hir::{Expr, ExprKind};
7+
use rustc_lint::LateContext;
8+
use rustc_span::source_map::Spanned;
9+
use rustc_span::{sym, Span};
10+
11+
use super::VEC_RESIZE_TO_ZERO;
12+
13+
pub(super) fn check<'tcx>(
14+
cx: &LateContext<'tcx>,
15+
expr: &'tcx Expr<'_>,
16+
count_arg: &'tcx Expr<'_>,
17+
default_arg: &'tcx Expr<'_>,
18+
name_span: Span,
19+
) {
20+
if_chain! {
21+
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
22+
if let Some(impl_id) = cx.tcx.impl_of_method(method_id);
23+
if is_type_diagnostic_item(cx, cx.tcx.type_of(impl_id), sym::Vec);
24+
if let ExprKind::Lit(Spanned { node: LitKind::Int(0, _), .. }) = count_arg.kind;
25+
if let ExprKind::Lit(Spanned { node: LitKind::Int(..), .. }) = default_arg.kind;
26+
then {
27+
let method_call_span = expr.span.with_lo(name_span.lo());
28+
span_lint_and_then(
29+
cx,
30+
VEC_RESIZE_TO_ZERO,
31+
expr.span,
32+
"emptying a vector with `resize`",
33+
|db| {
34+
db.help("the arguments may be inverted...");
35+
db.span_suggestion(
36+
method_call_span,
37+
"...or you can empty the vector with",
38+
"clear()".to_string(),
39+
Applicability::MaybeIncorrect,
40+
);
41+
},
42+
);
43+
}
44+
}
45+
}

clippy_lints/src/vec_resize_to_zero.rs

Lines changed: 0 additions & 64 deletions
This file was deleted.

tests/ui/vec_resize_to_zero.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
#![warn(clippy::vec_resize_to_zero)]
22

33
fn main() {
4+
let mut v = vec![1, 2, 3, 4, 5];
5+
46
// applicable here
5-
vec![1, 2, 3, 4, 5].resize(0, 5);
7+
v.resize(0, 5);
68

79
// not applicable
8-
vec![1, 2, 3, 4, 5].resize(2, 5);
10+
v.resize(2, 5);
11+
12+
let mut v = vec!["foo", "bar", "baz"];
913

1014
// applicable here, but only implemented for integer literals for now
11-
vec!["foo", "bar", "baz"].resize(0, "bar");
15+
v.resize(0, "bar");
1216

1317
// not applicable
14-
vec!["foo", "bar", "baz"].resize(2, "bar")
18+
v.resize(2, "bar")
1519
}

tests/ui/vec_resize_to_zero.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
error: emptying a vector with `resize`
2-
--> $DIR/vec_resize_to_zero.rs:5:5
2+
--> $DIR/vec_resize_to_zero.rs:7:5
33
|
4-
LL | vec![1, 2, 3, 4, 5].resize(0, 5);
5-
| ^^^^^^^^^^^^^^^^^^^^------------
6-
| |
7-
| help: ...or you can empty the vector with: `clear()`
4+
LL | v.resize(0, 5);
5+
| ^^------------
6+
| |
7+
| help: ...or you can empty the vector with: `clear()`
88
|
99
= note: `-D clippy::vec-resize-to-zero` implied by `-D warnings`
1010
= help: the arguments may be inverted...

0 commit comments

Comments
 (0)