Skip to content

Commit f55a049

Browse files
committed
hir: Remove opt_local_def_id_to_hir_id and opt_hir_node_by_def_id
Also replace a few `hir_node()` calls with `hir_node_by_def_id()`
1 parent 97f2ade commit f55a049

11 files changed

+21
-31
lines changed

clippy_lints/src/escape.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,9 @@ impl<'tcx> LateLintPass<'tcx> for BoxedLocal {
7676
.hir()
7777
.get_parent_item(cx.tcx.local_def_id_to_hir_id(fn_def_id))
7878
.def_id;
79-
let parent_node = cx.tcx.opt_hir_node_by_def_id(parent_id);
8079

8180
let mut trait_self_ty = None;
82-
if let Some(Node::Item(item)) = parent_node {
81+
if let Node::Item(item) = cx.tcx.hir_node_by_def_id(parent_id) {
8382
// If the method is an impl for a trait, don't warn.
8483
if let ItemKind::Impl(Impl { of_trait: Some(_), .. }) = item.kind {
8584
return;

clippy_lints/src/exit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<'tcx> LateLintPass<'tcx> for Exit {
4646
&& let Some(def_id) = cx.qpath_res(path, path_expr.hir_id).opt_def_id()
4747
&& cx.tcx.is_diagnostic_item(sym::process_exit, def_id)
4848
&& let parent = cx.tcx.hir().get_parent_item(e.hir_id).def_id
49-
&& let Some(Node::Item(Item{kind: ItemKind::Fn(..), ..})) = cx.tcx.opt_hir_node_by_def_id(parent)
49+
&& let Node::Item(Item{kind: ItemKind::Fn(..), ..}) = cx.tcx.hir_node_by_def_id(parent)
5050
// If the next item up is a function we check if it is an entry point
5151
// and only then emit a linter warning
5252
&& !is_entrypoint_fn(cx, parent.to_def_id())

clippy_lints/src/functions/result.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ fn check_result_large_err<'tcx>(cx: &LateContext<'tcx>, err_ty: Ty<'tcx>, hir_ty
9292
.expect("already checked this is adt")
9393
.did()
9494
.as_local()
95-
&& let Some(hir::Node::Item(item)) = cx.tcx.opt_hir_node_by_def_id(local_def_id)
95+
&& let hir::Node::Item(item) = cx.tcx.hir_node_by_def_id(local_def_id)
9696
&& let hir::ItemKind::Enum(ref def, _) = item.kind
9797
{
9898
let variants_size = AdtVariantInfo::new(cx, *adt, subst);

clippy_lints/src/misc.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,9 @@ impl<'tcx> LateLintPass<'tcx> for LintPass {
225225
if let Some(adt_def) = cx.typeck_results().expr_ty_adjusted(recv).ty_adt_def()
226226
&& let Some(field) = adt_def.all_fields().find(|field| field.name == ident.name)
227227
&& let Some(local_did) = field.did.as_local()
228-
&& let Some(hir_id) = cx.tcx.opt_local_def_id_to_hir_id(local_did)
229228
&& !cx.tcx.type_of(field.did).skip_binder().is_phantom_data()
230229
{
231-
(hir_id, ident)
230+
(cx.tcx.local_def_id_to_hir_id(local_did), ident)
232231
} else {
233232
return;
234233
}

clippy_lints/src/missing_fields_in_debug.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingFieldsInDebug {
220220
&& let self_ty = cx.tcx.type_of(self_path_did).skip_binder().peel_refs()
221221
&& let Some(self_adt) = self_ty.ty_adt_def()
222222
&& let Some(self_def_id) = self_adt.did().as_local()
223-
&& let Some(Node::Item(self_item)) = cx.tcx.opt_hir_node_by_def_id(self_def_id)
223+
&& let Node::Item(self_item) = cx.tcx.hir_node_by_def_id(self_def_id)
224224
// NB: can't call cx.typeck_results() as we are not in a body
225225
&& let typeck_results = cx.tcx.typeck_body(*body_id)
226226
&& should_lint(cx, typeck_results, block)

clippy_lints/src/needless_pass_by_ref_mut.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,7 @@ fn check_closures<'tcx>(
112112
}
113113
ctx.prev_bind = None;
114114
ctx.prev_move_to_closure.clear();
115-
if let Some(body) = cx
116-
.tcx
117-
.opt_hir_node_by_def_id(closure)
118-
.and_then(associated_body)
115+
if let Some(body) = associated_body(cx.tcx.hir_node_by_def_id(closure))
119116
.map(|(_, body_id)| hir.body(body_id))
120117
{
121118
euv::ExprUseVisitor::new(ctx, infcx, closure, cx.param_env, cx.typeck_results()).consume_body(body);

clippy_lints/src/self_named_constructors.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ impl<'tcx> LateLintPass<'tcx> for SelfNamedConstructors {
7272

7373
if let Some(self_def) = self_ty.ty_adt_def()
7474
&& let Some(self_local_did) = self_def.did().as_local()
75-
&& let self_id = cx.tcx.local_def_id_to_hir_id(self_local_did)
76-
&& let Node::Item(x) = cx.tcx.hir_node(self_id)
75+
&& let Node::Item(x) = cx.tcx.hir_node_by_def_id(self_local_did)
7776
&& let type_name = x.ident.name.as_str().to_lowercase()
7877
&& (impl_item.ident.name.as_str() == type_name
7978
|| impl_item.ident.name.as_str().replace('_', "") == type_name)

clippy_lints/src/single_call_fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl SingleCallFn {
9595
/// to be considered.
9696
fn is_valid_item_kind(cx: &LateContext<'_>, def_id: LocalDefId) -> bool {
9797
matches!(
98-
cx.tcx.hir_node(cx.tcx.local_def_id_to_hir_id(def_id)),
98+
cx.tcx.hir_node_by_def_id(def_id),
9999
Node::Item(_) | Node::ImplItem(_) | Node::TraitItem(_)
100100
)
101101
}

clippy_lints/src/types/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ impl<'tcx> LateLintPass<'tcx> for Types {
321321
_: Span,
322322
def_id: LocalDefId,
323323
) {
324-
let is_in_trait_impl = if let Some(hir::Node::Item(item)) = cx.tcx.opt_hir_node_by_def_id(
324+
let is_in_trait_impl = if let hir::Node::Item(item) = cx.tcx.hir_node_by_def_id(
325325
cx.tcx
326326
.hir()
327327
.get_parent_item(cx.tcx.local_def_id_to_hir_id(def_id))
@@ -366,9 +366,9 @@ impl<'tcx> LateLintPass<'tcx> for Types {
366366
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx ImplItem<'tcx>) {
367367
match item.kind {
368368
ImplItemKind::Const(ty, _) => {
369-
let is_in_trait_impl = if let Some(hir::Node::Item(item)) = cx
369+
let is_in_trait_impl = if let hir::Node::Item(item) = cx
370370
.tcx
371-
.opt_hir_node_by_def_id(cx.tcx.hir().get_parent_item(item.hir_id()).def_id)
371+
.hir_node_by_def_id(cx.tcx.hir().get_parent_item(item.hir_id()).def_id)
372372
{
373373
matches!(item.kind, ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }))
374374
} else {

clippy_lints/src/zero_sized_map_values.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl LateLintPass<'_> for ZeroSizedMapValues {
7474
fn in_trait_impl(cx: &LateContext<'_>, hir_id: HirId) -> bool {
7575
let parent_id = cx.tcx.hir().get_parent_item(hir_id);
7676
let second_parent_id = cx.tcx.hir().get_parent_item(parent_id.into()).def_id;
77-
if let Some(Node::Item(item)) = cx.tcx.opt_hir_node_by_def_id(second_parent_id) {
77+
if let Node::Item(item) = cx.tcx.hir_node_by_def_id(second_parent_id) {
7878
if let ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) = item.kind {
7979
return true;
8080
}

clippy_utils/src/lib.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,7 @@ pub fn is_trait_method(cx: &LateContext<'_>, expr: &Expr<'_>, diag_item: Symbol)
330330

331331
/// Checks if the `def_id` belongs to a function that is part of a trait impl.
332332
pub fn is_def_id_trait_method(cx: &LateContext<'_>, def_id: LocalDefId) -> bool {
333-
if let Some(hir_id) = cx.tcx.opt_local_def_id_to_hir_id(def_id)
334-
&& let Node::Item(item) = cx.tcx.parent_hir_node(hir_id)
333+
if let Node::Item(item) = cx.tcx.parent_hir_node(cx.tcx.local_def_id_to_hir_id(def_id))
335334
&& let ItemKind::Impl(imp) = item.kind
336335
{
337336
imp.of_trait.is_some()
@@ -574,12 +573,12 @@ fn local_item_children_by_name(tcx: TyCtxt<'_>, local_id: LocalDefId, name: Symb
574573
let hir = tcx.hir();
575574

576575
let root_mod;
577-
let item_kind = match tcx.opt_hir_node_by_def_id(local_id) {
578-
Some(Node::Crate(r#mod)) => {
576+
let item_kind = match tcx.hir_node_by_def_id(local_id) {
577+
Node::Crate(r#mod) => {
579578
root_mod = ItemKind::Mod(r#mod);
580579
&root_mod
581580
},
582-
Some(Node::Item(item)) => &item.kind,
581+
Node::Item(item) => &item.kind,
583582
_ => return Vec::new(),
584583
};
585584

@@ -1254,12 +1253,10 @@ pub fn is_in_panic_handler(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
12541253
/// Gets the name of the item the expression is in, if available.
12551254
pub fn get_item_name(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<Symbol> {
12561255
let parent_id = cx.tcx.hir().get_parent_item(expr.hir_id).def_id;
1257-
match cx.tcx.opt_hir_node_by_def_id(parent_id) {
1258-
Some(
1259-
Node::Item(Item { ident, .. })
1260-
| Node::TraitItem(TraitItem { ident, .. })
1261-
| Node::ImplItem(ImplItem { ident, .. }),
1262-
) => Some(ident.name),
1256+
match cx.tcx.hir_node_by_def_id(parent_id) {
1257+
Node::Item(Item { ident, .. })
1258+
| Node::TraitItem(TraitItem { ident, .. })
1259+
| Node::ImplItem(ImplItem { ident, .. }) => Some(ident.name),
12631260
_ => None,
12641261
}
12651262
}
@@ -2667,11 +2664,10 @@ impl<'tcx> ExprUseNode<'tcx> {
26672664
.and(Binder::dummy(cx.tcx.type_of(id).instantiate_identity())),
26682665
)),
26692666
Self::Return(id) => {
2670-
let hir_id = cx.tcx.local_def_id_to_hir_id(id.def_id);
26712667
if let Node::Expr(Expr {
26722668
kind: ExprKind::Closure(c),
26732669
..
2674-
}) = cx.tcx.hir_node(hir_id)
2670+
}) = cx.tcx.hir_node_by_def_id(id.def_id)
26752671
{
26762672
match c.fn_decl.output {
26772673
FnRetTy::DefaultReturn(_) => None,

0 commit comments

Comments
 (0)