Factor out hir::Node::Binding
This commit is contained in:
parent
ca1e68b322
commit
ec82bc1996
@ -192,9 +192,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn visit_pat(&mut self, pat: &'hir Pat<'hir>) {
|
fn visit_pat(&mut self, pat: &'hir Pat<'hir>) {
|
||||||
let node =
|
self.insert(pat.span, pat.hir_id, Node::Pat(pat));
|
||||||
if let PatKind::Binding(..) = pat.kind { Node::Binding(pat) } else { Node::Pat(pat) };
|
|
||||||
self.insert(pat.span, pat.hir_id, node);
|
|
||||||
|
|
||||||
self.with_parent(pat.hir_id, |this| {
|
self.with_parent(pat.hir_id, |this| {
|
||||||
intravisit::walk_pat(this, pat);
|
intravisit::walk_pat(this, pat);
|
||||||
|
@ -362,7 +362,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
|
|||||||
|
|
||||||
let upvar_hir_id = captured_place.get_root_variable();
|
let upvar_hir_id = captured_place.get_root_variable();
|
||||||
|
|
||||||
if let Some(Node::Binding(pat)) = self.infcx.tcx.hir().find(upvar_hir_id)
|
if let Some(Node::Pat(pat)) = self.infcx.tcx.hir().find(upvar_hir_id)
|
||||||
&& let hir::PatKind::Binding(
|
&& let hir::PatKind::Binding(
|
||||||
hir::BindingAnnotation::Unannotated,
|
hir::BindingAnnotation::Unannotated,
|
||||||
_,
|
_,
|
||||||
|
@ -244,7 +244,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
|
|||||||
// for a generator).
|
// for a generator).
|
||||||
let var_hir_id = captured_place.get_root_variable();
|
let var_hir_id = captured_place.get_root_variable();
|
||||||
let node = self.ecx.tcx.hir().get(var_hir_id);
|
let node = self.ecx.tcx.hir().get(var_hir_id);
|
||||||
if let hir::Node::Binding(pat) = node {
|
if let hir::Node::Pat(pat) = node {
|
||||||
if let hir::PatKind::Binding(_, _, ident, _) = pat.kind {
|
if let hir::PatKind::Binding(_, _, ident, _) = pat.kind {
|
||||||
name = Some(ident.name);
|
name = Some(ident.name);
|
||||||
}
|
}
|
||||||
|
@ -3326,7 +3326,6 @@ pub enum Node<'hir> {
|
|||||||
Ty(&'hir Ty<'hir>),
|
Ty(&'hir Ty<'hir>),
|
||||||
TypeBinding(&'hir TypeBinding<'hir>),
|
TypeBinding(&'hir TypeBinding<'hir>),
|
||||||
TraitRef(&'hir TraitRef<'hir>),
|
TraitRef(&'hir TraitRef<'hir>),
|
||||||
Binding(&'hir Pat<'hir>),
|
|
||||||
Pat(&'hir Pat<'hir>),
|
Pat(&'hir Pat<'hir>),
|
||||||
Arm(&'hir Arm<'hir>),
|
Arm(&'hir Arm<'hir>),
|
||||||
Block(&'hir Block<'hir>),
|
Block(&'hir Block<'hir>),
|
||||||
@ -3378,7 +3377,6 @@ impl<'hir> Node<'hir> {
|
|||||||
| Node::Block(..)
|
| Node::Block(..)
|
||||||
| Node::Ctor(..)
|
| Node::Ctor(..)
|
||||||
| Node::Pat(..)
|
| Node::Pat(..)
|
||||||
| Node::Binding(..)
|
|
||||||
| Node::Arm(..)
|
| Node::Arm(..)
|
||||||
| Node::Local(..)
|
| Node::Local(..)
|
||||||
| Node::Crate(..)
|
| Node::Crate(..)
|
||||||
|
@ -87,7 +87,7 @@ impl<'a> State<'a> {
|
|||||||
Node::Ty(a) => self.print_type(&a),
|
Node::Ty(a) => self.print_type(&a),
|
||||||
Node::TypeBinding(a) => self.print_type_binding(&a),
|
Node::TypeBinding(a) => self.print_type_binding(&a),
|
||||||
Node::TraitRef(a) => self.print_trait_ref(&a),
|
Node::TraitRef(a) => self.print_trait_ref(&a),
|
||||||
Node::Binding(a) | Node::Pat(a) => self.print_pat(&a),
|
Node::Pat(a) => self.print_pat(&a),
|
||||||
Node::Arm(a) => self.print_arm(&a),
|
Node::Arm(a) => self.print_arm(&a),
|
||||||
Node::Infer(_) => self.word("_"),
|
Node::Infer(_) => self.word("_"),
|
||||||
Node::Block(a) => {
|
Node::Block(a) => {
|
||||||
|
@ -302,7 +302,6 @@ impl<'hir> Map<'hir> {
|
|||||||
| Node::Infer(_)
|
| Node::Infer(_)
|
||||||
| Node::TraitRef(_)
|
| Node::TraitRef(_)
|
||||||
| Node::Pat(_)
|
| Node::Pat(_)
|
||||||
| Node::Binding(_)
|
|
||||||
| Node::Local(_)
|
| Node::Local(_)
|
||||||
| Node::Param(_)
|
| Node::Param(_)
|
||||||
| Node::Arm(_)
|
| Node::Arm(_)
|
||||||
@ -901,7 +900,7 @@ impl<'hir> Map<'hir> {
|
|||||||
#[inline]
|
#[inline]
|
||||||
fn opt_ident(self, id: HirId) -> Option<Ident> {
|
fn opt_ident(self, id: HirId) -> Option<Ident> {
|
||||||
match self.get(id) {
|
match self.get(id) {
|
||||||
Node::Binding(&Pat { kind: PatKind::Binding(_, _, ident, _), .. }) => Some(ident),
|
Node::Pat(&Pat { kind: PatKind::Binding(_, _, ident, _), .. }) => Some(ident),
|
||||||
// A `Ctor` doesn't have an identifier itself, but its parent
|
// A `Ctor` doesn't have an identifier itself, but its parent
|
||||||
// struct/variant does. Compare with `hir::Map::opt_span`.
|
// struct/variant does. Compare with `hir::Map::opt_span`.
|
||||||
Node::Ctor(..) => match self.find(self.get_parent_node(id))? {
|
Node::Ctor(..) => match self.find(self.get_parent_node(id))? {
|
||||||
@ -969,7 +968,6 @@ impl<'hir> Map<'hir> {
|
|||||||
Node::Ty(ty) => ty.span,
|
Node::Ty(ty) => ty.span,
|
||||||
Node::TypeBinding(tb) => tb.span,
|
Node::TypeBinding(tb) => tb.span,
|
||||||
Node::TraitRef(tr) => tr.path.span,
|
Node::TraitRef(tr) => tr.path.span,
|
||||||
Node::Binding(pat) => pat.span,
|
|
||||||
Node::Pat(pat) => pat.span,
|
Node::Pat(pat) => pat.span,
|
||||||
Node::Arm(arm) => arm.span,
|
Node::Arm(arm) => arm.span,
|
||||||
Node::Block(block) => block.span,
|
Node::Block(block) => block.span,
|
||||||
@ -1203,7 +1201,6 @@ fn hir_id_to_string(map: Map<'_>, id: HirId) -> String {
|
|||||||
Some(Node::Ty(_)) => node_str("type"),
|
Some(Node::Ty(_)) => node_str("type"),
|
||||||
Some(Node::TypeBinding(_)) => node_str("type binding"),
|
Some(Node::TypeBinding(_)) => node_str("type binding"),
|
||||||
Some(Node::TraitRef(_)) => node_str("trait ref"),
|
Some(Node::TraitRef(_)) => node_str("trait ref"),
|
||||||
Some(Node::Binding(_)) => node_str("local"),
|
|
||||||
Some(Node::Pat(_)) => node_str("pat"),
|
Some(Node::Pat(_)) => node_str("pat"),
|
||||||
Some(Node::Param(_)) => node_str("param"),
|
Some(Node::Param(_)) => node_str("param"),
|
||||||
Some(Node::Arm(_)) => node_str("arm"),
|
Some(Node::Arm(_)) => node_str("arm"),
|
||||||
|
@ -997,7 +997,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
let pat = match tcx.hir().get(arg.pat.hir_id) {
|
let pat = match tcx.hir().get(arg.pat.hir_id) {
|
||||||
Node::Pat(pat) | Node::Binding(pat) => pat,
|
Node::Pat(pat) => pat,
|
||||||
node => bug!("pattern became {:?}", node),
|
node => bug!("pattern became {:?}", node),
|
||||||
};
|
};
|
||||||
let pattern = pat_from_hir(tcx, self.param_env, self.typeck_results, pat);
|
let pattern = pat_from_hir(tcx, self.param_env, self.typeck_results, pat);
|
||||||
|
@ -101,7 +101,7 @@ impl<'tcx> Cx<'tcx> {
|
|||||||
#[tracing::instrument(level = "debug", skip(self))]
|
#[tracing::instrument(level = "debug", skip(self))]
|
||||||
pub(crate) fn pattern_from_hir(&mut self, p: &hir::Pat<'_>) -> Pat<'tcx> {
|
pub(crate) fn pattern_from_hir(&mut self, p: &hir::Pat<'_>) -> Pat<'tcx> {
|
||||||
let p = match self.tcx.hir().get(p.hir_id) {
|
let p = match self.tcx.hir().get(p.hir_id) {
|
||||||
Node::Pat(p) | Node::Binding(p) => p,
|
Node::Pat(p) => p,
|
||||||
node => bug!("pattern became {:?}", node),
|
node => bug!("pattern became {:?}", node),
|
||||||
};
|
};
|
||||||
pat_from_hir(self.tcx, self.param_env, self.typeck_results(), p)
|
pat_from_hir(self.tcx, self.param_env, self.typeck_results(), p)
|
||||||
|
@ -623,9 +623,9 @@ impl<'tcx> SaveContext<'tcx> {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
Node::Binding(&hir::Pat {
|
Node::Pat(&hir::Pat { kind: hir::PatKind::Binding(_, canonical_id, ..), .. }) => {
|
||||||
kind: hir::PatKind::Binding(_, canonical_id, ..), ..
|
Res::Local(canonical_id)
|
||||||
}) => Res::Local(canonical_id),
|
}
|
||||||
|
|
||||||
_ => Res::Err,
|
_ => Res::Err,
|
||||||
}
|
}
|
||||||
|
@ -223,7 +223,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||||||
None,
|
None,
|
||||||
hir::Path { res: hir::def::Res::Local(hir_id), .. },
|
hir::Path { res: hir::def::Res::Local(hir_id), .. },
|
||||||
)) => {
|
)) => {
|
||||||
if let Some(hir::Node::Binding(pat)) = self.tcx.hir().find(*hir_id) {
|
if let Some(hir::Node::Pat(pat)) = self.tcx.hir().find(*hir_id) {
|
||||||
let parent = self.tcx.hir().get_parent_node(pat.hir_id);
|
let parent = self.tcx.hir().get_parent_node(pat.hir_id);
|
||||||
primary_span = pat.span;
|
primary_span = pat.span;
|
||||||
secondary_span = pat.span;
|
secondary_span = pat.span;
|
||||||
|
@ -251,7 +251,6 @@ impl<'a, 'tcx> DropRangeVisitor<'a, 'tcx> {
|
|||||||
| hir::Node::Ty(..)
|
| hir::Node::Ty(..)
|
||||||
| hir::Node::TypeBinding(..)
|
| hir::Node::TypeBinding(..)
|
||||||
| hir::Node::TraitRef(..)
|
| hir::Node::TraitRef(..)
|
||||||
| hir::Node::Binding(..)
|
|
||||||
| hir::Node::Pat(..)
|
| hir::Node::Pat(..)
|
||||||
| hir::Node::Arm(..)
|
| hir::Node::Arm(..)
|
||||||
| hir::Node::Local(..)
|
| hir::Node::Local(..)
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use clippy_utils::diagnostics::span_lint_hir;
|
use clippy_utils::diagnostics::span_lint_hir;
|
||||||
use clippy_utils::ty::contains_ty;
|
use clippy_utils::ty::contains_ty;
|
||||||
use rustc_hir::intravisit;
|
use rustc_hir::intravisit;
|
||||||
use rustc_hir::{self, AssocItemKind, Body, FnDecl, HirId, HirIdSet, Impl, ItemKind, Node};
|
use rustc_hir::{self, AssocItemKind, Body, FnDecl, HirId, HirIdSet, Impl, ItemKind, Node, Pat, PatKind};
|
||||||
use rustc_infer::infer::TyCtxtInferExt;
|
use rustc_infer::infer::TyCtxtInferExt;
|
||||||
use rustc_lint::{LateContext, LateLintPass};
|
use rustc_lint::{LateContext, LateLintPass};
|
||||||
use rustc_middle::mir::FakeReadCause;
|
use rustc_middle::mir::FakeReadCause;
|
||||||
@ -132,7 +132,10 @@ impl<'tcx> LateLintPass<'tcx> for BoxedLocal {
|
|||||||
// TODO: Replace with Map::is_argument(..) when it's fixed
|
// TODO: Replace with Map::is_argument(..) when it's fixed
|
||||||
fn is_argument(map: rustc_middle::hir::map::Map<'_>, id: HirId) -> bool {
|
fn is_argument(map: rustc_middle::hir::map::Map<'_>, id: HirId) -> bool {
|
||||||
match map.find(id) {
|
match map.find(id) {
|
||||||
Some(Node::Binding(_)) => (),
|
Some(Node::Pat(Pat {
|
||||||
|
kind: PatKind::Binding(..),
|
||||||
|
..
|
||||||
|
})) => (),
|
||||||
_ => return false,
|
_ => return false,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -144,15 +147,6 @@ impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
|
|||||||
if cmt.place.projections.is_empty() {
|
if cmt.place.projections.is_empty() {
|
||||||
if let PlaceBase::Local(lid) = cmt.place.base {
|
if let PlaceBase::Local(lid) = cmt.place.base {
|
||||||
self.set.remove(&lid);
|
self.set.remove(&lid);
|
||||||
let map = &self.cx.tcx.hir();
|
|
||||||
if let Some(Node::Binding(_)) = map.find(cmt.hir_id) {
|
|
||||||
if self.set.contains(&lid) {
|
|
||||||
// let y = x where x is known
|
|
||||||
// remove x, insert y
|
|
||||||
self.set.insert(cmt.hir_id);
|
|
||||||
self.set.remove(&lid);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -125,7 +125,7 @@ fn look_in_block<'tcx, 'hir>(cx: &LateContext<'tcx>, kind: &'tcx ExprKind<'hir>)
|
|||||||
// Find id of the local that expr_end_of_block resolves to
|
// Find id of the local that expr_end_of_block resolves to
|
||||||
if let ExprKind::Path(QPath::Resolved(None, expr_path)) = expr_end_of_block.kind;
|
if let ExprKind::Path(QPath::Resolved(None, expr_path)) = expr_end_of_block.kind;
|
||||||
if let Res::Local(expr_res) = expr_path.res;
|
if let Res::Local(expr_res) = expr_path.res;
|
||||||
if let Some(Node::Binding(res_pat)) = cx.tcx.hir().find(expr_res);
|
if let Some(Node::Pat(res_pat)) = cx.tcx.hir().find(expr_res);
|
||||||
|
|
||||||
// Find id of the local we found in the block
|
// Find id of the local we found in the block
|
||||||
if let PatKind::Binding(BindingAnnotation::Unannotated, local_hir_id, _ident, None) = local.pat.kind;
|
if let PatKind::Binding(BindingAnnotation::Unannotated, local_hir_id, _ident, None) = local.pat.kind;
|
||||||
|
@ -43,7 +43,7 @@ fn mut_warn_with_span(cx: &LateContext<'_>, span: Option<Span>) {
|
|||||||
fn check_for_mutability(cx: &LateContext<'_>, bound: &Expr<'_>) -> Option<HirId> {
|
fn check_for_mutability(cx: &LateContext<'_>, bound: &Expr<'_>) -> Option<HirId> {
|
||||||
if_chain! {
|
if_chain! {
|
||||||
if let Some(hir_id) = path_to_local(bound);
|
if let Some(hir_id) = path_to_local(bound);
|
||||||
if let Node::Binding(pat) = cx.tcx.hir().get(hir_id);
|
if let Node::Pat(pat) = cx.tcx.hir().get(hir_id);
|
||||||
if let PatKind::Binding(BindingAnnotation::Mutable, ..) = pat.kind;
|
if let PatKind::Binding(BindingAnnotation::Mutable, ..) = pat.kind;
|
||||||
then {
|
then {
|
||||||
return Some(hir_id);
|
return Some(hir_id);
|
||||||
|
@ -63,7 +63,7 @@ pub(super) fn check<'tcx>(
|
|||||||
Res::Local(hir_id) => {
|
Res::Local(hir_id) => {
|
||||||
let node = cx.tcx.hir().get(hir_id);
|
let node = cx.tcx.hir().get(hir_id);
|
||||||
if_chain! {
|
if_chain! {
|
||||||
if let Node::Binding(pat) = node;
|
if let Node::Pat(pat) = node;
|
||||||
if let PatKind::Binding(bind_ann, ..) = pat.kind;
|
if let PatKind::Binding(bind_ann, ..) = pat.kind;
|
||||||
if !matches!(bind_ann, BindingAnnotation::RefMut | BindingAnnotation::Mutable);
|
if !matches!(bind_ann, BindingAnnotation::RefMut | BindingAnnotation::Mutable);
|
||||||
let parent_node = cx.tcx.hir().get_parent_node(hir_id);
|
let parent_node = cx.tcx.hir().get_parent_node(hir_id);
|
||||||
|
@ -71,8 +71,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualRemEuclid {
|
|||||||
&& let Some(const3) = check_for_unsigned_int_constant(cx, right)
|
&& let Some(const3) = check_for_unsigned_int_constant(cx, right)
|
||||||
// Also ensures the const is nonzero since zero can't be a divisor
|
// Also ensures the const is nonzero since zero can't be a divisor
|
||||||
&& const1 == const2 && const2 == const3
|
&& const1 == const2 && const2 == const3
|
||||||
&& let Some(hir_id) = path_to_local(expr3)
|
&& let Some(hir_id) = path_to_local(expr3) {
|
||||||
&& let Some(Node::Binding(_)) = cx.tcx.hir().find(hir_id) {
|
|
||||||
// Apply only to params or locals with annotated types
|
// Apply only to params or locals with annotated types
|
||||||
match cx.tcx.hir().find(cx.tcx.hir().get_parent_node(hir_id)) {
|
match cx.tcx.hir().find(cx.tcx.hir().get_parent_node(hir_id)) {
|
||||||
Some(Node::Param(..)) => (),
|
Some(Node::Param(..)) => (),
|
||||||
|
@ -22,7 +22,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr
|
|||||||
|diag| {
|
|diag| {
|
||||||
if_chain! {
|
if_chain! {
|
||||||
if let Some(id) = path_to_local(recv);
|
if let Some(id) = path_to_local(recv);
|
||||||
if let Node::Binding(pat) = cx.tcx.hir().get(id);
|
if let Node::Pat(pat) = cx.tcx.hir().get(id);
|
||||||
if let PatKind::Binding(ann, _, _, _) = pat.kind;
|
if let PatKind::Binding(ann, _, _, _) = pat.kind;
|
||||||
if ann != BindingAnnotation::Mutable;
|
if ann != BindingAnnotation::Mutable;
|
||||||
then {
|
then {
|
||||||
|
@ -183,7 +183,7 @@ pub fn expr_or_init<'a, 'b, 'tcx: 'b>(cx: &LateContext<'tcx>, mut expr: &'a Expr
|
|||||||
pub fn find_binding_init<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId) -> Option<&'tcx Expr<'tcx>> {
|
pub fn find_binding_init<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId) -> Option<&'tcx Expr<'tcx>> {
|
||||||
let hir = cx.tcx.hir();
|
let hir = cx.tcx.hir();
|
||||||
if_chain! {
|
if_chain! {
|
||||||
if let Some(Node::Binding(pat)) = hir.find(hir_id);
|
if let Some(Node::Pat(pat)) = hir.find(hir_id);
|
||||||
if matches!(pat.kind, PatKind::Binding(BindingAnnotation::Unannotated, ..));
|
if matches!(pat.kind, PatKind::Binding(BindingAnnotation::Unannotated, ..));
|
||||||
let parent = hir.get_parent_node(hir_id);
|
let parent = hir.get_parent_node(hir_id);
|
||||||
if let Some(Node::Local(local)) = hir.find(parent);
|
if let Some(Node::Local(local)) = hir.find(parent);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user