Skip to content

Commit 0fff522

Browse files
authored
Rollup merge of #5637 - montrivo:feature/vec_resize_to_zero, r=yaahc,flip1995
new lint: vec_resize_to_zero implements #5444 changelog: new lint vec_resize_to_zero
2 parents 5713574 + 5faab87 commit 0fff522

File tree

7 files changed

+101
-0
lines changed

7 files changed

+101
-0
lines changed

CHANGELOG.md

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

+5
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;
@@ -847,6 +848,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
847848
&utils::internal_lints::OUTER_EXPN_EXPN_DATA,
848849
&utils::internal_lints::PRODUCE_ICE,
849850
&vec::USELESS_VEC,
851+
&vec_resize_to_zero::VEC_RESIZE_TO_ZERO,
850852
&verbose_file_reads::VERBOSE_FILE_READS,
851853
&wildcard_dependencies::WILDCARD_DEPENDENCIES,
852854
&wildcard_imports::ENUM_GLOB_USE,
@@ -1062,6 +1064,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10621064
store.register_early_pass(|| box manual_non_exhaustive::ManualNonExhaustive);
10631065
store.register_late_pass(|| box manual_async_fn::ManualAsyncFn);
10641066
store.register_early_pass(|| box redundant_field_names::RedundantFieldNames);
1067+
store.register_late_pass(|| box vec_resize_to_zero::VecResizeToZero);
10651068
let single_char_binding_names_threshold = conf.single_char_binding_names_threshold;
10661069
store.register_early_pass(move || box non_expressive_names::NonExpressiveNames {
10671070
single_char_binding_names_threshold,
@@ -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

+1
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,6 @@ pub const VEC_AS_SLICE: [&str; 4] = ["alloc", "vec", "Vec", "as_slice"];
138138
pub const VEC_DEQUE: [&str; 4] = ["alloc", "collections", "vec_deque", "VecDeque"];
139139
pub const VEC_FROM_ELEM: [&str; 3] = ["alloc", "vec", "from_elem"];
140140
pub const VEC_NEW: [&str; 4] = ["alloc", "vec", "Vec", "new"];
141+
pub const VEC_RESIZE: [&str; 4] = ["alloc", "vec", "Vec", "resize"];
141142
pub const WEAK_ARC: [&str; 3] = ["alloc", "sync", "Weak"];
142143
pub const WEAK_RC: [&str; 3] = ["alloc", "rc", "Weak"];
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use crate::utils::span_lint_and_then;
2+
use if_chain::if_chain;
3+
use rustc_errors::Applicability;
4+
use rustc_hir::{Expr, ExprKind};
5+
use rustc_lint::{LateContext, LateLintPass};
6+
use rustc_session::{declare_lint_pass, declare_tool_lint};
7+
use rustc_span::source_map::Spanned;
8+
9+
use crate::utils::{match_def_path, paths};
10+
use rustc_ast::ast::LitKind;
11+
use rustc_hir as hir;
12+
13+
declare_clippy_lint! {
14+
/// **What it does:** Finds occurences of `Vec::resize(0, an_int)`
15+
///
16+
/// **Why is this bad?** This is probably an argument inversion mistake.
17+
///
18+
/// **Known problems:** None.
19+
///
20+
/// **Example:**
21+
/// ```rust
22+
/// vec!(1, 2, 3, 4, 5).resize(0, 5)
23+
/// ```
24+
pub VEC_RESIZE_TO_ZERO,
25+
correctness,
26+
"emptying a vector with `resize(0, an_int)` instead of `clear()` is probably an argument inversion mistake"
27+
}
28+
29+
declare_lint_pass!(VecResizeToZero => [VEC_RESIZE_TO_ZERO]);
30+
31+
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for VecResizeToZero {
32+
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
33+
if_chain! {
34+
if let hir::ExprKind::MethodCall(path_segment, _, ref args) = expr.kind;
35+
if let Some(method_def_id) = cx.tables.type_dependent_def_id(expr.hir_id);
36+
if match_def_path(cx, method_def_id, &paths::VEC_RESIZE) && args.len() == 3;
37+
if let ExprKind::Lit(Spanned { node: LitKind::Int(0, _), .. }) = args[1].kind;
38+
if let ExprKind::Lit(Spanned { node: LitKind::Int(..), .. }) = args[2].kind;
39+
then {
40+
let method_call_span = expr.span.with_lo(path_segment.ident.span.lo());
41+
span_lint_and_then(
42+
cx,
43+
VEC_RESIZE_TO_ZERO,
44+
expr.span,
45+
"emptying a vector with `resize`",
46+
|db| {
47+
db.help("the arguments may be inverted...");
48+
db.span_suggestion(
49+
method_call_span,
50+
"...or you can empty the vector with",
51+
"clear()".to_string(),
52+
Applicability::MaybeIncorrect,
53+
);
54+
},
55+
);
56+
}
57+
}
58+
}
59+
}

src/lintlist/mod.rs

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

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

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: emptying a vector with `resize`
2+
--> $DIR/vec_resize_to_zero.rs:5:5
3+
|
4+
LL | vec![1, 2, 3, 4, 5].resize(0, 5);
5+
| ^^^^^^^^^^^^^^^^^^^^------------
6+
| |
7+
| help: ...or you can empty the vector with: `clear()`
8+
|
9+
= note: `-D clippy::vec-resize-to-zero` implied by `-D warnings`
10+
= help: the arguments may be inverted...
11+
12+
error: aborting due to previous error
13+

0 commit comments

Comments
 (0)