Skip to content

Commit 8acc4d2

Browse files
committed
Move VecResizeToZero into Methods lint pass
1 parent d8d4a13 commit 8acc4d2

9 files changed

+88
-78
lines changed

clippy_lints/src/lib.register_all.rs

+1-1
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),
@@ -344,7 +345,6 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
344345
LintId::of(useless_conversion::USELESS_CONVERSION),
345346
LintId::of(vec::USELESS_VEC),
346347
LintId::of(vec_init_then_push::VEC_INIT_THEN_PUSH),
347-
LintId::of(vec_resize_to_zero::VEC_RESIZE_TO_ZERO),
348348
LintId::of(write::POSITIONAL_NAMED_FORMAT_PARAMETERS),
349349
LintId::of(write::PRINTLN_EMPTY_STRING),
350350
LintId::of(write::PRINT_LITERAL),

clippy_lints/src/lib.register_correctness.rs

+1-1
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ store.register_lints(&[
369369
methods::UNWRAP_OR_ELSE_DEFAULT,
370370
methods::UNWRAP_USED,
371371
methods::USELESS_ASREF,
372+
methods::VEC_RESIZE_TO_ZERO,
372373
methods::WRONG_SELF_CONVENTION,
373374
methods::ZST_OFFSET,
374375
minmax::MIN_MAX,
@@ -584,7 +585,6 @@ store.register_lints(&[
584585
useless_conversion::USELESS_CONVERSION,
585586
vec::USELESS_VEC,
586587
vec_init_then_push::VEC_INIT_THEN_PUSH,
587-
vec_resize_to_zero::VEC_RESIZE_TO_ZERO,
588588
verbose_file_reads::VERBOSE_FILE_READS,
589589
wildcard_imports::ENUM_GLOB_USE,
590590
wildcard_imports::WILDCARD_IMPORTS,

clippy_lints/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,6 @@ mod use_self;
391391
mod useless_conversion;
392392
mod vec;
393393
mod vec_init_then_push;
394-
mod vec_resize_to_zero;
395394
mod verbose_file_reads;
396395
mod wildcard_imports;
397396
mod write;
@@ -803,7 +802,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
803802
store.register_late_pass(|| Box::new(if_not_else::IfNotElse));
804803
store.register_late_pass(|| Box::new(equatable_if_let::PatternEquality));
805804
store.register_late_pass(|| Box::new(manual_async_fn::ManualAsyncFn));
806-
store.register_late_pass(|| Box::new(vec_resize_to_zero::VecResizeToZero));
807805
store.register_late_pass(|| Box::new(panic_in_result_fn::PanicInResultFn));
808806
let single_char_binding_names_threshold = conf.single_char_binding_names_threshold;
809807
store.register_early_pass(move || {

clippy_lints/src/methods/mod.rs

+27
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ mod unwrap_or_else_default;
9090
mod unwrap_used;
9191
mod useless_asref;
9292
mod utils;
93+
mod vec_resize_to_zero;
9394
mod wrong_self_convention;
9495
mod 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+
29102933
pub 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
},
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

-64
This file was deleted.

tests/ui/vec_resize_to_zero.rs

+8-4
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

+5-5
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)