From 8dac8a6ba47b9ea966c013603f4556b2256eb824 Mon Sep 17 00:00:00 2001 From: ljedrz Date: Sun, 24 Feb 2019 19:43:15 +0100 Subject: [PATCH 1/3] HirIdify some lints --- clippy_lints/src/assign_ops.rs | 9 ++- clippy_lints/src/booleans.rs | 3 +- clippy_lints/src/copies.rs | 2 +- clippy_lints/src/cyclomatic_complexity.rs | 3 +- clippy_lints/src/default_trait_access.rs | 2 +- clippy_lints/src/escape.rs | 6 +- clippy_lints/src/eval_order_dependence.rs | 12 ++-- clippy_lints/src/identity_conversion.rs | 9 ++- clippy_lints/src/lifetimes.rs | 2 +- clippy_lints/src/loops.rs | 55 ++++++++++--------- clippy_lints/src/matches.rs | 2 +- clippy_lints/src/methods/mod.rs | 6 +- clippy_lints/src/misc.rs | 4 +- clippy_lints/src/needless_bool.rs | 4 +- clippy_lints/src/needless_pass_by_value.rs | 12 ++-- clippy_lints/src/new_without_default.rs | 9 +-- clippy_lints/src/non_copy_const.rs | 8 +-- clippy_lints/src/partialeq_ne_impl.rs | 3 +- clippy_lints/src/regex.rs | 8 +-- .../src/slow_vector_initialization.rs | 10 ++-- clippy_lints/src/strings.rs | 2 +- clippy_lints/src/suspicious_trait_impl.rs | 17 +++--- clippy_lints/src/types.rs | 10 ++-- clippy_lints/src/unicode.rs | 6 +- clippy_lints/src/utils/diagnostics.rs | 12 ++-- clippy_lints/src/utils/mod.rs | 34 ++++++------ clippy_lints/src/utils/usage.rs | 6 +- 27 files changed, 130 insertions(+), 126 deletions(-) diff --git a/clippy_lints/src/assign_ops.rs b/clippy_lints/src/assign_ops.rs index 2ef2a1847461..376751f860d1 100644 --- a/clippy_lints/src/assign_ops.rs +++ b/clippy_lints/src/assign_ops.rs @@ -6,7 +6,6 @@ use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor}; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use rustc::{declare_tool_lint, lint_array}; use rustc_errors::Applicability; -use syntax::ast; /// **What it does:** Checks for `a = a op b` or `a = b commutative_op a` /// patterns. @@ -140,12 +139,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps { return; // useless if the trait doesn't exist }; // check that we are not inside an `impl AssignOp` of this exact operation - let parent_fn = cx.tcx.hir().get_parent(e.id); - let parent_impl = cx.tcx.hir().get_parent(parent_fn); + let parent_fn = cx.tcx.hir().get_parent_item(e.hir_id); + let parent_impl = cx.tcx.hir().get_parent_item(parent_fn); // the crate node is the only one that is not in the map if_chain! { - if parent_impl != ast::CRATE_NODE_ID; - if let hir::Node::Item(item) = cx.tcx.hir().get(parent_impl); + if parent_impl != hir::CRATE_HIR_ID; + if let hir::Node::Item(item) = cx.tcx.hir().get_by_hir_id(parent_impl); if let hir::ItemKind::Impl(_, _, _, _, Some(trait_ref), _, _) = &item.node; if trait_ref.path.def.def_id() == trait_id; diff --git a/clippy_lints/src/booleans.rs b/clippy_lints/src/booleans.rs index a2559c774011..3b76f288b6b3 100644 --- a/clippy_lints/src/booleans.rs +++ b/clippy_lints/src/booleans.rs @@ -7,7 +7,7 @@ use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use rustc::{declare_tool_lint, lint_array}; use rustc_data_structures::thin_vec::ThinVec; use rustc_errors::Applicability; -use syntax::ast::{LitKind, DUMMY_NODE_ID}; +use syntax::ast::LitKind; use syntax::source_map::{dummy_spanned, Span, DUMMY_SP}; /// **What it does:** Checks for boolean expressions that can be written more @@ -132,7 +132,6 @@ impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> { } let mk_expr = |op| Expr { - id: DUMMY_NODE_ID, hir_id: DUMMY_HIR_ID, span: DUMMY_SP, attrs: ThinVec::new(), diff --git a/clippy_lints/src/copies.rs b/clippy_lints/src/copies.rs index 41afe5ce0d79..2c4b335c1326 100644 --- a/clippy_lints/src/copies.rs +++ b/clippy_lints/src/copies.rs @@ -125,7 +125,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CopyAndPaste { .. }) = get_parent_expr(cx, expr) { - if else_expr.id == expr.id { + if else_expr.hir_id == expr.hir_id { return; } } diff --git a/clippy_lints/src/cyclomatic_complexity.rs b/clippy_lints/src/cyclomatic_complexity.rs index da86734cac1e..502937878530 100644 --- a/clippy_lints/src/cyclomatic_complexity.rs +++ b/clippy_lints/src/cyclomatic_complexity.rs @@ -222,8 +222,7 @@ fn report_cc_bug( span: Span, id: HirId, ) { - let node_id = cx.tcx.hir().hir_to_node_id(id); - if !is_allowed(cx, CYCLOMATIC_COMPLEXITY, node_id) { + if !is_allowed(cx, CYCLOMATIC_COMPLEXITY, id) { cx.sess().span_note_without_error( span, &format!( diff --git a/clippy_lints/src/default_trait_access.rs b/clippy_lints/src/default_trait_access.rs index 7b3899b2f499..3fa2ceda5d7d 100644 --- a/clippy_lints/src/default_trait_access.rs +++ b/clippy_lints/src/default_trait_access.rs @@ -45,7 +45,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DefaultTraitAccess { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { if_chain! { if let ExprKind::Call(ref path, ..) = expr.node; - if !any_parent_is_automatically_derived(cx.tcx, expr.id); + if !any_parent_is_automatically_derived(cx.tcx, expr.hir_id); if let ExprKind::Path(ref qpath) = path.node; if let Some(def_id) = opt_def_id(cx.tables.qpath_def(qpath, path.hir_id)); if match_def_path(cx.tcx, def_id, &paths::DEFAULT_TRAIT_METHOD); diff --git a/clippy_lints/src/escape.rs b/clippy_lints/src/escape.rs index a39ee6d18eea..3f49cbdc0f2c 100644 --- a/clippy_lints/src/escape.rs +++ b/clippy_lints/src/escape.rs @@ -100,7 +100,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass { } impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> { - fn consume(&mut self, _: NodeId, _: Span, cmt: &cmt_<'tcx>, mode: ConsumeMode) { + fn consume(&mut self, _: HirId, _: Span, cmt: &cmt_<'tcx>, mode: ConsumeMode) { if let Categorization::Local(lid) = cmt.cat { if let Move(DirectRefMove) = mode { // moved out or in. clearly can't be localized @@ -150,7 +150,7 @@ impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> { } fn borrow( &mut self, - _: NodeId, + _: HirId, _: Span, cmt: &cmt_<'tcx>, _: ty::Region<'_>, @@ -178,7 +178,7 @@ impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> { } } fn decl_without_init(&mut self, _: NodeId, _: Span) {} - fn mutate(&mut self, _: NodeId, _: Span, _: &cmt_<'tcx>, _: MutateMode) {} + fn mutate(&mut self, _: HirId, _: Span, _: &cmt_<'tcx>, _: MutateMode) {} } impl<'a, 'tcx> EscapeDelegate<'a, 'tcx> { diff --git a/clippy_lints/src/eval_order_dependence.rs b/clippy_lints/src/eval_order_dependence.rs index 8c933ef74d24..81e653b6c42c 100644 --- a/clippy_lints/src/eval_order_dependence.rs +++ b/clippy_lints/src/eval_order_dependence.rs @@ -186,13 +186,13 @@ impl<'a, 'tcx> Visitor<'tcx> for DivergenceVisitor<'a, 'tcx> { /// When such a read is found, the lint is triggered. fn check_for_unsequenced_reads(vis: &mut ReadVisitor<'_, '_>) { let map = &vis.cx.tcx.hir(); - let mut cur_id = vis.write_expr.id; + let mut cur_id = vis.write_expr.hir_id; loop { - let parent_id = map.get_parent_node(cur_id); + let parent_id = map.get_parent_node_by_hir_id(cur_id); if parent_id == cur_id { break; } - let parent_node = match map.find(parent_id) { + let parent_node = match map.find_by_hir_id(parent_id) { Some(parent) => parent, None => break, }; @@ -224,7 +224,7 @@ enum StopEarly { } fn check_expr<'a, 'tcx>(vis: &mut ReadVisitor<'a, 'tcx>, expr: &'tcx Expr) -> StopEarly { - if expr.id == vis.last_expr.id { + if expr.hir_id == vis.last_expr.hir_id { return StopEarly::KeepGoing; } @@ -298,7 +298,7 @@ struct ReadVisitor<'a, 'tcx: 'a> { impl<'a, 'tcx> Visitor<'tcx> for ReadVisitor<'a, 'tcx> { fn visit_expr(&mut self, expr: &'tcx Expr) { - if expr.id == self.last_expr.id { + if expr.hir_id == self.last_expr.hir_id { return; } @@ -355,7 +355,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ReadVisitor<'a, 'tcx> { fn is_in_assignment_position(cx: &LateContext<'_, '_>, expr: &Expr) -> bool { if let Some(parent) = get_parent_expr(cx, expr) { if let ExprKind::Assign(ref lhs, _) = parent.node { - return lhs.id == expr.id; + return lhs.hir_id == expr.hir_id; } } false diff --git a/clippy_lints/src/identity_conversion.rs b/clippy_lints/src/identity_conversion.rs index abe8a9d6856c..b09031b553f9 100644 --- a/clippy_lints/src/identity_conversion.rs +++ b/clippy_lints/src/identity_conversion.rs @@ -6,7 +6,6 @@ use rustc::hir::*; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use rustc::{declare_tool_lint, lint_array}; use rustc_errors::Applicability; -use syntax::ast::NodeId; /// **What it does:** Checks for always-identical `Into`/`From`/`IntoIter` conversions. /// @@ -27,7 +26,7 @@ declare_clippy_lint! { #[derive(Default)] pub struct IdentityConversion { - try_desugar_arm: Vec, + try_desugar_arm: Vec, } impl LintPass for IdentityConversion { @@ -46,7 +45,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion { return; } - if Some(&e.id) == self.try_desugar_arm.last() { + if Some(&e.hir_id) == self.try_desugar_arm.last() { return; } @@ -57,7 +56,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion { _ => return, }; if let ExprKind::Call(_, ref args) = e.node { - self.try_desugar_arm.push(args[0].id); + self.try_desugar_arm.push(args[0].hir_id); } else { return; } @@ -126,7 +125,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion { } fn check_expr_post(&mut self, _: &LateContext<'a, 'tcx>, e: &'tcx Expr) { - if Some(&e.id) == self.try_desugar_arm.last() { + if Some(&e.hir_id) == self.try_desugar_arm.last() { self.try_desugar_arm.pop(); } } diff --git a/clippy_lints/src/lifetimes.rs b/clippy_lints/src/lifetimes.rs index db9dbea97dc7..34de33bd546a 100644 --- a/clippy_lints/src/lifetimes.rs +++ b/clippy_lints/src/lifetimes.rs @@ -319,7 +319,7 @@ impl<'v, 't> RefVisitor<'v, 't> { _ => false, }) { - let hir_id = self.cx.tcx.hir().node_to_hir_id(ty.id); + let hir_id = ty.hir_id; match self.cx.tables.qpath_def(qpath, hir_id) { Def::TyAlias(def_id) | Def::Struct(def_id) => { let generics = self.cx.tcx.generics_of(def_id); diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index fcf6a879b915..9439a3ff4546 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -486,7 +486,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass { // check for never_loop match expr.node { ExprKind::While(_, ref block, _) | ExprKind::Loop(ref block, _, _) => { - match never_loop_block(block, expr.id) { + let node_id = cx.tcx.hir().hir_to_node_id(expr.hir_id); + match never_loop_block(block, node_id) { NeverLoopResult::AlwaysBreak => { span_lint(cx, NEVER_LOOP, expr.span, "this loop never actually loops") }, @@ -1109,8 +1110,8 @@ fn check_for_loop_range<'a, 'tcx>( // ensure that the indexed variable was declared before the loop, see #601 if let Some(indexed_extent) = indexed_extent { - let parent_id = cx.tcx.hir().get_parent(expr.id); - let parent_def_id = cx.tcx.hir().local_def_id(parent_id); + let parent_id = cx.tcx.hir().get_parent_item(expr.hir_id); + let parent_def_id = cx.tcx.hir().local_def_id_from_hir_id(parent_id); let region_scope_tree = cx.tcx.region_scope_tree(parent_def_id); let pat_extent = region_scope_tree.var_scope(pat.hir_id.local_id); if region_scope_tree.is_subscope_of(indexed_extent, pat_extent) { @@ -1469,8 +1470,9 @@ fn check_for_loop_explicit_counter<'a, 'tcx>( // For each candidate, check the parent block to see if // it's initialized to zero at the start of the loop. let map = &cx.tcx.hir(); + let expr_node_id = map.hir_to_node_id(expr.hir_id); let parent_scope = map - .get_enclosing_scope(expr.id) + .get_enclosing_scope(expr_node_id) .and_then(|id| map.get_enclosing_scope(id)); if let Some(parent_id) = parent_scope { if let Node::Block(block) = map.get(parent_id) { @@ -1567,13 +1569,13 @@ struct MutatePairDelegate { } impl<'tcx> Delegate<'tcx> for MutatePairDelegate { - fn consume(&mut self, _: NodeId, _: Span, _: &cmt_<'tcx>, _: ConsumeMode) {} + fn consume(&mut self, _: HirId, _: Span, _: &cmt_<'tcx>, _: ConsumeMode) {} fn matched_pat(&mut self, _: &Pat, _: &cmt_<'tcx>, _: MatchMode) {} fn consume_pat(&mut self, _: &Pat, _: &cmt_<'tcx>, _: ConsumeMode) {} - fn borrow(&mut self, _: NodeId, sp: Span, cmt: &cmt_<'tcx>, _: ty::Region<'_>, bk: ty::BorrowKind, _: LoanCause) { + fn borrow(&mut self, _: HirId, sp: Span, cmt: &cmt_<'tcx>, _: ty::Region<'_>, bk: ty::BorrowKind, _: LoanCause) { if let ty::BorrowKind::MutBorrow = bk { if let Categorization::Local(id) = cmt.cat { if Some(id) == self.node_id_low { @@ -1586,7 +1588,7 @@ impl<'tcx> Delegate<'tcx> for MutatePairDelegate { } } - fn mutate(&mut self, _: NodeId, sp: Span, cmt: &cmt_<'tcx>, _: MutateMode) { + fn mutate(&mut self, _: HirId, sp: Span, cmt: &cmt_<'tcx>, _: MutateMode) { if let Categorization::Local(id) = cmt.cat { if Some(id) == self.node_id_low { self.span_low = Some(sp) @@ -1778,8 +1780,8 @@ impl<'a, 'tcx> VarVisitor<'a, 'tcx> { Def::Local(node_id) | Def::Upvar(node_id, ..) => { let hir_id = self.cx.tcx.hir().node_to_hir_id(node_id); - let parent_id = self.cx.tcx.hir().get_parent(expr.id); - let parent_def_id = self.cx.tcx.hir().local_def_id(parent_id); + let parent_id = self.cx.tcx.hir().get_parent_item(expr.hir_id); + let parent_def_id = self.cx.tcx.hir().local_def_id_from_hir_id(parent_id); let extent = self.cx.tcx.region_scope_tree(parent_def_id).var_scope(hir_id.local_id); if indexed_indirectly { self.indexed_indirectly.insert(seqvar.segments[0].ident.name, Some(extent)); @@ -1932,11 +1934,12 @@ fn is_iterator_used_after_while_let<'a, 'tcx: 'a>(cx: &LateContext<'a, 'tcx>, it let mut visitor = VarUsedAfterLoopVisitor { cx, def_id, - iter_expr_id: iter_expr.id, + iter_expr_id: iter_expr.hir_id, past_while_let: false, var_used_after_while_let: false, }; - if let Some(enclosing_block) = get_enclosing_block(cx, def_id) { + let def_hir_id = cx.tcx.hir().node_to_hir_id(def_id); + if let Some(enclosing_block) = get_enclosing_block(cx, def_hir_id) { walk_block(&mut visitor, enclosing_block); } visitor.var_used_after_while_let @@ -1945,7 +1948,7 @@ fn is_iterator_used_after_while_let<'a, 'tcx: 'a>(cx: &LateContext<'a, 'tcx>, it struct VarUsedAfterLoopVisitor<'a, 'tcx: 'a> { cx: &'a LateContext<'a, 'tcx>, def_id: NodeId, - iter_expr_id: NodeId, + iter_expr_id: HirId, past_while_let: bool, var_used_after_while_let: bool, } @@ -1956,7 +1959,7 @@ impl<'a, 'tcx> Visitor<'tcx> for VarUsedAfterLoopVisitor<'a, 'tcx> { if Some(self.def_id) == var_def_id(self.cx, expr) { self.var_used_after_while_let = true; } - } else if self.iter_expr_id == expr.id { + } else if self.iter_expr_id == expr.hir_id { self.past_while_let = true; } walk_expr(self, expr); @@ -2068,7 +2071,7 @@ impl<'a, 'tcx> Visitor<'tcx> for IncrementVisitor<'a, 'tcx> { match parent.node { ExprKind::AssignOp(op, ref lhs, ref rhs) => { - if lhs.id == expr.id { + if lhs.hir_id == expr.hir_id { if op.node == BinOpKind::Add && is_integer_literal(rhs, 1) { *state = match *state { VarState::Initial if self.depth == 0 => VarState::IncrOnce, @@ -2080,7 +2083,7 @@ impl<'a, 'tcx> Visitor<'tcx> for IncrementVisitor<'a, 'tcx> { } } }, - ExprKind::Assign(ref lhs, _) if lhs.id == expr.id => *state = VarState::DontWarn, + ExprKind::Assign(ref lhs, _) if lhs.hir_id == expr.hir_id => *state = VarState::DontWarn, ExprKind::AddrOf(mutability, _) if mutability == MutMutable => *state = VarState::DontWarn, _ => (), } @@ -2153,10 +2156,10 @@ impl<'a, 'tcx> Visitor<'tcx> for InitializeVisitor<'a, 'tcx> { if var_def_id(self.cx, expr) == Some(self.var_id) { if let Some(parent) = get_parent_expr(self.cx, expr) { match parent.node { - ExprKind::AssignOp(_, ref lhs, _) if lhs.id == expr.id => { + ExprKind::AssignOp(_, ref lhs, _) if lhs.hir_id == expr.hir_id => { self.state = VarState::DontWarn; }, - ExprKind::Assign(ref lhs, ref rhs) if lhs.id == expr.id => { + ExprKind::Assign(ref lhs, ref rhs) if lhs.hir_id == expr.hir_id => { self.state = if is_integer_literal(rhs, 0) && self.depth == 0 { VarState::Warn } else { @@ -2214,8 +2217,8 @@ fn is_conditional(expr: &Expr) -> bool { fn is_nested(cx: &LateContext<'_, '_>, match_expr: &Expr, iter_expr: &Expr) -> bool { if_chain! { - if let Some(loop_block) = get_enclosing_block(cx, match_expr.id); - if let Some(Node::Expr(loop_expr)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_node(loop_block.id)); + if let Some(loop_block) = get_enclosing_block(cx, match_expr.hir_id); + if let Some(Node::Expr(loop_expr)) = cx.tcx.hir().find_by_hir_id(cx.tcx.hir().get_parent_node_by_hir_id(loop_block.hir_id)); then { return is_loop_nested(cx, loop_expr, iter_expr) } @@ -2224,18 +2227,18 @@ fn is_nested(cx: &LateContext<'_, '_>, match_expr: &Expr, iter_expr: &Expr) -> b } fn is_loop_nested(cx: &LateContext<'_, '_>, loop_expr: &Expr, iter_expr: &Expr) -> bool { - let mut id = loop_expr.id; + let mut id = loop_expr.hir_id; let iter_name = if let Some(name) = path_name(iter_expr) { name } else { return true; }; loop { - let parent = cx.tcx.hir().get_parent_node(id); + let parent = cx.tcx.hir().get_parent_node_by_hir_id(id); if parent == id { return false; } - match cx.tcx.hir().find(parent) { + match cx.tcx.hir().find_by_hir_id(parent) { Some(Node::Expr(expr)) => match expr.node { ExprKind::Loop(..) | ExprKind::While(..) => { return true; @@ -2244,7 +2247,7 @@ fn is_loop_nested(cx: &LateContext<'_, '_>, loop_expr: &Expr, iter_expr: &Expr) }, Some(Node::Block(block)) => { let mut block_visitor = LoopNestVisitor { - id, + hir_id: id, iterator: iter_name, nesting: Unknown, }; @@ -2272,14 +2275,14 @@ enum Nesting { use self::Nesting::{LookFurther, RuledOut, Unknown}; struct LoopNestVisitor { - id: NodeId, + hir_id: HirId, iterator: Name, nesting: Nesting, } impl<'tcx> Visitor<'tcx> for LoopNestVisitor { fn visit_stmt(&mut self, stmt: &'tcx Stmt) { - if stmt.id == self.id { + if stmt.hir_id == self.hir_id { self.nesting = LookFurther; } else if self.nesting == Unknown { walk_stmt(self, stmt); @@ -2290,7 +2293,7 @@ impl<'tcx> Visitor<'tcx> for LoopNestVisitor { if self.nesting != Unknown { return; } - if expr.id == self.id { + if expr.hir_id == self.hir_id { self.nesting = LookFurther; return; } diff --git a/clippy_lints/src/matches.rs b/clippy_lints/src/matches.rs index fcff1e16f383..5594b0eb7f56 100644 --- a/clippy_lints/src/matches.rs +++ b/clippy_lints/src/matches.rs @@ -266,7 +266,7 @@ fn check_single_match(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm], expr: & return; }; let ty = cx.tables.expr_ty(ex); - if ty.sty != ty::Bool || is_allowed(cx, MATCH_BOOL, ex.id) { + if ty.sty != ty::Bool || is_allowed(cx, MATCH_BOOL, ex.hir_id) { check_single_match_single_pattern(cx, ex, arms, expr, els); check_single_match_opt_like(cx, ex, arms, expr, ty, els); } diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 4b147cf82a68..dd75c8aff37b 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -1082,7 +1082,7 @@ fn lint_or_fun_call(cx: &LateContext<'_, '_>, expr: &hir::Expr, method_span: Spa } // don't lint for constant values - let owner_def = cx.tcx.hir().get_parent_did(arg.id); + let owner_def = cx.tcx.hir().get_parent_did_by_hir_id(arg.hir_id); let promotable = cx.tcx.rvalue_promotable_map(owner_def).contains(&arg.hir_id.local_id); if promotable { return; @@ -1341,8 +1341,8 @@ fn lint_clone_on_copy(cx: &LateContext<'_, '_>, expr: &hir::Expr, arg: &hir::Exp if cx.tables.expr_ty(arg) == ty { snip = Some(("try removing the `clone` call", format!("{}", snippet))); } else { - let parent = cx.tcx.hir().get_parent_node(expr.id); - match cx.tcx.hir().get(parent) { + let parent = cx.tcx.hir().get_parent_node_by_hir_id(expr.hir_id); + match cx.tcx.hir().get_by_hir_id(parent) { hir::Node::Expr(parent) => match parent.node { // &*x is a nop, &x.clone() is not hir::ExprKind::AddrOf(..) | diff --git a/clippy_lints/src/misc.rs b/clippy_lints/src/misc.rs index d045eaefbdb9..7125c77ec8e0 100644 --- a/clippy_lints/src/misc.rs +++ b/clippy_lints/src/misc.rs @@ -460,7 +460,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass { } fn check_nan(cx: &LateContext<'_, '_>, path: &Path, expr: &Expr) { - if !in_constant(cx, expr.id) { + if !in_constant(cx, expr.hir_id) { if let Some(seg) = path.segments.last() { if seg.ident.name == "NAN" { span_lint( @@ -615,7 +615,7 @@ fn check_cast(cx: &LateContext<'_, '_>, span: Span, e: &Expr, ty: &Ty) { if let ExprKind::Lit(ref lit) = e.node; if let LitKind::Int(value, ..) = lit.node; if value == 0; - if !in_constant(cx, e.id); + if !in_constant(cx, e.hir_id); then { let msg = match mutbl { Mutability::MutMutable => "`0 as *mut _` detected. Consider using `ptr::null_mut()`", diff --git a/clippy_lints/src/needless_bool.rs b/clippy_lints/src/needless_bool.rs index 49f607f525dc..164fb82f00f6 100644 --- a/clippy_lints/src/needless_bool.rs +++ b/clippy_lints/src/needless_bool.rs @@ -126,8 +126,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBool { } fn parent_node_is_if_expr<'a, 'b>(expr: &Expr, cx: &LateContext<'a, 'b>) -> bool { - let parent_id = cx.tcx.hir().get_parent_node(expr.id); - let parent_node = cx.tcx.hir().get(parent_id); + let parent_id = cx.tcx.hir().get_parent_node_by_hir_id(expr.hir_id); + let parent_node = cx.tcx.hir().get_by_hir_id(parent_id); if let rustc::hir::Node::Expr(e) = parent_node { if let ExprKind::If(_, _, _) = e.node { diff --git a/clippy_lints/src/needless_pass_by_value.rs b/clippy_lints/src/needless_pass_by_value.rs index fb78dd01f5e5..69d04ff7495e 100644 --- a/clippy_lints/src/needless_pass_by_value.rs +++ b/clippy_lints/src/needless_pass_by_value.rs @@ -341,7 +341,7 @@ impl<'a, 'tcx> MovedVariablesCtxt<'a, 'tcx> { } } - fn move_common(&mut self, _consume_id: NodeId, _span: Span, cmt: &mc::cmt_<'tcx>) { + fn move_common(&mut self, _consume_id: HirId, _span: Span, cmt: &mc::cmt_<'tcx>) { let cmt = unwrap_downcast_or_interior(cmt); if let mc::Categorization::Local(vid) = cmt.cat { @@ -399,7 +399,7 @@ impl<'a, 'tcx> MovedVariablesCtxt<'a, 'tcx> { } impl<'a, 'tcx> euv::Delegate<'tcx> for MovedVariablesCtxt<'a, 'tcx> { - fn consume(&mut self, consume_id: NodeId, consume_span: Span, cmt: &mc::cmt_<'tcx>, mode: euv::ConsumeMode) { + fn consume(&mut self, consume_id: HirId, consume_span: Span, cmt: &mc::cmt_<'tcx>, mode: euv::ConsumeMode) { if let euv::ConsumeMode::Move(_) = mode { self.move_common(consume_id, consume_span, cmt); } @@ -407,7 +407,7 @@ impl<'a, 'tcx> euv::Delegate<'tcx> for MovedVariablesCtxt<'a, 'tcx> { fn matched_pat(&mut self, matched_pat: &Pat, cmt: &mc::cmt_<'tcx>, mode: euv::MatchMode) { if let euv::MatchMode::MovingMatch = mode { - self.move_common(matched_pat.id, matched_pat.span, cmt); + self.move_common(matched_pat.hir_id, matched_pat.span, cmt); } else { self.non_moving_pat(matched_pat, cmt); } @@ -415,13 +415,13 @@ impl<'a, 'tcx> euv::Delegate<'tcx> for MovedVariablesCtxt<'a, 'tcx> { fn consume_pat(&mut self, consume_pat: &Pat, cmt: &mc::cmt_<'tcx>, mode: euv::ConsumeMode) { if let euv::ConsumeMode::Move(_) = mode { - self.move_common(consume_pat.id, consume_pat.span, cmt); + self.move_common(consume_pat.hir_id, consume_pat.span, cmt); } } fn borrow( &mut self, - _: NodeId, + _: HirId, _: Span, _: &mc::cmt_<'tcx>, _: ty::Region<'_>, @@ -430,7 +430,7 @@ impl<'a, 'tcx> euv::Delegate<'tcx> for MovedVariablesCtxt<'a, 'tcx> { ) { } - fn mutate(&mut self, _: NodeId, _: Span, _: &mc::cmt_<'tcx>, _: euv::MutateMode) {} + fn mutate(&mut self, _: HirId, _: Span, _: &mc::cmt_<'tcx>, _: euv::MutateMode) {} fn decl_without_init(&mut self, _: NodeId, _: Span) {} } diff --git a/clippy_lints/src/new_without_default.rs b/clippy_lints/src/new_without_default.rs index f851ae4638e6..ce8b1db505ee 100644 --- a/clippy_lints/src/new_without_default.rs +++ b/clippy_lints/src/new_without_default.rs @@ -110,7 +110,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NewWithoutDefault { } if let hir::ImplItemKind::Method(ref sig, _) = impl_item.node { let name = impl_item.ident.name; - let id = impl_item.id; + let id = impl_item.hir_id; + let node_id = cx.tcx.hir().hir_to_node_id(id); if sig.header.constness == hir::Constness::Const { // can't be implemented by default return; @@ -128,11 +129,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NewWithoutDefault { // impl of `Default` return; } - if sig.decl.inputs.is_empty() && name == "new" && cx.access_levels.is_reachable(id) { - let self_did = cx.tcx.hir().local_def_id(cx.tcx.hir().get_parent(id)); + if sig.decl.inputs.is_empty() && name == "new" && cx.access_levels.is_reachable(node_id) { + let self_did = cx.tcx.hir().local_def_id_from_hir_id(cx.tcx.hir().get_parent_item(id)); let self_ty = cx.tcx.type_of(self_did); if_chain! { - if same_tys(cx, self_ty, return_ty(cx, id)); + if same_tys(cx, self_ty, return_ty(cx, node_id)); if let Some(default_trait_id) = get_trait_def_id(cx, &paths::DEFAULT_TRAIT); then { if self.impling_types.is_none() { diff --git a/clippy_lints/src/non_copy_const.rs b/clippy_lints/src/non_copy_const.rs index 50d8b69ffae8..3bc46f53308f 100644 --- a/clippy_lints/src/non_copy_const.rs +++ b/clippy_lints/src/non_copy_const.rs @@ -197,7 +197,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonCopyConst { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { if let ExprKind::Path(qpath) = &expr.node { // Only lint if we use the const item inside a function. - if in_constant(cx, expr.id) { + if in_constant(cx, expr.hir_id) { return; } @@ -212,11 +212,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonCopyConst { let mut dereferenced_expr = expr; let mut needs_check_adjustment = true; loop { - let parent_id = cx.tcx.hir().get_parent_node(cur_expr.id); - if parent_id == cur_expr.id { + let parent_id = cx.tcx.hir().get_parent_node_by_hir_id(cur_expr.hir_id); + if parent_id == cur_expr.hir_id { break; } - if let Some(Node::Expr(parent_expr)) = cx.tcx.hir().find(parent_id) { + if let Some(Node::Expr(parent_expr)) = cx.tcx.hir().find_by_hir_id(parent_id) { match &parent_expr.node { ExprKind::AddrOf(..) => { // `&e` => `e` must be referenced diff --git a/clippy_lints/src/partialeq_ne_impl.rs b/clippy_lints/src/partialeq_ne_impl.rs index b9d5102ccd6a..f57fd22c0707 100644 --- a/clippy_lints/src/partialeq_ne_impl.rs +++ b/clippy_lints/src/partialeq_ne_impl.rs @@ -51,10 +51,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass { then { for impl_item in impl_items { if impl_item.ident.name == "ne" { + let hir_id = cx.tcx.hir().node_to_hir_id(impl_item.id.node_id); span_lint_node( cx, PARTIALEQ_NE_IMPL, - impl_item.id.node_id, + hir_id, impl_item.span, "re-implementing `PartialEq::ne` is unnecessary", ); diff --git a/clippy_lints/src/regex.rs b/clippy_lints/src/regex.rs index 0171c2a52831..eaaa6b93b3b9 100644 --- a/clippy_lints/src/regex.rs +++ b/clippy_lints/src/regex.rs @@ -7,7 +7,7 @@ use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use rustc::{declare_tool_lint, lint_array}; use rustc_data_structures::fx::FxHashSet; use std::convert::TryFrom; -use syntax::ast::{LitKind, NodeId, StrStyle}; +use syntax::ast::{LitKind, StrStyle}; use syntax::source_map::{BytePos, Span}; /// **What it does:** Checks [regex](https://crates.io/crates/regex) creation @@ -69,7 +69,7 @@ declare_clippy_lint! { #[derive(Clone, Default)] pub struct Pass { spans: FxHashSet, - last: Option, + last: Option, } impl LintPass for Pass { @@ -102,13 +102,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass { Please use `Regex::new(_)`, which is faster for now."); self.spans.insert(span); } - self.last = Some(block.id); + self.last = Some(block.hir_id); } } } fn check_block_post(&mut self, _: &LateContext<'a, 'tcx>, block: &'tcx Block) { - if self.last.map_or(false, |id| block.id == id) { + if self.last.map_or(false, |id| block.hir_id == id) { self.last = None; } } diff --git a/clippy_lints/src/slow_vector_initialization.rs b/clippy_lints/src/slow_vector_initialization.rs index b8ab32491a3b..e8f480a22c91 100644 --- a/clippy_lints/src/slow_vector_initialization.rs +++ b/clippy_lints/src/slow_vector_initialization.rs @@ -6,7 +6,7 @@ use rustc::hir::*; use rustc::lint::{LateContext, LateLintPass, Lint, LintArray, LintPass}; use rustc::{declare_tool_lint, lint_array}; use rustc_errors::Applicability; -use syntax::ast::{LitKind, NodeId}; +use syntax::ast::LitKind; use syntax_pos::symbol::Symbol; /// **What it does:** Checks slow zero-filled vector initialization @@ -87,7 +87,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass { len_expr: len_arg, }; - Self::search_initialization(cx, vi, expr.id); + Self::search_initialization(cx, vi, expr.hir_id); } } } @@ -107,7 +107,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass { len_expr: len_arg, }; - Self::search_initialization(cx, vi, stmt.id); + Self::search_initialization(cx, vi, stmt.hir_id); } } } @@ -132,7 +132,7 @@ impl Pass { } /// Search initialization for the given vector - fn search_initialization<'tcx>(cx: &LateContext<'_, 'tcx>, vec_alloc: VecAllocation<'tcx>, parent_node: NodeId) { + fn search_initialization<'tcx>(cx: &LateContext<'_, 'tcx>, vec_alloc: VecAllocation<'tcx>, parent_node: HirId) { let enclosing_body = get_enclosing_block(cx, parent_node); if enclosing_body.is_none() { @@ -317,7 +317,7 @@ impl<'a, 'tcx> Visitor<'tcx> for VectorInitializationVisitor<'a, 'tcx> { fn visit_expr(&mut self, expr: &'tcx Expr) { // Skip all the expressions previous to the vector initialization - if self.vec_alloc.allocation_expr.id == expr.id { + if self.vec_alloc.allocation_expr.hir_id == expr.hir_id { self.initialization_found = true; } diff --git a/clippy_lints/src/strings.rs b/clippy_lints/src/strings.rs index 71784397463b..ba085396e7ae 100644 --- a/clippy_lints/src/strings.rs +++ b/clippy_lints/src/strings.rs @@ -96,7 +96,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringAdd { ) = e.node { if is_string(cx, left) { - if !is_allowed(cx, STRING_ADD_ASSIGN, e.id) { + if !is_allowed(cx, STRING_ADD_ASSIGN, e.hir_id) { let parent = get_parent_expr(cx, e); if let Some(p) = parent { if let ExprKind::Assign(ref target, _) = p.node { diff --git a/clippy_lints/src/suspicious_trait_impl.rs b/clippy_lints/src/suspicious_trait_impl.rs index 5f0e49dd7e6d..135ffd772db3 100644 --- a/clippy_lints/src/suspicious_trait_impl.rs +++ b/clippy_lints/src/suspicious_trait_impl.rs @@ -4,7 +4,6 @@ use rustc::hir; use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor}; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use rustc::{declare_tool_lint, lint_array}; -use syntax::ast; /// **What it does:** Lints for suspicious operations in impls of arithmetic operators, e.g. /// subtracting elements in an Add impl. @@ -77,9 +76,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for SuspiciousImpl { } // Check if the binary expression is part of another bi/unary expression // as a child node - let mut parent_expr = cx.tcx.hir().get_parent_node(expr.id); - while parent_expr != ast::CRATE_NODE_ID { - if let hir::Node::Expr(e) = cx.tcx.hir().get(parent_expr) { + let mut parent_expr = cx.tcx.hir().get_parent_node_by_hir_id(expr.hir_id); + while parent_expr != hir::CRATE_HIR_ID { + if let hir::Node::Expr(e) = cx.tcx.hir().get_by_hir_id(parent_expr) { match e.node { hir::ExprKind::Binary(..) | hir::ExprKind::Unary(hir::UnOp::UnNot, _) @@ -87,7 +86,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for SuspiciousImpl { _ => {}, } } - parent_expr = cx.tcx.hir().get_parent_node(parent_expr); + parent_expr = cx.tcx.hir().get_parent_node_by_hir_id(parent_expr); } // as a parent node let mut visitor = BinaryExprVisitor { in_binary_expr: false }; @@ -177,12 +176,12 @@ fn check_binop<'a>( } // Get the actually implemented trait - let parent_fn = cx.tcx.hir().get_parent(expr.id); - let parent_impl = cx.tcx.hir().get_parent(parent_fn); + let parent_fn = cx.tcx.hir().get_parent_item(expr.hir_id); + let parent_impl = cx.tcx.hir().get_parent_item(parent_fn); if_chain! { - if parent_impl != ast::CRATE_NODE_ID; - if let hir::Node::Item(item) = cx.tcx.hir().get(parent_impl); + if parent_impl != hir::CRATE_HIR_ID; + if let hir::Node::Item(item) = cx.tcx.hir().get_by_hir_id(parent_impl); if let hir::ItemKind::Impl(_, _, _, _, Some(ref trait_ref), _, _) = item.node; if let Some(idx) = trait_ids.iter().position(|&tid| tid == trait_ref.path.def.def_id()); if binop != expected_ops[idx]; diff --git a/clippy_lints/src/types.rs b/clippy_lints/src/types.rs index dc1119595022..7356f957d311 100644 --- a/clippy_lints/src/types.rs +++ b/clippy_lints/src/types.rs @@ -225,7 +225,7 @@ fn match_type_parameter(cx: &LateContext<'_, '_>, qpath: &QPath, path: &[&str]) _ => None, }); if let TyKind::Path(ref qpath) = ty.node; - if let Some(did) = opt_def_id(cx.tables.qpath_def(qpath, cx.tcx.hir().node_to_hir_id(ty.id))); + if let Some(did) = opt_def_id(cx.tables.qpath_def(qpath, ty.hir_id)); if match_def_path(cx.tcx, did, path); then { return true; @@ -246,7 +246,7 @@ fn check_ty(cx: &LateContext<'_, '_>, hir_ty: &hir::Ty, is_local: bool) { } match hir_ty.node { TyKind::Path(ref qpath) if !is_local => { - let hir_id = cx.tcx.hir().node_to_hir_id(hir_ty.id); + let hir_id = hir_ty.hir_id; let def = cx.tables.qpath_def(qpath, hir_id); if let Some(def_id) = opt_def_id(def) { if Some(def_id) == cx.tcx.lang_items().owned_box() { @@ -375,7 +375,7 @@ fn check_ty(cx: &LateContext<'_, '_>, hir_ty: &hir::Ty, is_local: bool) { fn check_ty_rptr(cx: &LateContext<'_, '_>, hir_ty: &hir::Ty, is_local: bool, lt: &Lifetime, mut_ty: &MutTy) { match mut_ty.ty.node { TyKind::Path(ref qpath) => { - let hir_id = cx.tcx.hir().node_to_hir_id(mut_ty.ty.id); + let hir_id = mut_ty.ty.hir_id; let def = cx.tables.qpath_def(qpath, hir_id); if_chain! { if let Some(def_id) = opt_def_id(def); @@ -617,7 +617,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnitArg { } if_chain! { let map = &cx.tcx.hir(); - let opt_parent_node = map.find(map.get_parent_node(expr.id)); + let opt_parent_node = map.find_by_hir_id(map.get_parent_node_by_hir_id(expr.hir_id)); if let Some(hir::Node::Expr(parent_expr)) = opt_parent_node; if is_questionmark_desugar_marked_call(parent_expr); then { @@ -961,7 +961,7 @@ fn should_strip_parens(op: &Expr, snip: &str) -> bool { fn span_lossless_lint(cx: &LateContext<'_, '_>, expr: &Expr, op: &Expr, cast_from: Ty<'_>, cast_to: Ty<'_>) { // Do not suggest using From in consts/statics until it is valid to do so (see #2267). - if in_constant(cx, expr.id) { + if in_constant(cx, expr.hir_id) { return; } // The suggestion is to use a function call, so if the original expression diff --git a/clippy_lints/src/unicode.rs b/clippy_lints/src/unicode.rs index d1f39a5e2c23..c31a8aebc351 100644 --- a/clippy_lints/src/unicode.rs +++ b/clippy_lints/src/unicode.rs @@ -2,7 +2,7 @@ use crate::utils::{is_allowed, snippet, span_help_and_lint}; use rustc::hir::*; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use rustc::{declare_tool_lint, lint_array}; -use syntax::ast::{LitKind, NodeId}; +use syntax::ast::LitKind; use syntax::source_map::Span; use unicode_normalization::UnicodeNormalization; @@ -75,7 +75,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Unicode { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { if let ExprKind::Lit(ref lit) = expr.node { if let LitKind::Str(_, _) = lit.node { - check_str(cx, lit.span, expr.id) + check_str(cx, lit.span, expr.hir_id) } } } @@ -95,7 +95,7 @@ fn escape>(s: T) -> String { result } -fn check_str(cx: &LateContext<'_, '_>, span: Span, id: NodeId) { +fn check_str(cx: &LateContext<'_, '_>, span: Span, id: HirId) { let string = snippet(cx, span, ""); if string.contains('\u{200B}') { span_help_and_lint( diff --git a/clippy_lints/src/utils/diagnostics.rs b/clippy_lints/src/utils/diagnostics.rs index e676a28fe656..e2234b67f9b4 100644 --- a/clippy_lints/src/utils/diagnostics.rs +++ b/clippy_lints/src/utils/diagnostics.rs @@ -1,6 +1,6 @@ //! Clippy wrappers around rustc's diagnostic functions. -use crate::reexport::*; +use rustc::hir::HirId; use rustc::lint::{LateContext, Lint, LintContext}; use rustc_errors::{Applicability, CodeSuggestion, Substitution, SubstitutionPart, SuggestionStyle}; use std::env; @@ -134,19 +134,21 @@ pub fn span_lint_and_then<'a, 'tcx: 'a, T: LintContext<'tcx>, F>( db.docs_link(lint); } -pub fn span_lint_node(cx: &LateContext<'_, '_>, lint: &'static Lint, node: NodeId, sp: Span, msg: &str) { - DiagnosticWrapper(cx.tcx.struct_span_lint_node(lint, node, sp, msg)).docs_link(lint); +pub fn span_lint_node(cx: &LateContext<'_, '_>, lint: &'static Lint, node: HirId, sp: Span, msg: &str) { + let node_id = cx.tcx.hir().hir_to_node_id(node); + DiagnosticWrapper(cx.tcx.struct_span_lint_node(lint, node_id, sp, msg)).docs_link(lint); } pub fn span_lint_node_and_then( cx: &LateContext<'_, '_>, lint: &'static Lint, - node: NodeId, + node: HirId, sp: Span, msg: &str, f: impl FnOnce(&mut DiagnosticBuilder<'_>), ) { - let mut db = DiagnosticWrapper(cx.tcx.struct_span_lint_node(lint, node, sp, msg)); + let node_id = cx.tcx.hir().hir_to_node_id(node); + let mut db = DiagnosticWrapper(cx.tcx.struct_span_lint_node(lint, node_id, sp, msg)); f(&mut db.0); db.docs_link(lint); } diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index c5af70cae968..88636c6e4754 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -62,9 +62,9 @@ pub fn differing_macro_contexts(lhs: Span, rhs: Span) -> bool { /// // Do something /// } /// ``` -pub fn in_constant(cx: &LateContext<'_, '_>, id: NodeId) -> bool { - let parent_id = cx.tcx.hir().get_parent(id); - match cx.tcx.hir().get(parent_id) { +pub fn in_constant(cx: &LateContext<'_, '_>, id: HirId) -> bool { + let parent_id = cx.tcx.hir().get_parent_item(id); + match cx.tcx.hir().get_by_hir_id(parent_id) { Node::Item(&Item { node: ItemKind::Const(..), .. @@ -378,8 +378,8 @@ pub fn is_entrypoint_fn(cx: &LateContext<'_, '_>, def_id: DefId) -> bool { /// Get the name of the item the expression is in, if available. pub fn get_item_name(cx: &LateContext<'_, '_>, expr: &Expr) -> Option { - let parent_id = cx.tcx.hir().get_parent(expr.id); - match cx.tcx.hir().find(parent_id) { + let parent_id = cx.tcx.hir().get_parent_item(expr.hir_id); + match cx.tcx.hir().find_by_hir_id(parent_id) { Some(Node::Item(&Item { ref ident, .. })) => Some(ident.name), Some(Node::TraitItem(&TraitItem { ident, .. })) | Some(Node::ImplItem(&ImplItem { ident, .. })) => { Some(ident.name) @@ -571,12 +571,12 @@ fn trim_multiline_inner(s: Cow<'_, str>, ignore_first: bool, ch: char) -> Cow<'_ /// Get a parent expressions if any – this is useful to constrain a lint. pub fn get_parent_expr<'c>(cx: &'c LateContext<'_, '_>, e: &Expr) -> Option<&'c Expr> { let map = &cx.tcx.hir(); - let node_id: NodeId = e.id; - let parent_id: NodeId = map.get_parent_node(node_id); - if node_id == parent_id { + let hir_id = e.hir_id; + let parent_id = map.get_parent_node_by_hir_id(hir_id); + if hir_id == parent_id { return None; } - map.find(parent_id).and_then(|node| { + map.find_by_hir_id(hir_id).and_then(|node| { if let Node::Expr(parent) = node { Some(parent) } else { @@ -585,10 +585,11 @@ pub fn get_parent_expr<'c>(cx: &'c LateContext<'_, '_>, e: &Expr) -> Option<&'c }) } -pub fn get_enclosing_block<'a, 'tcx: 'a>(cx: &LateContext<'a, 'tcx>, node: NodeId) -> Option<&'tcx Block> { +pub fn get_enclosing_block<'a, 'tcx: 'a>(cx: &LateContext<'a, 'tcx>, node: HirId) -> Option<&'tcx Block> { let map = &cx.tcx.hir(); + let node_id = map.hir_to_node_id(node); let enclosing_node = map - .get_enclosing_scope(node) + .get_enclosing_scope(node_id) .and_then(|enclosing_id| map.find(enclosing_id)); if let Some(node) = enclosing_node { match node { @@ -927,8 +928,9 @@ pub fn is_try(expr: &Expr) -> Option<&Expr> { /// Returns true if the lint is allowed in the current context /// /// Useful for skipping long running code when it's unnecessary -pub fn is_allowed(cx: &LateContext<'_, '_>, lint: &'static Lint, id: NodeId) -> bool { - cx.tcx.lint_level_at_node(lint, id).0 == Level::Allow +pub fn is_allowed(cx: &LateContext<'_, '_>, lint: &'static Lint, id: HirId) -> bool { + let node_id = cx.tcx.hir().hir_to_node_id(id); + cx.tcx.lint_level_at_node(lint, node_id).0 == Level::Allow } pub fn get_arg_name(pat: &Pat) -> Option { @@ -1001,16 +1003,16 @@ pub fn without_block_comments(lines: Vec<&str>) -> Vec<&str> { without } -pub fn any_parent_is_automatically_derived(tcx: TyCtxt<'_, '_, '_>, node: NodeId) -> bool { +pub fn any_parent_is_automatically_derived(tcx: TyCtxt<'_, '_, '_>, node: HirId) -> bool { let map = &tcx.hir(); let mut prev_enclosing_node = None; let mut enclosing_node = node; while Some(enclosing_node) != prev_enclosing_node { - if is_automatically_derived(map.attrs(enclosing_node)) { + if is_automatically_derived(map.attrs_by_hir_id(enclosing_node)) { return true; } prev_enclosing_node = Some(enclosing_node); - enclosing_node = map.get_parent(enclosing_node); + enclosing_node = map.get_parent_item(enclosing_node); } false } diff --git a/clippy_lints/src/utils/usage.rs b/clippy_lints/src/utils/usage.rs index a3d3518ef98d..1f649829f810 100644 --- a/clippy_lints/src/utils/usage.rs +++ b/clippy_lints/src/utils/usage.rs @@ -63,19 +63,19 @@ impl<'tcx> MutVarsDelegate { } impl<'tcx> Delegate<'tcx> for MutVarsDelegate { - fn consume(&mut self, _: NodeId, _: Span, _: &cmt_<'tcx>, _: ConsumeMode) {} + fn consume(&mut self, _: HirId, _: Span, _: &cmt_<'tcx>, _: ConsumeMode) {} fn matched_pat(&mut self, _: &Pat, _: &cmt_<'tcx>, _: MatchMode) {} fn consume_pat(&mut self, _: &Pat, _: &cmt_<'tcx>, _: ConsumeMode) {} - fn borrow(&mut self, _: NodeId, _: Span, cmt: &cmt_<'tcx>, _: ty::Region<'_>, bk: ty::BorrowKind, _: LoanCause) { + fn borrow(&mut self, _: HirId, _: Span, cmt: &cmt_<'tcx>, _: ty::Region<'_>, bk: ty::BorrowKind, _: LoanCause) { if let ty::BorrowKind::MutBorrow = bk { self.update(&cmt.cat) } } - fn mutate(&mut self, _: NodeId, _: Span, cmt: &cmt_<'tcx>, _: MutateMode) { + fn mutate(&mut self, _: HirId, _: Span, cmt: &cmt_<'tcx>, _: MutateMode) { self.update(&cmt.cat) } From 7bc2e1d60d23a2f6a31d7a04d40171372d80b5b3 Mon Sep 17 00:00:00 2001 From: ljedrz Date: Mon, 25 Feb 2019 11:06:46 +0100 Subject: [PATCH 2/3] fix: replace wrong id --- clippy_lints/src/utils/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index 88636c6e4754..ee148ce35588 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -576,7 +576,7 @@ pub fn get_parent_expr<'c>(cx: &'c LateContext<'_, '_>, e: &Expr) -> Option<&'c if hir_id == parent_id { return None; } - map.find_by_hir_id(hir_id).and_then(|node| { + map.find_by_hir_id(parent_id).and_then(|node| { if let Node::Expr(parent) = node { Some(parent) } else { From c1b65ec363e552e1698949e34fc59a9196cbbcd9 Mon Sep 17 00:00:00 2001 From: ljedrz Date: Tue, 26 Feb 2019 11:21:07 +0100 Subject: [PATCH 3/3] fix line format --- clippy_lints/src/loops.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index 9439a3ff4546..73da2467f59c 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -2218,7 +2218,8 @@ fn is_conditional(expr: &Expr) -> bool { fn is_nested(cx: &LateContext<'_, '_>, match_expr: &Expr, iter_expr: &Expr) -> bool { if_chain! { if let Some(loop_block) = get_enclosing_block(cx, match_expr.hir_id); - if let Some(Node::Expr(loop_expr)) = cx.tcx.hir().find_by_hir_id(cx.tcx.hir().get_parent_node_by_hir_id(loop_block.hir_id)); + let parent_node = cx.tcx.hir().get_parent_node_by_hir_id(loop_block.hir_id); + if let Some(Node::Expr(loop_expr)) = cx.tcx.hir().find_by_hir_id(parent_node); then { return is_loop_nested(cx, loop_expr, iter_expr) }