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(
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);
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");
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 {
struct EscapeDelegate<'a, 'tcx: 'a> {
cx: &'a LateContext<'a, 'tcx>,
- set: NodeSet,
+ set: HirIdSet,
too_large_for_stack: u64,
}
let mut v = EscapeDelegate {
cx,
- set: NodeSet::default(),
+ set: HirIdSet::default(),
too_large_for_stack: self.too_large_for_stack,
};
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",
);
}
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;
}
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![]
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);
}
}
}
}
}
- 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) {}
}
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);
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),
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");
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 {
}
}
- 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
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 {
}
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"
};
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(
}
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>,
}
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)
}
}
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 {
}
}
-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;
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));
}
}
}
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,
};
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,
/// 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
+ states: FxHashMap<HirId, VarState>, // incremented variables
depth: u32, // depth of conditional expressions
done: bool,
}
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
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);
}
}
-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
/// 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,
}
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);
use rustc::hir::def::Def;
use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
use rustc::lint::LateContext;
-use syntax::ast;
use if_chain::if_chain;
if let hir::ExprKind::Closure(_, _, body_id, ..) = args[1].node {
let body = cx.tcx.hir().body(body_id);
- let arg_id = body.arguments[0].pat.id;
+ let arg_id = body.arguments[0].pat.hir_id;
let mutates_arg = match mutated_variables(&body.value, cx) {
Some(used_mutably) => used_mutably.contains(&arg_id),
None => true,
// returns (found_mapping, found_filtering)
fn check_expression<'a, 'tcx: 'a>(
cx: &'a LateContext<'a, 'tcx>,
- arg_id: ast::NodeId,
+ arg_id: hir::HirId,
expr: &'tcx hir::Expr,
) -> (bool, bool) {
match &expr.node {
if let hir::ExprKind::Path(path) = &args[0].node;
if let Def::Local(ref local) = cx.tables.qpath_def(path, args[0].hir_id);
then {
- if arg_id == *local {
+ if arg_id == cx.tcx.hir().node_to_hir_id(*local) {
return (false, false)
}
}
struct ReturnVisitor<'a, 'tcx: 'a> {
cx: &'a LateContext<'a, 'tcx>,
- arg_id: ast::NodeId,
+ arg_id: hir::HirId,
// Found a non-None return that isn't Some(input)
found_mapping: bool,
// Found a return that isn't Some
}
impl<'a, 'tcx: 'a> ReturnVisitor<'a, 'tcx> {
- fn new(cx: &'a LateContext<'a, 'tcx>, arg_id: ast::NodeId) -> ReturnVisitor<'a, 'tcx> {
+ fn new(cx: &'a LateContext<'a, 'tcx>, arg_id: hir::HirId) -> ReturnVisitor<'a, 'tcx> {
ReturnVisitor {
cx,
arg_id,
hir::ItemKind::Fn(..) => {
// ignore main()
if it.ident.name == "main" {
- let def_id = cx.tcx.hir().local_def_id(it.id);
+ let def_id = cx.tcx.hir().local_def_id_from_hir_id(it.hir_id);
let def_key = cx.tcx.hir().def_key(def_id);
if def_key.parent == Some(hir::def_id::CRATE_DEF_INDEX) {
return;
return;
}
- if !cx.access_levels.is_exported(it.id) {
+ if !cx.access_levels.is_exported(cx.tcx.hir().hir_to_node_id(it.hir_id)) {
return;
}
match it.node {
// trait method with default body needs inline in case
// an impl is not provided
let desc = "a default trait method";
- let item = cx.tcx.hir().expect_trait_item(tit.id.node_id);
+ let item = cx.tcx.hir().expect_trait_item_by_hir_id(tit.id.hir_id);
check_missing_inline_attrs(cx, &item.attrs, item.span, desc);
}
},
use crate::utils::{in_macro, snippet_opt, span_lint_and_then};
use if_chain::if_chain;
-use rustc::hir::{BindingAnnotation, Expr, ExprKind, Item, MutImmutable, Pat, PatKind};
+use rustc::hir::{BindingAnnotation, Expr, ExprKind, HirId, Item, MutImmutable, Pat, PatKind};
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use rustc::ty;
use rustc::ty::adjustment::{Adjust, Adjustment};
use rustc::{declare_tool_lint, lint_array};
use rustc_errors::Applicability;
-use syntax::ast::NodeId;
/// **What it does:** Checks for address of operations (`&`) that are going to
/// be dereferenced immediately by the compiler.
#[derive(Default)]
pub struct NeedlessBorrow {
- derived_item: Option<NodeId>,
+ derived_item: Option<HirId>,
}
impl LintPass for NeedlessBorrow {
fn check_item(&mut self, _: &LateContext<'a, 'tcx>, item: &'tcx Item) {
if item.attrs.iter().any(|a| a.check_name("automatically_derived")) {
debug_assert!(self.derived_item.is_none());
- self.derived_item = Some(item.id);
+ self.derived_item = Some(item.hir_id);
}
}
fn check_item_post(&mut self, _: &LateContext<'a, 'tcx>, item: &'tcx Item) {
if let Some(id) = self.derived_item {
- if item.id == id {
+ if item.hir_id == id {
self.derived_item = None;
}
}
use rustc_errors::Applicability;
use rustc_target::spec::abi::Abi;
use std::borrow::Cow;
-use syntax::ast::NodeId;
use syntax::errors::DiagnosticBuilder;
use syntax_pos::Span;
if !implements_borrow_trait;
if !all_borrowable_trait;
- if let PatKind::Binding(mode, canonical_id, ..) = arg.pat.node;
+ if let PatKind::Binding(mode, _, canonical_id, ..) = arg.pat.node;
if !moved_vars.contains(&canonical_id);
then {
if mode == BindingAnnotation::Mutable || mode == BindingAnnotation::RefMut {
struct MovedVariablesCtxt<'a, 'tcx: 'a> {
cx: &'a LateContext<'a, 'tcx>,
- moved_vars: FxHashSet<NodeId>,
+ moved_vars: FxHashSet<HirId>,
/// Spans which need to be prefixed with `*` for dereferencing the
/// suggested additional reference.
- spans_need_deref: FxHashMap<NodeId, FxHashSet<Span>>,
+ spans_need_deref: FxHashMap<HirId, FxHashSet<Span>>,
}
impl<'a, 'tcx> MovedVariablesCtxt<'a, 'tcx> {
let cmt = unwrap_downcast_or_interior(cmt);
if let mc::Categorization::Local(vid) = cmt.cat {
- let mut id = matched_pat.id;
+ let mut id = matched_pat.hir_id;
loop {
- let parent = self.cx.tcx.hir().get_parent_node(id);
+ let parent = self.cx.tcx.hir().get_parent_node_by_hir_id(id);
if id == parent {
// no parent
return;
}
id = parent;
- if let Some(node) = self.cx.tcx.hir().find(id) {
+ if let Some(node) = self.cx.tcx.hir().find_by_hir_id(id) {
match node {
Node::Expr(e) => {
// `match` and `if let`
fn mutate(&mut self, _: HirId, _: Span, _: &mc::cmt_<'tcx>, _: euv::MutateMode) {}
- fn decl_without_init(&mut self, _: NodeId, _: Span) {}
+ fn decl_without_init(&mut self, _: HirId, _: Span) {}
}
fn unwrap_downcast_or_interior<'a, 'tcx>(mut cmt: &'a mc::cmt_<'tcx>) -> mc::cmt_<'tcx> {
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,
- hir_id,
+ impl_item.id.hir_id,
impl_item.span,
"re-implementing `PartialEq::ne` is unnecessary",
);
}
fn check_trait_method(&mut self, cx: &LateContext<'_, 'tcx>, item: &TraitItemRef) {
- let method_def_id = cx.tcx.hir().local_def_id(item.id.node_id);
+ let method_def_id = cx.tcx.hir().local_def_id_from_hir_id(item.id.hir_id);
let method_sig = cx.tcx.fn_sig(method_def_id);
let method_sig = cx.tcx.erase_late_bound_regions(&method_sig);
- let decl = match cx.tcx.hir().fn_decl(item.id.node_id) {
+ let decl = match cx.tcx.hir().fn_decl_by_hir_id(item.id.hir_id) {
Some(b) => b,
None => return,
};
}
}
- if !cx.access_levels.is_exported(item.id) {
+ if !cx.access_levels.is_exported(cx.tcx.hir().hir_to_node_id(item.hir_id)) {
return;
}
item_path,
cx,
};
- 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);
let impl_trait_ref = cx.tcx.impl_trait_ref(impl_def_id);
if let Some(impl_trait_ref) = impl_trait_ref {
}
fn print_item(cx: &LateContext<'_, '_>, item: &hir::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);
println!("item `{}`", item.ident.name);
match item.vis.node {
hir::VisibilityKind::Public => println!("public"),
}
match item.node {
hir::ItemKind::ExternCrate(ref _renamed_from) => {
- let def_id = cx.tcx.hir().local_def_id(item.id);
+ let def_id = cx.tcx.hir().local_def_id_from_hir_id(item.hir_id);
if let Some(crate_id) = cx.tcx.extern_mod_stmt_cnum(def_id) {
let source = cx.tcx.used_crate_source(crate_id);
if let Some(ref src) = source.dylib {
output: &mut self.registered_lints,
cx,
};
- let body_id = cx.tcx.hir().body_owned_by(impl_item_refs[0].id.node_id);
+ let node_id = cx.tcx.hir().hir_to_node_id(impl_item_refs[0].id.hir_id);
+ let body_id = cx.tcx.hir().body_owned_by(node_id);
collector.visit_expr(&cx.tcx.hir().body(body_id).value);
}
}
use rustc::middle::mem_categorization::Categorization;
use rustc::ty;
use rustc_data_structures::fx::FxHashSet;
-use syntax::ast::NodeId;
use syntax::source_map::Span;
/// Returns a set of mutated local variable ids or None if mutations could not be determined.
-pub fn mutated_variables<'a, 'tcx: 'a>(expr: &'tcx Expr, cx: &'a LateContext<'a, 'tcx>) -> Option<FxHashSet<NodeId>> {
+pub fn mutated_variables<'a, 'tcx: 'a>(expr: &'tcx Expr, cx: &'a LateContext<'a, 'tcx>) -> Option<FxHashSet<HirId>> {
let mut delegate = MutVarsDelegate {
used_mutably: FxHashSet::default(),
skip: false,
Def::Local(id) | Def::Upvar(id, ..) => id,
_ => return true,
};
- mutated_variables(expr, cx).map_or(true, |mutated| mutated.contains(&id))
+ mutated_variables(expr, cx).map_or(true, |mutated| mutated.contains(&cx.tcx.hir().node_to_hir_id(id)))
}
struct MutVarsDelegate {
- used_mutably: FxHashSet<NodeId>,
+ used_mutably: FxHashSet<HirId>,
skip: bool,
}
self.update(&cmt.cat)
}
- fn decl_without_init(&mut self, _: NodeId, _: Span) {}
+ fn decl_without_init(&mut self, _: HirId, _: Span) {}
}