Change to span_lint_and_sugg from span_lint_and_help

This commit is contained in:
Yoshitomo Nakanishi 2021-02-16 18:07:09 +09:00
parent 1f8ee3cc19
commit 9b0c1ebc18
2 changed files with 38 additions and 76 deletions

View file

@ -1,4 +1,5 @@
use rustc_ast::ast::{LitFloatType, LitIntType, LitKind};
use rustc_errors::Applicability;
use rustc_hir::{
intravisit::{walk_expr, walk_stmt, NestedVisitorMap, Visitor},
Body, Expr, ExprKind, HirId, Lit, Stmt, StmtKind,
@ -12,7 +13,7 @@ use rustc_session::{declare_lint_pass, declare_tool_lint};
use if_chain::if_chain;
use crate::utils::span_lint_and_help;
use crate::utils::{snippet, span_lint_and_sugg};
declare_clippy_lint! {
/// **What it does:** Checks for usage of unconstrained numeric literals which may cause default numeric fallback in type
@ -75,16 +76,24 @@ impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> {
if let Some(ty_bound) = self.ty_bounds.last();
if matches!(lit.node,
LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed));
if matches!(lit_ty.kind(), ty::Int(IntTy::I32) | ty::Float(FloatTy::F64));
if !ty_bound.is_integral();
then {
span_lint_and_help(
let suffix = match lit_ty.kind() {
ty::Int(IntTy::I32) => "i32",
ty::Float(FloatTy::F64) => "f64",
// Default numeric fallback never results in other types.
_ => return,
};
let sugg = format!("{}_{}", snippet(self.cx, lit.span, ""), suffix);
span_lint_and_sugg(
self.cx,
DEFAULT_NUMERIC_FALLBACK,
lit.span,
"default numeric fallback might occur",
None,
"consider adding suffix to avoid default numeric fallback",
"consider adding suffix",
sugg,
Applicability::MaybeIncorrect,
);
}
}

View file

@ -2,194 +2,147 @@ error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:10:17
|
LL | let x = 22;
| ^^
| ^^ help: consider adding suffix: `22_i32`
|
= note: `-D clippy::default-numeric-fallback` implied by `-D warnings`
= help: consider adding suffix to avoid default numeric fallback
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:11:18
|
LL | let x = [1, 2, 3];
| ^
|
= help: consider adding suffix to avoid default numeric fallback
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:11:21
|
LL | let x = [1, 2, 3];
| ^
|
= help: consider adding suffix to avoid default numeric fallback
| ^ help: consider adding suffix: `2_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:11:24
|
LL | let x = [1, 2, 3];
| ^
|
= help: consider adding suffix to avoid default numeric fallback
| ^ help: consider adding suffix: `3_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:12:28
|
LL | let x = if true { (1, 2) } else { (3, 4) };
| ^
|
= help: consider adding suffix to avoid default numeric fallback
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:12:31
|
LL | let x = if true { (1, 2) } else { (3, 4) };
| ^
|
= help: consider adding suffix to avoid default numeric fallback
| ^ help: consider adding suffix: `2_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:12:44
|
LL | let x = if true { (1, 2) } else { (3, 4) };
| ^
|
= help: consider adding suffix to avoid default numeric fallback
| ^ help: consider adding suffix: `3_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:12:47
|
LL | let x = if true { (1, 2) } else { (3, 4) };
| ^
|
= help: consider adding suffix to avoid default numeric fallback
| ^ help: consider adding suffix: `4_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:13:23
|
LL | let x = match 1 {
| ^
|
= help: consider adding suffix to avoid default numeric fallback
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:14:13
|
LL | 1 => 1,
| ^
|
= help: consider adding suffix to avoid default numeric fallback
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:14:18
|
LL | 1 => 1,
| ^
|
= help: consider adding suffix to avoid default numeric fallback
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:15:18
|
LL | _ => 2,
| ^
|
= help: consider adding suffix to avoid default numeric fallback
| ^ help: consider adding suffix: `2_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:19:17
|
LL | let x = 0.12;
| ^^^^
|
= help: consider adding suffix to avoid default numeric fallback
| ^^^^ help: consider adding suffix: `0.12_f64`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:37:21
|
LL | let y = 1;
| ^
|
= help: consider adding suffix to avoid default numeric fallback
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:45:21
|
LL | let y = 1;
| ^
|
= help: consider adding suffix to avoid default numeric fallback
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:51:21
|
LL | let y = 1;
| ^
|
= help: consider adding suffix to avoid default numeric fallback
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:63:9
|
LL | 1
| ^
|
= help: consider adding suffix to avoid default numeric fallback
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:69:27
|
LL | let f = || -> _ { 1 };
| ^
|
= help: consider adding suffix to avoid default numeric fallback
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:73:29
|
LL | let f = || -> i32 { 1 };
| ^
|
= help: consider adding suffix to avoid default numeric fallback
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:87:21
|
LL | generic_arg(1);
| ^
|
= help: consider adding suffix to avoid default numeric fallback
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:90:32
|
LL | let x: _ = generic_arg(1);
| ^
|
= help: consider adding suffix to avoid default numeric fallback
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:108:28
|
LL | GenericStruct { x: 1 };
| ^
|
= help: consider adding suffix to avoid default numeric fallback
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:111:36
|
LL | let _ = GenericStruct { x: 1 };
| ^
|
= help: consider adding suffix to avoid default numeric fallback
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:131:23
|
LL | s.generic_arg(1);
| ^
|
= help: consider adding suffix to avoid default numeric fallback
| ^ help: consider adding suffix: `1_i32`
error: aborting due to 24 previous errors