Skip to content

Commit c44eafd

Browse files
committed
Use id instead of name
1 parent 4bbd7e4 commit c44eafd

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

clippy_lints/src/vec_init_then_push.rs

Lines changed: 11 additions & 12 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,
@@ -86,12 +88,12 @@ impl LateLintPass<'_> for VecInitThenPush {
8688
if_chain! {
8789
if !in_external_macro(cx.sess(), local.span);
8890
if let Some(init) = local.init;
89-
if let PatKind::Binding(BindingAnnotation::Mutable, _, ident, None) = local.pat.kind;
91+
if let PatKind::Binding(BindingAnnotation::Mutable, id, _, None) = local.pat.kind;
9092
if let Some(init_kind) = get_vec_init_kind(cx, init);
9193
then {
9294
self.searcher = Some(VecPushSearcher {
95+
local_id: id,
9396
init: init_kind,
94-
name: ident.name,
9597
lhs_is_local: true,
9698
lhs_span: local.ty.map_or(local.pat.span, |t| local.pat.span.to(t.span)),
9799
err_span: local.span,
@@ -106,13 +108,12 @@ impl LateLintPass<'_> for VecInitThenPush {
106108
if_chain! {
107109
if !in_external_macro(cx.sess(), expr.span);
108110
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);
111+
if let Some(id) = path_to_local(left);
111112
if let Some(init_kind) = get_vec_init_kind(cx, right);
112113
then {
113114
self.searcher = Some(VecPushSearcher {
115+
local_id: id,
114116
init: init_kind,
115-
name: name.ident.name,
116117
lhs_is_local: false,
117118
lhs_span: left.span,
118119
err_span: expr.span,
@@ -128,10 +129,8 @@ impl LateLintPass<'_> for VecInitThenPush {
128129
if_chain! {
129130
if let StmtKind::Expr(expr) | StmtKind::Semi(expr) = stmt.kind;
130131
if let ExprKind::MethodCall(path, _, [self_arg, _], _) = expr.kind;
132+
if path_to_local_id(self_arg, searcher.local_id);
131133
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;
135134
then {
136135
self.searcher = Some(VecPushSearcher {
137136
found: searcher.found + 1,

0 commit comments

Comments
 (0)