Skip to content

Commit d995730

Browse files
committed
Move VecResizeToZero into Methods lint pass
1 parent b9bd0d8 commit d995730

9 files changed

+83
-73
lines changed

clippy_lints/src/lib.register_all.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
223223
LintId::of(methods::UNNECESSARY_TO_OWNED),
224224
LintId::of(methods::UNWRAP_OR_ELSE_DEFAULT),
225225
LintId::of(methods::USELESS_ASREF),
226+
LintId::of(methods::VEC_RESIZE_TO_ZERO),
226227
LintId::of(methods::WRONG_SELF_CONVENTION),
227228
LintId::of(methods::ZST_OFFSET),
228229
LintId::of(minmax::MIN_MAX),
@@ -335,7 +336,6 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
335336
LintId::of(useless_conversion::USELESS_CONVERSION),
336337
LintId::of(vec::USELESS_VEC),
337338
LintId::of(vec_init_then_push::VEC_INIT_THEN_PUSH),
338-
LintId::of(vec_resize_to_zero::VEC_RESIZE_TO_ZERO),
339339
LintId::of(write::PRINTLN_EMPTY_STRING),
340340
LintId::of(write::PRINT_LITERAL),
341341
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
@@ -47,6 +47,7 @@ store.register_group(true, "clippy::correctness", Some("clippy_correctness"), ve
4747
LintId::of(methods::SUSPICIOUS_SPLITN),
4848
LintId::of(methods::UNINIT_ASSUMED_INIT),
4949
LintId::of(methods::UNIT_HASH),
50+
LintId::of(methods::VEC_RESIZE_TO_ZERO),
5051
LintId::of(methods::ZST_OFFSET),
5152
LintId::of(minmax::MIN_MAX),
5253
LintId::of(misc::CMP_NAN),
@@ -72,5 +73,4 @@ store.register_group(true, "clippy::correctness", Some("clippy_correctness"), ve
7273
LintId::of(unnamed_address::VTABLE_ADDRESS_COMPARISONS),
7374
LintId::of(unused_io_amount::UNUSED_IO_AMOUNT),
7475
LintId::of(unwrap::PANICKING_UNWRAP),
75-
LintId::of(vec_resize_to_zero::VEC_RESIZE_TO_ZERO),
7676
])

clippy_lints/src/lib.register_lints.rs

Lines changed: 1 addition & 1 deletion
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,
@@ -563,7 +564,6 @@ store.register_lints(&[
563564
useless_conversion::USELESS_CONVERSION,
564565
vec::USELESS_VEC,
565566
vec_init_then_push::VEC_INIT_THEN_PUSH,
566-
vec_resize_to_zero::VEC_RESIZE_TO_ZERO,
567567
verbose_file_reads::VERBOSE_FILE_READS,
568568
wildcard_imports::ENUM_GLOB_USE,
569569
wildcard_imports::WILDCARD_IMPORTS,

clippy_lints/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,6 @@ mod use_self;
394394
mod useless_conversion;
395395
mod vec;
396396
mod vec_init_then_push;
397-
mod vec_resize_to_zero;
398397
mod verbose_file_reads;
399398
mod wildcard_imports;
400399
mod write;
@@ -784,7 +783,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
784783
store.register_late_pass(|| Box::new(if_not_else::IfNotElse));
785784
store.register_late_pass(|| Box::new(equatable_if_let::PatternEquality));
786785
store.register_late_pass(|| Box::new(manual_async_fn::ManualAsyncFn));
787-
store.register_late_pass(|| Box::new(vec_resize_to_zero::VecResizeToZero));
788786
store.register_late_pass(|| Box::new(panic_in_result_fn::PanicInResultFn));
789787
let single_char_binding_names_threshold = conf.single_char_binding_names_threshold;
790788
store.register_early_pass(move || {

clippy_lints/src/methods/mod.rs

Lines changed: 22 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

@@ -2765,6 +2766,23 @@ declare_clippy_lint! {
27652766
"Use of `Vec::sort_by` when `Vec::sort_by_key` or `Vec::sort` would be clearer"
27662767
}
27672768

2769+
declare_clippy_lint! {
2770+
/// ### What it does
2771+
/// Finds occurrences of `Vec::resize(0, an_int)`
2772+
///
2773+
/// ### Why is this bad?
2774+
/// This is probably an argument inversion mistake.
2775+
///
2776+
/// ### Example
2777+
/// ```rust
2778+
/// vec!(1, 2, 3, 4, 5).resize(0, 5)
2779+
/// ```
2780+
#[clippy::version = "1.46.0"]
2781+
pub VEC_RESIZE_TO_ZERO,
2782+
correctness,
2783+
"emptying a vector with `resize(0, an_int)` instead of `clear()` is probably an argument inversion mistake"
2784+
}
2785+
27682786
pub struct Methods {
27692787
avoid_breaking_exported_api: bool,
27702788
msrv: Option<RustcVersion>,
@@ -2881,6 +2899,7 @@ impl_lint_pass!(Methods => [
28812899
STABLE_SORT_PRIMITIVE,
28822900
UNIT_HASH,
28832901
UNNECESSARY_SORT_BY,
2902+
VEC_RESIZE_TO_ZERO,
28842903
]);
28852904

28862905
/// Extracts a method call name, args, and `Span` of the method name.
@@ -3271,6 +3290,9 @@ impl Methods {
32713290
("repeat", [arg]) => {
32723291
repeat_once::check(cx, expr, recv, arg);
32733292
},
3293+
("resize", [count_arg, default_arg]) => {
3294+
vec_resize_to_zero::check(cx, expr, count_arg, default_arg, span);
3295+
},
32743296
("sort", []) => {
32753297
stable_sort_primitive::check(cx, expr, recv);
32763298
},
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 & 59 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)