Skip to content

Commit 0198ac7

Browse files
committed
Add fn_sig_opt to get fn signature from HirId
1 parent 93796b2 commit 0198ac7

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

clippy_lints/src/default_numeric_fallback.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use rustc_ast::ast::{LitFloatType, LitIntType, LitKind};
22
use rustc_hir::{
33
intravisit::{walk_expr, walk_stmt, NestedVisitorMap, Visitor},
4-
Body, Expr, ExprKind, Lit, Stmt, StmtKind,
4+
Body, Expr, ExprKind, HirId, Lit, Stmt, StmtKind,
55
};
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_middle::{
88
hir::map::Map,
9-
ty::{self, FloatTy, IntTy, Ty},
9+
ty::{self, FloatTy, IntTy, PolyFnSig, Ty},
1010
};
1111
use rustc_session::{declare_lint_pass, declare_tool_lint};
1212

@@ -64,15 +64,15 @@ struct NumericFallbackVisitor<'a, 'tcx> {
6464
impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> {
6565
fn new(cx: &'a LateContext<'tcx>) -> Self {
6666
Self {
67-
ty_bounds: vec![TyBound::Nothing],
67+
ty_bounds: Vec::new(),
6868
cx,
6969
}
7070
}
7171

7272
/// Check whether a passed literal has potential to cause fallback or not.
7373
fn check_lit(&self, lit: &Lit, lit_ty: Ty<'tcx>) {
74-
let ty_bound = self.ty_bounds.last().unwrap();
7574
if_chain! {
75+
if let Some(ty_bound) = self.ty_bounds.last();
7676
if matches!(lit.node,
7777
LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed));
7878
if matches!(lit_ty.kind(), ty::Int(IntTy::I32) | ty::Float(FloatTy::F64));
@@ -98,19 +98,14 @@ impl<'a, 'tcx> Visitor<'tcx> for NumericFallbackVisitor<'a, 'tcx> {
9898
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
9999
match &expr.kind {
100100
ExprKind::Call(func, args) => {
101-
if_chain! {
102-
if let ExprKind::Path(ref func_path) = func.kind;
103-
if let Some(def_id) = self.cx.qpath_res(func_path, func.hir_id).opt_def_id();
104-
then {
105-
let fn_sig = self.cx.tcx.fn_sig(def_id).skip_binder();
106-
for (expr, bound) in args.iter().zip(fn_sig.inputs().iter()) {
107-
// Push found arg type, then visit arg.
108-
self.ty_bounds.push(TyBound::Ty(bound));
109-
self.visit_expr(expr);
110-
self.ty_bounds.pop();
111-
}
112-
return;
101+
if let Some(fn_sig) = fn_sig_opt(self.cx, func.hir_id) {
102+
for (expr, bound) in args.iter().zip(fn_sig.skip_binder().inputs().iter()) {
103+
// Push found arg type, then visit arg.
104+
self.ty_bounds.push(TyBound::Ty(bound));
105+
self.visit_expr(expr);
106+
self.ty_bounds.pop();
113107
}
108+
return;
114109
}
115110
},
116111

@@ -160,6 +155,16 @@ impl<'a, 'tcx> Visitor<'tcx> for NumericFallbackVisitor<'a, 'tcx> {
160155
}
161156
}
162157

158+
fn fn_sig_opt<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId) -> Option<PolyFnSig<'tcx>> {
159+
let node_ty = cx.typeck_results().node_type_opt(hir_id)?;
160+
// We can't use `TyS::fn_sig` because it automatically performs substs, this may result in FNs.
161+
match node_ty.kind() {
162+
ty::FnDef(def_id, _) => Some(cx.tcx.fn_sig(*def_id)),
163+
ty::FnPtr(fn_sig) => Some(*fn_sig),
164+
_ => None,
165+
}
166+
}
167+
163168
#[derive(Debug, Clone, Copy)]
164169
enum TyBound<'ctx> {
165170
Any,

0 commit comments

Comments
 (0)