Skip to content

Commit

Permalink
Auto merge of #3814 - ljedrz:HirIdification_lockstep_upgrade, r=phansch
Browse files Browse the repository at this point in the history
HirIdify some lints

Unblocks rust-lang/rust#58561 (a part of [rust-lang/rust#57578](rust-lang/rust#57578)). Can we branch it like with #3790? I can rebase on a different commit if need be.

Haven't had time to run tests yet, so I'd wait for Travis 🙈.
  • Loading branch information
bors committed Feb 26, 2019
2 parents d0717d1 + c1b65ec commit 412d41a
Show file tree
Hide file tree
Showing 27 changed files with 131 additions and 126 deletions.
9 changes: 4 additions & 5 deletions clippy_lints/src/assign_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions clippy_lints/src/booleans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/copies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
3 changes: 1 addition & 2 deletions clippy_lints/src/cyclomatic_complexity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/default_trait_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<'_>,
Expand Down Expand Up @@ -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> {
Expand Down
12 changes: 6 additions & 6 deletions clippy_lints/src/eval_order_dependence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions clippy_lints/src/identity_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -27,7 +26,7 @@ declare_clippy_lint! {

#[derive(Default)]
pub struct IdentityConversion {
try_desugar_arm: Vec<NodeId>,
try_desugar_arm: Vec<HirId>,
}

impl LintPass for IdentityConversion {
Expand All @@ -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;
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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();
}
}
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
56 changes: 30 additions & 26 deletions clippy_lints/src/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
},
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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
Expand All @@ -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,
}
Expand All @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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,
_ => (),
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -2214,8 +2217,9 @@ 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);
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)
}
Expand All @@ -2224,18 +2228,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;
Expand All @@ -2244,7 +2248,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,
};
Expand Down Expand Up @@ -2272,14 +2276,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);
Expand All @@ -2290,7 +2294,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;
}
Expand Down
Loading

0 comments on commit 412d41a

Please sign in to comment.