Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit eb80ac4

Browse files
committed
Auto merge of rust-lang#6697 - camsteffen:vec-init-push-fp, r=flip1995
Fix vec_init_then_push false positives changelog: Fix vec_init_then_push false positives Fixes rust-lang#6615
2 parents 5f611ce + a42be85 commit eb80ac4

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

clippy_lints/src/vec_init_then_push.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
use crate::utils::{is_type_diagnostic_item, match_def_path, paths, snippet, span_lint_and_sugg};
1+
use crate::utils::{
2+
is_type_diagnostic_item, match_def_path, path_to_local, path_to_local_id, paths, snippet, span_lint_and_sugg,
3+
};
24
use if_chain::if_chain;
35
use rustc_ast::ast::LitKind;
46
use rustc_errors::Applicability;
5-
use rustc_hir::{BindingAnnotation, Block, Expr, ExprKind, Local, PatKind, QPath, Stmt, StmtKind};
7+
use rustc_hir::{BindingAnnotation, Block, Expr, ExprKind, HirId, Local, PatKind, QPath, Stmt, StmtKind};
68
use rustc_lint::{LateContext, LateLintPass, LintContext};
79
use rustc_middle::lint::in_external_macro;
810
use rustc_session::{declare_tool_lint, impl_lint_pass};
9-
use rustc_span::{symbol::sym, Span, Symbol};
11+
use rustc_span::{symbol::sym, Span};
1012
use std::convert::TryInto;
1113

1214
declare_clippy_lint! {
@@ -45,8 +47,8 @@ enum VecInitKind {
4547
WithCapacity(u64),
4648
}
4749
struct VecPushSearcher {
50+
local_id: HirId,
4851
init: VecInitKind,
49-
name: Symbol,
5052
lhs_is_local: bool,
5153
lhs_span: Span,
5254
err_span: Span,
@@ -81,17 +83,20 @@ impl VecPushSearcher {
8183
}
8284

8385
impl LateLintPass<'_> for VecInitThenPush {
84-
fn check_local(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>) {
86+
fn check_block(&mut self, _: &LateContext<'tcx>, _: &'tcx Block<'tcx>) {
8587
self.searcher = None;
88+
}
89+
90+
fn check_local(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>) {
8691
if_chain! {
8792
if !in_external_macro(cx.sess(), local.span);
8893
if let Some(init) = local.init;
89-
if let PatKind::Binding(BindingAnnotation::Mutable, _, ident, None) = local.pat.kind;
94+
if let PatKind::Binding(BindingAnnotation::Mutable, id, _, None) = local.pat.kind;
9095
if let Some(init_kind) = get_vec_init_kind(cx, init);
9196
then {
9297
self.searcher = Some(VecPushSearcher {
98+
local_id: id,
9399
init: init_kind,
94-
name: ident.name,
95100
lhs_is_local: true,
96101
lhs_span: local.ty.map_or(local.pat.span, |t| local.pat.span.to(t.span)),
97102
err_span: local.span,
@@ -106,13 +111,12 @@ impl LateLintPass<'_> for VecInitThenPush {
106111
if_chain! {
107112
if !in_external_macro(cx.sess(), expr.span);
108113
if let ExprKind::Assign(left, right, _) = expr.kind;
109-
if let ExprKind::Path(QPath::Resolved(_, path)) = left.kind;
110-
if let Some(name) = path.segments.get(0);
114+
if let Some(id) = path_to_local(left);
111115
if let Some(init_kind) = get_vec_init_kind(cx, right);
112116
then {
113117
self.searcher = Some(VecPushSearcher {
118+
local_id: id,
114119
init: init_kind,
115-
name: name.ident.name,
116120
lhs_is_local: false,
117121
lhs_span: left.span,
118122
err_span: expr.span,
@@ -128,10 +132,8 @@ impl LateLintPass<'_> for VecInitThenPush {
128132
if_chain! {
129133
if let StmtKind::Expr(expr) | StmtKind::Semi(expr) = stmt.kind;
130134
if let ExprKind::MethodCall(path, _, [self_arg, _], _) = expr.kind;
135+
if path_to_local_id(self_arg, searcher.local_id);
131136
if path.ident.name.as_str() == "push";
132-
if let ExprKind::Path(QPath::Resolved(_, self_path)) = self_arg.kind;
133-
if let [self_name] = self_path.segments;
134-
if self_name.ident.name == searcher.name;
135137
then {
136138
self.searcher = Some(VecPushSearcher {
137139
found: searcher.found + 1,

tests/ui/vec_init_then_push.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,35 @@ fn main() {
1212
cap_err.push(0);
1313
cap_err.push(1);
1414
cap_err.push(2);
15+
if true {
16+
// don't include this one
17+
cap_err.push(3);
18+
}
1519

1620
let mut cap_ok = Vec::with_capacity(10);
1721
cap_ok.push(0);
1822

1923
new_err = Vec::new();
2024
new_err.push(0);
25+
26+
let mut vec = Vec::new();
27+
// control flow at block final expression
28+
if true {
29+
// no lint
30+
vec.push(1);
31+
}
32+
}
33+
34+
pub fn no_lint() -> Vec<i32> {
35+
let mut p = Some(1);
36+
let mut vec = Vec::new();
37+
loop {
38+
match p {
39+
None => return vec,
40+
Some(i) => {
41+
vec.push(i);
42+
p = None;
43+
},
44+
}
45+
}
2146
}

tests/ui/vec_init_then_push.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ LL | | cap_err.push(2);
2424
| |____________________^ help: consider using the `vec![]` macro: `let mut cap_err = vec![..];`
2525

2626
error: calls to `push` immediately after creation
27-
--> $DIR/vec_init_then_push.rs:19:5
27+
--> $DIR/vec_init_then_push.rs:23:5
2828
|
2929
LL | / new_err = Vec::new();
3030
LL | | new_err.push(0);

0 commit comments

Comments
 (0)