2022-06-09 07:17:46 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_hir_and_then;
|
2021-07-15 08:44:10 +00:00
|
|
|
use clippy_utils::numeric_literal;
|
|
|
|
use clippy_utils::source::snippet_opt;
|
2021-03-25 18:29:11 +00:00
|
|
|
use if_chain::if_chain;
|
2021-02-25 10:25:22 +00:00
|
|
|
use rustc_ast::ast::{LitFloatType, LitIntType, LitKind};
|
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir::{
|
2022-01-15 22:07:52 +00:00
|
|
|
intravisit::{walk_expr, walk_stmt, Visitor},
|
2021-02-25 10:25:22 +00:00
|
|
|
Body, Expr, ExprKind, HirId, Lit, Stmt, StmtKind,
|
|
|
|
};
|
2021-07-01 16:17:38 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
2021-02-25 10:25:22 +00:00
|
|
|
use rustc_middle::{
|
2021-07-01 16:17:38 +00:00
|
|
|
lint::in_external_macro,
|
2021-02-25 10:25:22 +00:00
|
|
|
ty::{self, FloatTy, IntTy, PolyFnSig, Ty},
|
|
|
|
};
|
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2021-03-08 23:57:44 +00:00
|
|
|
use std::iter;
|
2021-02-25 10:25:22 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for usage of unconstrained numeric literals which may cause default numeric fallback in type
|
2021-02-25 10:25:22 +00:00
|
|
|
/// inference.
|
|
|
|
///
|
|
|
|
/// Default numeric fallback means that if numeric types have not yet been bound to concrete
|
|
|
|
/// types at the end of type inference, then integer type is bound to `i32`, and similarly
|
|
|
|
/// floating type is bound to `f64`.
|
|
|
|
///
|
|
|
|
/// See [RFC0212](https://github.com/rust-lang/rfcs/blob/master/text/0212-restore-int-fallback.md) for more information about the fallback.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// For those who are very careful about types, default numeric fallback
|
2021-02-25 10:25:22 +00:00
|
|
|
/// can be a pitfall that cause unexpected runtime behavior.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Known problems
|
|
|
|
/// This lint can only be allowed at the function level or above.
|
2021-02-25 10:25:22 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2021-02-25 10:25:22 +00:00
|
|
|
/// ```rust
|
|
|
|
/// let i = 10;
|
|
|
|
/// let f = 1.23;
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
|
|
|
/// let i = 10i32;
|
|
|
|
/// let f = 1.23f64;
|
|
|
|
/// ```
|
2021-12-06 11:33:31 +00:00
|
|
|
#[clippy::version = "1.52.0"]
|
2021-02-25 10:25:22 +00:00
|
|
|
pub DEFAULT_NUMERIC_FALLBACK,
|
|
|
|
restriction,
|
|
|
|
"usage of unconstrained numeric literals which may cause default numeric fallback."
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(DefaultNumericFallback => [DEFAULT_NUMERIC_FALLBACK]);
|
|
|
|
|
2022-01-13 12:18:19 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for DefaultNumericFallback {
|
2021-02-25 10:25:22 +00:00
|
|
|
fn check_body(&mut self, cx: &LateContext<'tcx>, body: &'tcx Body<'_>) {
|
|
|
|
let mut visitor = NumericFallbackVisitor::new(cx);
|
|
|
|
visitor.visit_body(body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct NumericFallbackVisitor<'a, 'tcx> {
|
|
|
|
/// Stack manages type bound of exprs. The top element holds current expr type.
|
|
|
|
ty_bounds: Vec<TyBound<'tcx>>,
|
|
|
|
|
|
|
|
cx: &'a LateContext<'tcx>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> {
|
|
|
|
fn new(cx: &'a LateContext<'tcx>) -> Self {
|
|
|
|
Self {
|
|
|
|
ty_bounds: vec![TyBound::Nothing],
|
|
|
|
cx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check whether a passed literal has potential to cause fallback or not.
|
2022-06-09 07:17:46 +00:00
|
|
|
fn check_lit(&self, lit: &Lit, lit_ty: Ty<'tcx>, emit_hir_id: HirId) {
|
2021-02-25 10:25:22 +00:00
|
|
|
if_chain! {
|
2021-07-01 16:17:38 +00:00
|
|
|
if !in_external_macro(self.cx.sess(), lit.span);
|
2021-02-25 10:25:22 +00:00
|
|
|
if let Some(ty_bound) = self.ty_bounds.last();
|
|
|
|
if matches!(lit.node,
|
|
|
|
LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed));
|
2021-07-15 08:44:10 +00:00
|
|
|
if !ty_bound.is_numeric();
|
2021-02-25 10:25:22 +00:00
|
|
|
then {
|
2021-07-15 08:44:10 +00:00
|
|
|
let (suffix, is_float) = match lit_ty.kind() {
|
|
|
|
ty::Int(IntTy::I32) => ("i32", false),
|
|
|
|
ty::Float(FloatTy::F64) => ("f64", true),
|
2021-02-25 10:25:22 +00:00
|
|
|
// Default numeric fallback never results in other types.
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
2021-07-15 08:44:10 +00:00
|
|
|
let src = if let Some(src) = snippet_opt(self.cx, lit.span) {
|
|
|
|
src
|
|
|
|
} else {
|
|
|
|
match lit.node {
|
|
|
|
LitKind::Int(src, _) => format!("{}", src),
|
|
|
|
LitKind::Float(src, _) => format!("{}", src),
|
|
|
|
_ => return,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let sugg = numeric_literal::format(&src, Some(suffix), is_float);
|
2022-06-09 07:17:46 +00:00
|
|
|
span_lint_hir_and_then(
|
2021-02-25 10:25:22 +00:00
|
|
|
self.cx,
|
|
|
|
DEFAULT_NUMERIC_FALLBACK,
|
2022-06-09 07:17:46 +00:00
|
|
|
emit_hir_id,
|
2021-02-25 10:25:22 +00:00
|
|
|
lit.span,
|
|
|
|
"default numeric fallback might occur",
|
2022-06-09 07:17:46 +00:00
|
|
|
|diag| {
|
|
|
|
diag.span_suggestion(lit.span, "consider adding suffix", sugg, Applicability::MaybeIncorrect);
|
|
|
|
}
|
2021-02-25 10:25:22 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for NumericFallbackVisitor<'a, 'tcx> {
|
|
|
|
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
|
|
|
|
match &expr.kind {
|
|
|
|
ExprKind::Call(func, args) => {
|
|
|
|
if let Some(fn_sig) = fn_sig_opt(self.cx, func.hir_id) {
|
2021-03-08 23:57:44 +00:00
|
|
|
for (expr, bound) in iter::zip(*args, fn_sig.skip_binder().inputs()) {
|
2021-02-25 10:25:22 +00:00
|
|
|
// Push found arg type, then visit arg.
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 03:13:38 +00:00
|
|
|
self.ty_bounds.push(TyBound::Ty(*bound));
|
2021-02-25 10:25:22 +00:00
|
|
|
self.visit_expr(expr);
|
|
|
|
self.ty_bounds.pop();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-12-01 17:17:50 +00:00
|
|
|
ExprKind::MethodCall(_, args, _) => {
|
2021-02-25 10:25:22 +00:00
|
|
|
if let Some(def_id) = self.cx.typeck_results().type_dependent_def_id(expr.hir_id) {
|
|
|
|
let fn_sig = self.cx.tcx.fn_sig(def_id).skip_binder();
|
2021-03-08 23:57:44 +00:00
|
|
|
for (expr, bound) in iter::zip(*args, fn_sig.inputs()) {
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 03:13:38 +00:00
|
|
|
self.ty_bounds.push(TyBound::Ty(*bound));
|
2021-02-25 10:25:22 +00:00
|
|
|
self.visit_expr(expr);
|
|
|
|
self.ty_bounds.pop();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-03-12 14:30:50 +00:00
|
|
|
ExprKind::Struct(_, fields, base) => {
|
2021-04-08 15:50:13 +00:00
|
|
|
let ty = self.cx.typeck_results().expr_ty(expr);
|
2021-02-25 10:25:22 +00:00
|
|
|
if_chain! {
|
|
|
|
if let Some(adt_def) = ty.ty_adt_def();
|
|
|
|
if adt_def.is_struct();
|
2022-03-04 20:28:41 +00:00
|
|
|
if let Some(variant) = adt_def.variants().iter().next();
|
2021-02-25 10:25:22 +00:00
|
|
|
then {
|
|
|
|
let fields_def = &variant.fields;
|
|
|
|
|
|
|
|
// Push field type then visit each field expr.
|
|
|
|
for field in fields.iter() {
|
|
|
|
let bound =
|
|
|
|
fields_def
|
|
|
|
.iter()
|
|
|
|
.find_map(|f_def| {
|
2022-01-03 03:37:05 +00:00
|
|
|
if f_def.ident(self.cx.tcx) == field.ident
|
2021-02-25 10:25:22 +00:00
|
|
|
{ Some(self.cx.tcx.type_of(f_def.did)) }
|
|
|
|
else { None }
|
|
|
|
});
|
|
|
|
self.ty_bounds.push(bound.into());
|
|
|
|
self.visit_expr(field.expr);
|
|
|
|
self.ty_bounds.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Visit base with no bound.
|
|
|
|
if let Some(base) = base {
|
|
|
|
self.ty_bounds.push(TyBound::Nothing);
|
|
|
|
self.visit_expr(base);
|
|
|
|
self.ty_bounds.pop();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
ExprKind::Lit(lit) => {
|
|
|
|
let ty = self.cx.typeck_results().expr_ty(expr);
|
2022-06-09 07:17:46 +00:00
|
|
|
self.check_lit(lit, ty, expr.hir_id);
|
2021-02-25 10:25:22 +00:00
|
|
|
return;
|
|
|
|
},
|
|
|
|
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
walk_expr(self, expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_stmt(&mut self, stmt: &'tcx Stmt<'_>) {
|
|
|
|
match stmt.kind {
|
|
|
|
StmtKind::Local(local) => {
|
|
|
|
if local.ty.is_some() {
|
2021-06-03 06:41:37 +00:00
|
|
|
self.ty_bounds.push(TyBound::Any);
|
2021-02-25 10:25:22 +00:00
|
|
|
} else {
|
2021-06-03 06:41:37 +00:00
|
|
|
self.ty_bounds.push(TyBound::Nothing);
|
2021-02-25 10:25:22 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_ => self.ty_bounds.push(TyBound::Nothing),
|
|
|
|
}
|
|
|
|
|
|
|
|
walk_stmt(self, stmt);
|
|
|
|
self.ty_bounds.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fn_sig_opt<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId) -> Option<PolyFnSig<'tcx>> {
|
|
|
|
let node_ty = cx.typeck_results().node_type_opt(hir_id)?;
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 03:13:38 +00:00
|
|
|
// We can't use `Ty::fn_sig` because it automatically performs substs, this may result in FNs.
|
2021-02-25 10:25:22 +00:00
|
|
|
match node_ty.kind() {
|
|
|
|
ty::FnDef(def_id, _) => Some(cx.tcx.fn_sig(*def_id)),
|
|
|
|
ty::FnPtr(fn_sig) => Some(*fn_sig),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
enum TyBound<'tcx> {
|
|
|
|
Any,
|
|
|
|
Ty(Ty<'tcx>),
|
|
|
|
Nothing,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> TyBound<'tcx> {
|
2021-07-15 08:44:10 +00:00
|
|
|
fn is_numeric(self) -> bool {
|
2021-02-25 10:25:22 +00:00
|
|
|
match self {
|
|
|
|
TyBound::Any => true,
|
2021-07-15 08:44:10 +00:00
|
|
|
TyBound::Ty(t) => t.is_numeric(),
|
2021-02-25 10:25:22 +00:00
|
|
|
TyBound::Nothing => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> From<Option<Ty<'tcx>>> for TyBound<'tcx> {
|
|
|
|
fn from(v: Option<Ty<'tcx>>) -> Self {
|
|
|
|
match v {
|
|
|
|
Some(t) => TyBound::Ty(t),
|
|
|
|
None => TyBound::Nothing,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|