Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 8b8b0a9

Browse files
committed
Fix enclosing_body_owner_opt and rename it to enclosing_body_opt
1 parent e32e4de commit 8b8b0a9

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

clippy_lints/src/default_numeric_fallback.rs

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_ast::ast::{Label, LitFloatType, LitIntType, LitKind};
22
use rustc_hir::{
33
self as hir,
44
intravisit::{walk_expr, walk_stmt, walk_ty, FnKind, NestedVisitorMap, Visitor},
5-
Body, Expr, ExprKind, FnDecl, FnRetTy, Guard, HirId, Lit, Stmt, StmtKind,
5+
Body, BodyId, Expr, ExprKind, FnDecl, FnRetTy, Guard, HirId, Lit, Stmt, StmtKind,
66
};
77
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_middle::{
@@ -50,16 +50,31 @@ declare_clippy_lint! {
5050

5151
declare_lint_pass!(DefaultNumericFallback => [DEFAULT_NUMERIC_FALLBACK]);
5252

53-
fn enclosing_body_owner_opt(tcx: TyCtxt<'_>, hir_id: HirId) -> Option<HirId> {
53+
/// Return the body that includes passed `hir_id` if exists.
54+
fn enclosing_body_opt(tcx: TyCtxt<'_>, hir_id: HirId) -> Option<BodyId> {
5455
let hir_map = tcx.hir();
56+
let mut trace = vec![(hir_id)];
57+
5558
for (parent, _) in hir_map.parent_iter(hir_id) {
59+
trace.push(parent);
5660
if let Some(body) = hir_map.maybe_body_owned_by(parent) {
57-
return Some(hir_map.body_owner(body));
61+
if trace.iter().any(|hir_id| *hir_id == body.hir_id) {
62+
return Some(body);
63+
}
5864
}
5965
}
66+
6067
None
6168
}
6269

70+
fn ty_from_hir_ty<'tcx>(cx: &LateContext<'tcx>, hir_ty: &hir::Ty<'tcx>) -> Option<Ty<'tcx>> {
71+
if enclosing_body_opt(cx.tcx, hir_ty.hir_id).is_some() {
72+
cx.typeck_results().node_type_opt(hir_ty.hir_id)
73+
} else {
74+
Some(hir_ty_to_ty(cx.tcx, hir_ty))
75+
}
76+
}
77+
6378
impl LateLintPass<'_> for DefaultNumericFallback {
6479
fn check_fn(
6580
&mut self,
@@ -68,21 +83,17 @@ impl LateLintPass<'_> for DefaultNumericFallback {
6883
fn_decl: &'tcx FnDecl<'_>,
6984
body: &'tcx Body<'_>,
7085
_: Span,
71-
hir_id: HirId,
86+
_: HirId,
7287
) {
7388
let ret_ty_bound = match fn_decl.output {
7489
FnRetTy::DefaultReturn(_) => None,
7590
FnRetTy::Return(ty) => Some(ty),
7691
}
7792
.and_then(|ty| {
78-
let mut infer_ty_finder = InferTyFinder::new();
79-
infer_ty_finder.visit_ty(ty);
80-
if infer_ty_finder.found {
93+
if is_infer_included(ty) {
8194
None
82-
} else if enclosing_body_owner_opt(cx.tcx, hir_id).is_some() {
83-
cx.typeck_results().node_type_opt(ty.hir_id)
8495
} else {
85-
Some(hir_ty_to_ty(cx.tcx, ty))
96+
ty_from_hir_ty(cx, ty)
8697
}
8798
});
8899

@@ -114,7 +125,7 @@ impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> {
114125
}
115126
}
116127

117-
/// Check whether lit cause fallback or not.
128+
/// Check whether a passed literal has potential to cause fallback or not.
118129
fn check_lit(&self, lit: &Lit, lit_ty: Ty<'tcx>) {
119130
let ty_bound = self.ty_bounds.last().unwrap();
120131

@@ -334,12 +345,10 @@ impl<'a, 'tcx> Visitor<'tcx> for NumericFallbackVisitor<'a, 'tcx> {
334345
match stmt.kind {
335346
StmtKind::Local(local) => {
336347
let ty = local.ty.and_then(|hir_ty| {
337-
let mut infer_ty_finder = InferTyFinder::new();
338-
infer_ty_finder.visit_ty(hir_ty);
339-
if infer_ty_finder.found {
348+
if is_infer_included(hir_ty) {
340349
None
341350
} else {
342-
self.cx.typeck_results().node_type_opt(hir_ty.hir_id)
351+
ty_from_hir_ty(self.cx, hir_ty)
343352
}
344353
});
345354
self.ty_bounds.push(ty);
@@ -357,7 +366,13 @@ impl<'a, 'tcx> Visitor<'tcx> for NumericFallbackVisitor<'a, 'tcx> {
357366
}
358367
}
359368

360-
/// Find `hir::TyKind::Infer` is included in passed typed.
369+
/// Return true if a given ty includes `hir::TyKind::Infer`.
370+
fn is_infer_included(ty: &hir::Ty<'_>) -> bool {
371+
let mut infer_ty_finder = InferTyFinder::new();
372+
infer_ty_finder.visit_ty(ty);
373+
infer_ty_finder.found
374+
}
375+
361376
struct InferTyFinder {
362377
found: bool,
363378
}

0 commit comments

Comments
 (0)