-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use rustc_ast::ast::{LitFloatType, LitIntType, LitKind}; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_middle::ty; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
use crate::utils::span_lint_and_help; | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** Checks for usage of unconstrained numeric literals which may cause default numeric fallback in type | ||
/// 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. | ||
/// | ||
/// **Why is this bad?** For those who are very careful about types, default numeric fallback | ||
/// may be a pitfall that cause unexpected runtime behavior. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// ```rust | ||
/// let i = 10; | ||
/// let f = 1.23; | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```rust | ||
/// let i = 10i32; | ||
/// let f = 1.23f64; | ||
/// ``` | ||
pub DEFAULT_NUMERIC_FALLBACK, | ||
restriction, | ||
"usage of unconstrained numeric literals which may cause default numeric fallback." | ||
} | ||
|
||
declare_lint_pass!(DefaultNumericFallback => [DEFAULT_NUMERIC_FALLBACK]); | ||
|
||
impl LateLintPass<'_> for DefaultNumericFallback { | ||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { | ||
let lit = match &expr.kind { | ||
ExprKind::Lit(lit) => lit, | ||
_ => return, | ||
}; | ||
|
||
match (&lit.node, cx.typeck_results().expr_ty(expr).kind()) { | ||
(LitKind::Int(_, LitIntType::Unsuffixed), ty::Int(ty::IntTy::I32)) | ||
| (LitKind::Float(_, LitFloatType::Unsuffixed), ty::Float(ty::FloatTy::F64)) => span_lint_and_help( | ||
cx, | ||
DEFAULT_NUMERIC_FALLBACK, | ||
lit.span, | ||
"default numeric fallback may occured", | ||
None, | ||
"Consider adding suffix to avoid default numeric fallback", | ||
), | ||
_ => {}, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#![warn(clippy::default_numeric_fallback)] | ||
#![allow(unused)] | ||
|
||
fn foo_mono(x: u32) -> u32 { | ||
x | ||
} | ||
|
||
fn foo_poly<T>(t: T) -> T { | ||
t | ||
} | ||
|
||
fn main() { | ||
let i = 23; | ||
let f = 1.23; | ||
foo_poly(10); | ||
for _ in 0..10 {} | ||
|
||
let x = 23i32; | ||
let f = 1.23f64; | ||
for i in 0..10 { | ||
let _ = 10usize + i; | ||
} | ||
foo_mono(10); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
error: default numeric fallback may occured | ||
--> $DIR/default_numeric_fallback.rs:13:13 | ||
| | ||
LL | let i = 23; | ||
| ^^ | ||
| | ||
= note: `-D clippy::default-numeric-fallback` implied by `-D warnings` | ||
= help: Consider adding suffix to avoid default numeric fallback | ||
|
||
error: default numeric fallback may occured | ||
--> $DIR/default_numeric_fallback.rs:14:13 | ||
| | ||
LL | let f = 1.23; | ||
| ^^^^ | ||
| | ||
= help: Consider adding suffix to avoid default numeric fallback | ||
|
||
error: default numeric fallback may occured | ||
--> $DIR/default_numeric_fallback.rs:15:14 | ||
| | ||
LL | foo_poly(10); | ||
| ^^ | ||
| | ||
= help: Consider adding suffix to avoid default numeric fallback | ||
|
||
error: default numeric fallback may occured | ||
--> $DIR/default_numeric_fallback.rs:16:14 | ||
| | ||
LL | for _ in 0..10 {} | ||
| ^ | ||
| | ||
= help: Consider adding suffix to avoid default numeric fallback | ||
|
||
error: default numeric fallback may occured | ||
--> $DIR/default_numeric_fallback.rs:16:17 | ||
| | ||
LL | for _ in 0..10 {} | ||
| ^^ | ||
| | ||
= help: Consider adding suffix to avoid default numeric fallback | ||
|
||
error: aborting due to 5 previous errors | ||
|