Skip to content

Commit fb6e6db

Browse files
committed
new lint: vec_resize_to_zero
1 parent 1831385 commit fb6e6db

File tree

7 files changed

+90
-0
lines changed

7 files changed

+90
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,6 +1630,7 @@ Released 2018-09-13
16301630
[`useless_transmute`]: https://rust-lang.github.io/rust-clippy/master/index.html#useless_transmute
16311631
[`useless_vec`]: https://rust-lang.github.io/rust-clippy/master/index.html#useless_vec
16321632
[`vec_box`]: https://rust-lang.github.io/rust-clippy/master/index.html#vec_box
1633+
[`vec_resize_to_zero`]: https://rust-lang.github.io/rust-clippy/master/index.html#vec_resize_to_zero
16331634
[`verbose_bit_mask`]: https://rust-lang.github.io/rust-clippy/master/index.html#verbose_bit_mask
16341635
[`verbose_file_reads`]: https://rust-lang.github.io/rust-clippy/master/index.html#verbose_file_reads
16351636
[`vtable_address_comparisons`]: https://rust-lang.github.io/rust-clippy/master/index.html#vtable_address_comparisons

clippy_lints/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ mod unwrap;
325325
mod use_self;
326326
mod useless_conversion;
327327
mod vec;
328+
mod vec_resize_to_zero;
328329
mod verbose_file_reads;
329330
mod wildcard_dependencies;
330331
mod wildcard_imports;
@@ -852,6 +853,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
852853
&utils::internal_lints::OUTER_EXPN_EXPN_DATA,
853854
&utils::internal_lints::PRODUCE_ICE,
854855
&vec::USELESS_VEC,
856+
&vec_resize_to_zero::VEC_RESIZE_TO_ZERO,
855857
&verbose_file_reads::VERBOSE_FILE_READS,
856858
&wildcard_dependencies::WILDCARD_DEPENDENCIES,
857859
&wildcard_imports::ENUM_GLOB_USE,
@@ -1066,6 +1068,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10661068
store.register_late_pass(|| box match_on_vec_items::MatchOnVecItems);
10671069
store.register_early_pass(|| box manual_non_exhaustive::ManualNonExhaustive);
10681070
store.register_late_pass(|| box manual_async_fn::ManualAsyncFn);
1071+
store.register_late_pass(|| box vec_resize_to_zero::VecResizeToZero);
10691072

10701073
store.register_group(true, "clippy::restriction", Some("clippy_restriction"), vec![
10711074
LintId::of(&arithmetic::FLOAT_ARITHMETIC),
@@ -1430,6 +1433,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
14301433
LintId::of(&unwrap::UNNECESSARY_UNWRAP),
14311434
LintId::of(&useless_conversion::USELESS_CONVERSION),
14321435
LintId::of(&vec::USELESS_VEC),
1436+
LintId::of(&vec_resize_to_zero::VEC_RESIZE_TO_ZERO),
14331437
LintId::of(&write::PRINTLN_EMPTY_STRING),
14341438
LintId::of(&write::PRINT_LITERAL),
14351439
LintId::of(&write::PRINT_WITH_NEWLINE),
@@ -1677,6 +1681,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
16771681
LintId::of(&unnamed_address::VTABLE_ADDRESS_COMPARISONS),
16781682
LintId::of(&unused_io_amount::UNUSED_IO_AMOUNT),
16791683
LintId::of(&unwrap::PANICKING_UNWRAP),
1684+
LintId::of(&vec_resize_to_zero::VEC_RESIZE_TO_ZERO),
16801685
]);
16811686

16821687
store.register_group(true, "clippy::perf", Some("clippy_perf"), vec![

clippy_lints/src/utils/paths.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,6 @@ pub const VEC_AS_SLICE: [&str; 4] = ["alloc", "vec", "Vec", "as_slice"];
136136
pub const VEC_DEQUE: [&str; 4] = ["alloc", "collections", "vec_deque", "VecDeque"];
137137
pub const VEC_FROM_ELEM: [&str; 3] = ["alloc", "vec", "from_elem"];
138138
pub const VEC_NEW: [&str; 4] = ["alloc", "vec", "Vec", "new"];
139+
pub const VEC_RESIZE: [&str; 4] = ["alloc", "vec", "Vec", "resize"];
139140
pub const WEAK_ARC: [&str; 3] = ["alloc", "sync", "Weak"];
140141
pub const WEAK_RC: [&str; 3] = ["alloc", "rc", "Weak"];
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use crate::utils::span_lint;
2+
use if_chain::if_chain;
3+
use rustc_hir::{Expr, ExprKind};
4+
use rustc_lint::{LateContext, LateLintPass};
5+
use rustc_session::{declare_lint_pass, declare_tool_lint};
6+
use rustc_span::source_map::Spanned;
7+
8+
use crate::utils::{match_def_path, paths};
9+
use rustc_ast::ast::LitKind;
10+
use rustc_hir as hir;
11+
12+
declare_clippy_lint! {
13+
/// **What it does:** Finds occurences of `Vec::resize(0, an_int)`
14+
///
15+
/// **Why is this bad?** This is probably an argument inversion mistake.
16+
///
17+
/// **Known problems:** None.
18+
///
19+
/// **Example:**
20+
/// ```rust
21+
/// vec!(1, 2, 3, 4, 5).resize(0, 5)
22+
/// ```
23+
pub VEC_RESIZE_TO_ZERO,
24+
correctness,
25+
"emptying a vector with `resize(0, an_int)` instead of `clear()` is probably an argument inversion mistake"
26+
}
27+
28+
declare_lint_pass!(VecResizeToZero => [VEC_RESIZE_TO_ZERO]);
29+
30+
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for VecResizeToZero {
31+
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
32+
if_chain! {
33+
if let hir::ExprKind::MethodCall(_, _, ref args) = expr.kind;
34+
if let Some(method_def_id) = cx.tables.type_dependent_def_id(expr.hir_id);
35+
if match_def_path(cx, method_def_id, &paths::VEC_RESIZE) && args.len() == 3;
36+
if let ExprKind::Lit(Spanned { node: LitKind::Int(0, _), .. }) = args[1].kind;
37+
if let ExprKind::Lit(Spanned { node: LitKind::Int(..), .. }) = args[2].kind;
38+
then {
39+
span_lint(cx, VEC_RESIZE_TO_ZERO, expr.span, "this empties the vector. It could be an argument inversion mistake. If not, call `clear()` instead.");
40+
}
41+
}
42+
}
43+
}

src/lintlist/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2460,6 +2460,13 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
24602460
deprecation: None,
24612461
module: "types",
24622462
},
2463+
Lint {
2464+
name: "vec_resize_to_zero",
2465+
group: "correctness",
2466+
desc: "emptying a vector with `resize(0, an_int)` instead of `clear()` is probably an argument inversion mistake",
2467+
deprecation: None,
2468+
module: "vec_resize_to_zero",
2469+
},
24632470
Lint {
24642471
name: "verbose_bit_mask",
24652472
group: "style",

tests/ui/vec_resize_to_zero.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![warn(clippy::vec_resize_to_zero)]
2+
3+
fn main() {
4+
// applicable here
5+
vec![1, 2, 3, 4, 5].resize(0, 5);
6+
7+
// not applicable
8+
vec![1, 2, 3, 4, 5].resize(2, 5);
9+
10+
// applicable here, but only implemented for integer litterals for now
11+
vec!["foo", "bar", "baz"].resize(0, "bar");
12+
13+
// not applicable
14+
vec!["foo", "bar", "baz"].resize(2, "bar")
15+
}

tests/ui/vec_resize_to_zero.stderr

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: this empties the vector. This is probably an argument inversion mistake. If not, call `clear()` instead.
2+
--> $DIR/vec_resize_to_zero.rs:5:5
3+
|
4+
LL | vec![1, 2, 3, 4, 5].resize(0, 5);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `#[deny(clippy::vec_resize_to_zero)]` on by default
8+
9+
error: unknown clippy lint: clippy::vec_resize_to_zero
10+
--> $DIR/vec_resize_to_zero.rs:1:9
11+
|
12+
LL | #![warn(clippy::vec_resize_to_zero)]
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
|
15+
= note: `-D clippy::unknown-clippy-lints` implied by `-D warnings`
16+
17+
error: aborting due to 2 previous errors
18+

0 commit comments

Comments
 (0)