Skip to content

Commit

Permalink
Auto merge of #3834 - ljedrz:HirIdification_fix, r=phansch
Browse files Browse the repository at this point in the history
HirIdification fixes

Supersedes #3828, enables rust-lang/rust#58836.

As usual, requesting a branch.
  • Loading branch information
bors committed Mar 3, 2019
2 parents 8dfabdf + 68096cf commit 9d1792a
Show file tree
Hide file tree
Showing 27 changed files with 114 additions and 116 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/copy_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl LintPass for CopyIterator {
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CopyIterator {
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
if let ItemKind::Impl(_, _, _, _, Some(ref trait_ref), _, _) = item.node {
let ty = cx.tcx.type_of(cx.tcx.hir().local_def_id(item.id));
let ty = cx.tcx.type_of(cx.tcx.hir().local_def_id_from_hir_id(item.hir_id));

if is_copy(cx, ty) && match_path(&trait_ref.path, &paths::ITERATOR) {
span_note_and_lint(
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl LintPass for Derive {
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Derive {
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
if let ItemKind::Impl(_, _, _, _, Some(ref trait_ref), _, _) = item.node {
let ty = cx.tcx.type_of(cx.tcx.hir().local_def_id(item.id));
let ty = cx.tcx.type_of(cx.tcx.hir().local_def_id_from_hir_id(item.hir_id));
let is_automatically_derived = is_automatically_derived(&*item.attrs);

check_hash_peq(cx, item.span, trait_ref, ty, is_automatically_derived);
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/empty_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl LintPass for EmptyEnum {

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EmptyEnum {
fn check_item(&mut self, cx: &LateContext<'_, '_>, item: &Item) {
let did = cx.tcx.hir().local_def_id(item.id);
let did = cx.tcx.hir().local_def_id_from_hir_id(item.hir_id);
if let ItemKind::Enum(..) = item.node {
let ty = cx.tcx.type_of(did);
let adt = ty.ty_adt_def().expect("already checked whether this is an enum");
Expand Down
21 changes: 10 additions & 11 deletions clippy_lints/src/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ use rustc::middle::expr_use_visitor::*;
use rustc::middle::mem_categorization::{cmt_, Categorization};
use rustc::ty::layout::LayoutOf;
use rustc::ty::{self, Ty};
use rustc::util::nodemap::NodeSet;
use rustc::util::nodemap::HirIdSet;
use rustc::{declare_tool_lint, lint_array};
use syntax::ast::NodeId;
use syntax::source_map::Span;

pub struct Pass {
Expand Down Expand Up @@ -44,7 +43,7 @@ fn is_non_trait_box(ty: Ty<'_>) -> bool {

struct EscapeDelegate<'a, 'tcx: 'a> {
cx: &'a LateContext<'a, 'tcx>,
set: NodeSet,
set: HirIdSet,
too_large_for_stack: u64,
}

Expand Down Expand Up @@ -80,7 +79,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {

let mut v = EscapeDelegate {
cx,
set: NodeSet::default(),
set: HirIdSet::default(),
too_large_for_stack: self.too_large_for_stack,
};

Expand All @@ -92,7 +91,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
span_lint(
cx,
BOXED_LOCAL,
cx.tcx.hir().span(node),
cx.tcx.hir().span_by_hir_id(node),
"local variable doesn't need to be boxed here",
);
}
Expand All @@ -111,13 +110,13 @@ impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
fn matched_pat(&mut self, _: &Pat, _: &cmt_<'tcx>, _: MatchMode) {}
fn consume_pat(&mut self, consume_pat: &Pat, cmt: &cmt_<'tcx>, _: ConsumeMode) {
let map = &self.cx.tcx.hir();
if map.is_argument(consume_pat.id) {
if map.is_argument(map.hir_to_node_id(consume_pat.hir_id)) {
// Skip closure arguments
if let Some(Node::Expr(..)) = map.find(map.get_parent_node(consume_pat.id)) {
if let Some(Node::Expr(..)) = map.find_by_hir_id(map.get_parent_node_by_hir_id(consume_pat.hir_id)) {
return;
}
if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) {
self.set.insert(consume_pat.id);
self.set.insert(consume_pat.hir_id);
}
return;
}
Expand All @@ -129,7 +128,7 @@ impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
if let ExprKind::Box(..) = ex.node {
if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) {
// let x = box (...)
self.set.insert(consume_pat.id);
self.set.insert(consume_pat.hir_id);
}
// TODO Box::new
// TODO vec![]
Expand All @@ -143,7 +142,7 @@ impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
if self.set.contains(&lid) {
// let y = x where x is known
// remove x, insert y
self.set.insert(consume_pat.id);
self.set.insert(consume_pat.hir_id);
self.set.remove(&lid);
}
}
Expand Down Expand Up @@ -177,7 +176,7 @@ impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
}
}
}
fn decl_without_init(&mut self, _: NodeId, _: Span) {}
fn decl_without_init(&mut self, _: HirId, _: Span) {}
fn mutate(&mut self, _: HirId, _: Span, _: &cmt_<'tcx>, _: MutateMode) {}
}

Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/fallible_impl_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl LintPass for FallibleImplFrom {
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FallibleImplFrom {
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::Item) {
// check for `impl From<???> for ..`
let impl_def_id = cx.tcx.hir().local_def_id(item.id);
let impl_def_id = cx.tcx.hir().local_def_id_from_hir_id(item.hir_id);
if_chain! {
if let hir::ItemKind::Impl(.., ref impl_items) = item.node;
if let Some(impl_trait_ref) = cx.tcx.impl_trait_ref(impl_def_id);
Expand Down Expand Up @@ -105,7 +105,7 @@ fn lint_impl_body<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, impl_span: Span, impl_it
then {
// check the body for `begin_panic` or `unwrap`
let body = cx.tcx.hir().body(body_id);
let impl_item_def_id = cx.tcx.hir().local_def_id(impl_item.id.node_id);
let impl_item_def_id = cx.tcx.hir().local_def_id_from_hir_id(impl_item.id.hir_id);
let mut fpu = FindPanicUnwrap {
tcx: cx.tcx,
tables: cx.tcx.typeck_tables_of(impl_item_def_id),
Expand Down
10 changes: 5 additions & 5 deletions clippy_lints/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Functions {
}
}

let nodeid = cx.tcx.hir().hir_to_node_id(hir_id);
self.check_raw_ptr(cx, unsafety, decl, body, nodeid);
self.check_raw_ptr(cx, unsafety, decl, body, hir_id);
self.check_line_number(cx, span, body);
}

Expand All @@ -164,7 +163,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Functions {

if let hir::TraitMethod::Provided(eid) = *eid {
let body = cx.tcx.hir().body(eid);
self.check_raw_ptr(cx, sig.header.unsafety, &sig.decl, body, item.id);
self.check_raw_ptr(cx, sig.header.unsafety, &sig.decl, body, item.hir_id);
}
}
}
Expand Down Expand Up @@ -255,10 +254,11 @@ impl<'a, 'tcx> Functions {
unsafety: hir::Unsafety,
decl: &'tcx hir::FnDecl,
body: &'tcx hir::Body,
nodeid: ast::NodeId,
hir_id: hir::HirId,
) {
let expr = &body.value;
if unsafety == hir::Unsafety::Normal && cx.access_levels.is_exported(nodeid) {
let node_id = cx.tcx.hir().hir_to_node_id(hir_id);
if unsafety == hir::Unsafety::Normal && cx.access_levels.is_exported(node_id) {
let raw_ptrs = iter_input_pats(decl, body)
.zip(decl.inputs.iter())
.filter_map(|(arg, ty)| raw_ptr_arg(arg, ty))
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/large_enum_variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl LintPass for LargeEnumVariant {

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LargeEnumVariant {
fn check_item(&mut self, cx: &LateContext<'_, '_>, item: &Item) {
let did = cx.tcx.hir().local_def_id(item.id);
let did = cx.tcx.hir().local_def_id_from_hir_id(item.hir_id);
if let ItemKind::Enum(ref def, _) = item.node {
let ty = cx.tcx.type_of(did);
let adt = ty.ty_adt_def().expect("already checked whether this is an enum");
Expand Down
19 changes: 12 additions & 7 deletions clippy_lints/src/len_zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ fn check_trait_items(cx: &LateContext<'_, '_>, visited_trait: &Item, trait_items
item.ident.name == name
&& if let AssociatedItemKind::Method { has_self } = item.kind {
has_self && {
let did = cx.tcx.hir().local_def_id(item.id.node_id);
let did = cx.tcx.hir().local_def_id_from_hir_id(item.id.hir_id);
cx.tcx.fn_sig(did).inputs().skip_binder().len() == 1
}
} else {
Expand All @@ -149,9 +149,11 @@ fn check_trait_items(cx: &LateContext<'_, '_>, visited_trait: &Item, trait_items
}
}

if cx.access_levels.is_exported(visited_trait.id) && trait_items.iter().any(|i| is_named_self(cx, i, "len")) {
let trait_node_id = cx.tcx.hir().hir_to_node_id(visited_trait.hir_id);

if cx.access_levels.is_exported(trait_node_id) && trait_items.iter().any(|i| is_named_self(cx, i, "len")) {
let mut current_and_super_traits = FxHashSet::default();
let visited_trait_def_id = cx.tcx.hir().local_def_id(visited_trait.id);
let visited_trait_def_id = cx.tcx.hir().local_def_id_from_hir_id(visited_trait.hir_id);
fill_trait_set(visited_trait_def_id, &mut current_and_super_traits, cx);

let is_empty_method_found = current_and_super_traits
Expand Down Expand Up @@ -183,7 +185,7 @@ fn check_impl_items(cx: &LateContext<'_, '_>, item: &Item, impl_items: &[ImplIte
item.ident.name == name
&& if let AssociatedItemKind::Method { has_self } = item.kind {
has_self && {
let did = cx.tcx.hir().local_def_id(item.id.node_id);
let did = cx.tcx.hir().local_def_id_from_hir_id(item.id.hir_id);
cx.tcx.fn_sig(did).inputs().skip_binder().len() == 1
}
} else {
Expand All @@ -192,7 +194,10 @@ fn check_impl_items(cx: &LateContext<'_, '_>, item: &Item, impl_items: &[ImplIte
}

let is_empty = if let Some(is_empty) = impl_items.iter().find(|i| is_named_self(cx, i, "is_empty")) {
if cx.access_levels.is_exported(is_empty.id.node_id) {
if cx
.access_levels
.is_exported(cx.tcx.hir().hir_to_node_id(is_empty.id.hir_id))
{
return;
} else {
"a private"
Expand All @@ -202,8 +207,8 @@ fn check_impl_items(cx: &LateContext<'_, '_>, item: &Item, impl_items: &[ImplIte
};

if let Some(i) = impl_items.iter().find(|i| is_named_self(cx, i, "len")) {
if cx.access_levels.is_exported(i.id.node_id) {
let def_id = cx.tcx.hir().local_def_id(item.id);
if cx.access_levels.is_exported(cx.tcx.hir().hir_to_node_id(i.id.hir_id)) {
let def_id = cx.tcx.hir().local_def_id_from_hir_id(item.hir_id);
let ty = cx.tcx.type_of(def_id);

span_lint(
Expand Down
47 changes: 23 additions & 24 deletions clippy_lints/src/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1562,8 +1562,8 @@ fn check_for_loop_over_map_kv<'a, 'tcx>(
}

struct MutatePairDelegate {
node_id_low: Option<NodeId>,
node_id_high: Option<NodeId>,
hir_id_low: Option<HirId>,
hir_id_high: Option<HirId>,
span_low: Option<Span>,
span_high: Option<Span>,
}
Expand All @@ -1578,10 +1578,10 @@ impl<'tcx> Delegate<'tcx> for MutatePairDelegate {
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 {
if Some(id) == self.hir_id_low {
self.span_low = Some(sp)
}
if Some(id) == self.node_id_high {
if Some(id) == self.hir_id_high {
self.span_high = Some(sp)
}
}
Expand All @@ -1590,16 +1590,16 @@ impl<'tcx> Delegate<'tcx> for MutatePairDelegate {

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 {
if Some(id) == self.hir_id_low {
self.span_low = Some(sp)
}
if Some(id) == self.node_id_high {
if Some(id) == self.hir_id_high {
self.span_high = Some(sp)
}
}
}

fn decl_without_init(&mut self, _: NodeId, _: Span) {}
fn decl_without_init(&mut self, _: HirId, _: Span) {}
}

impl<'tcx> MutatePairDelegate {
Expand Down Expand Up @@ -1635,7 +1635,7 @@ fn mut_warn_with_span(cx: &LateContext<'_, '_>, span: Option<Span>) {
}
}

fn check_for_mutability(cx: &LateContext<'_, '_>, bound: &Expr) -> Option<NodeId> {
fn check_for_mutability(cx: &LateContext<'_, '_>, bound: &Expr) -> Option<HirId> {
if_chain! {
if let ExprKind::Path(ref qpath) = bound.node;
if let QPath::Resolved(None, _) = *qpath;
Expand All @@ -1648,7 +1648,7 @@ fn check_for_mutability(cx: &LateContext<'_, '_>, bound: &Expr) -> Option<NodeId
if let PatKind::Binding(bind_ann, ..) = pat.node;
if let BindingAnnotation::Mutable = bind_ann;
then {
return Some(node_id);
return Some(cx.tcx.hir().node_to_hir_id(node_id));
}
}
}
Expand All @@ -1660,11 +1660,11 @@ fn check_for_mutability(cx: &LateContext<'_, '_>, bound: &Expr) -> Option<NodeId
fn check_for_mutation(
cx: &LateContext<'_, '_>,
body: &Expr,
bound_ids: &[Option<NodeId>],
bound_ids: &[Option<HirId>],
) -> (Option<Span>, Option<Span>) {
let mut delegate = MutatePairDelegate {
node_id_low: bound_ids[0],
node_id_high: bound_ids[1],
hir_id_low: bound_ids[0],
hir_id_high: bound_ids[1],
span_low: None,
span_high: None,
};
Expand Down Expand Up @@ -1938,16 +1938,15 @@ fn is_iterator_used_after_while_let<'a, 'tcx: 'a>(cx: &LateContext<'a, 'tcx>, it
past_while_let: false,
var_used_after_while_let: false,
};
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) {
if let Some(enclosing_block) = get_enclosing_block(cx, def_id) {
walk_block(&mut visitor, enclosing_block);
}
visitor.var_used_after_while_let
}

struct VarUsedAfterLoopVisitor<'a, 'tcx: 'a> {
cx: &'a LateContext<'a, 'tcx>,
def_id: NodeId,
def_id: HirId,
iter_expr_id: HirId,
past_while_let: bool,
var_used_after_while_let: bool,
Expand Down Expand Up @@ -2052,9 +2051,9 @@ enum VarState {

/// Scan a for loop for variables that are incremented exactly once.
struct IncrementVisitor<'a, 'tcx: 'a> {
cx: &'a LateContext<'a, 'tcx>, // context reference
states: FxHashMap<NodeId, VarState>, // incremented variables
depth: u32, // depth of conditional expressions
cx: &'a LateContext<'a, 'tcx>, // context reference
states: FxHashMap<HirId, VarState>, // incremented variables
depth: u32, // depth of conditional expressions
done: bool,
}

Expand Down Expand Up @@ -2108,7 +2107,7 @@ impl<'a, 'tcx> Visitor<'tcx> for IncrementVisitor<'a, 'tcx> {
struct InitializeVisitor<'a, 'tcx: 'a> {
cx: &'a LateContext<'a, 'tcx>, // context reference
end_expr: &'tcx Expr, // the for loop. Stop scanning here.
var_id: NodeId,
var_id: HirId,
state: VarState,
name: Option<Name>,
depth: u32, // depth of conditional expressions
Expand All @@ -2119,7 +2118,7 @@ impl<'a, 'tcx> Visitor<'tcx> for InitializeVisitor<'a, 'tcx> {
fn visit_stmt(&mut self, stmt: &'tcx Stmt) {
// Look for declarations of the variable
if let StmtKind::Local(ref local) = stmt.node {
if local.pat.id == self.var_id {
if local.pat.hir_id == self.var_id {
if let PatKind::Binding(.., ident, _) = local.pat.node {
self.name = Some(ident.name);

Expand Down Expand Up @@ -2191,11 +2190,11 @@ impl<'a, 'tcx> Visitor<'tcx> for InitializeVisitor<'a, 'tcx> {
}
}

fn var_def_id(cx: &LateContext<'_, '_>, expr: &Expr) -> Option<NodeId> {
fn var_def_id(cx: &LateContext<'_, '_>, expr: &Expr) -> Option<HirId> {
if let ExprKind::Path(ref qpath) = expr.node {
let path_res = cx.tables.qpath_def(qpath, expr.hir_id);
if let Def::Local(node_id) = path_res {
return Some(node_id);
return Some(cx.tcx.hir().node_to_hir_id(node_id));
}
}
None
Expand Down Expand Up @@ -2376,7 +2375,7 @@ fn check_infinite_loop<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, cond: &'tcx Expr, e
/// All variables definition IDs are collected
struct VarCollectorVisitor<'a, 'tcx: 'a> {
cx: &'a LateContext<'a, 'tcx>,
ids: FxHashSet<NodeId>,
ids: FxHashSet<HirId>,
def_ids: FxHashMap<def_id::DefId, bool>,
skip: bool,
}
Expand All @@ -2390,7 +2389,7 @@ impl<'a, 'tcx> VarCollectorVisitor<'a, 'tcx> {
then {
match def {
Def::Local(node_id) | Def::Upvar(node_id, ..) => {
self.ids.insert(node_id);
self.ids.insert(self.cx.tcx.hir().node_to_hir_id(node_id));
},
Def::Static(def_id, mutable) => {
self.def_ids.insert(def_id, mutable);
Expand Down
Loading

0 comments on commit 9d1792a

Please sign in to comment.