default_numeric_fallback do not lint on constants

This commit is contained in:
kraktus 2022-10-12 12:04:41 +02:00
parent 2d588175ca
commit e51e9308d5
7 changed files with 93 additions and 40 deletions

View file

@ -1,12 +1,12 @@
use clippy_utils::diagnostics::span_lint_hir_and_then; use clippy_utils::diagnostics::span_lint_hir_and_then;
use clippy_utils::numeric_literal;
use clippy_utils::source::snippet_opt; use clippy_utils::source::snippet_opt;
use clippy_utils::{get_parent_node, numeric_literal};
use if_chain::if_chain; use if_chain::if_chain;
use rustc_ast::ast::{LitFloatType, LitIntType, LitKind}; use rustc_ast::ast::{LitFloatType, LitIntType, LitKind};
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::{ use rustc_hir::{
intravisit::{walk_expr, walk_stmt, Visitor}, intravisit::{walk_expr, walk_stmt, Visitor},
Body, Expr, ExprKind, HirId, Lit, Stmt, StmtKind, Body, Expr, ExprKind, HirId, ItemKind, Lit, Node, Stmt, StmtKind,
}; };
use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::{ use rustc_middle::{
@ -55,7 +55,12 @@ declare_lint_pass!(DefaultNumericFallback => [DEFAULT_NUMERIC_FALLBACK]);
impl<'tcx> LateLintPass<'tcx> for DefaultNumericFallback { impl<'tcx> LateLintPass<'tcx> for DefaultNumericFallback {
fn check_body(&mut self, cx: &LateContext<'tcx>, body: &'tcx Body<'_>) { fn check_body(&mut self, cx: &LateContext<'tcx>, body: &'tcx Body<'_>) {
let mut visitor = NumericFallbackVisitor::new(cx); let is_parent_const = if let Some(Node::Item(item)) = get_parent_node(cx.tcx, body.id().hir_id) {
matches!(item.kind, ItemKind::Const(..))
} else {
false
};
let mut visitor = NumericFallbackVisitor::new(cx, is_parent_const);
visitor.visit_body(body); visitor.visit_body(body);
} }
} }
@ -68,9 +73,13 @@ struct NumericFallbackVisitor<'a, 'tcx> {
} }
impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> { impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> {
fn new(cx: &'a LateContext<'tcx>) -> Self { fn new(cx: &'a LateContext<'tcx>, is_parent_const: bool) -> Self {
Self { Self {
ty_bounds: vec![TyBound::Nothing], ty_bounds: vec![if is_parent_const {
TyBound::Any
} else {
TyBound::Nothing
}],
cx, cx,
} }
} }
@ -192,13 +201,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NumericFallbackVisitor<'a, 'tcx> {
fn visit_stmt(&mut self, stmt: &'tcx Stmt<'_>) { fn visit_stmt(&mut self, stmt: &'tcx Stmt<'_>) {
match stmt.kind { match stmt.kind {
StmtKind::Local(local) => { StmtKind::Local(local) if local.ty.is_some() => self.ty_bounds.push(TyBound::Any),
if local.ty.is_some() {
self.ty_bounds.push(TyBound::Any);
} else {
self.ty_bounds.push(TyBound::Nothing);
}
},
_ => self.ty_bounds.push(TyBound::Nothing), _ => self.ty_bounds.push(TyBound::Nothing),
} }

View file

@ -33,6 +33,7 @@ mod basic_expr {
let x: [f64; 3] = [1., 2., 3.]; let x: [f64; 3] = [1., 2., 3.];
let x: (f64, f64) = if true { (1., 2.) } else { (3., 4.) }; let x: (f64, f64) = if true { (1., 2.) } else { (3., 4.) };
let x: _ = 1.; let x: _ = 1.;
const X: f32 = 1.;
} }
} }
@ -59,6 +60,14 @@ mod nested_local {
// Should NOT lint this because this literal is bound to `_` of outer `Local`. // Should NOT lint this because this literal is bound to `_` of outer `Local`.
2. 2.
}; };
const X: f32 = {
// Should lint this because this literal is not bound to any types.
let y = 1.0_f64;
// Should NOT lint this because this literal is bound to `_` of outer `Local`.
1.
};
} }
} }

View file

@ -33,6 +33,7 @@ mod basic_expr {
let x: [f64; 3] = [1., 2., 3.]; let x: [f64; 3] = [1., 2., 3.];
let x: (f64, f64) = if true { (1., 2.) } else { (3., 4.) }; let x: (f64, f64) = if true { (1., 2.) } else { (3., 4.) };
let x: _ = 1.; let x: _ = 1.;
const X: f32 = 1.;
} }
} }
@ -59,6 +60,14 @@ mod nested_local {
// Should NOT lint this because this literal is bound to `_` of outer `Local`. // Should NOT lint this because this literal is bound to `_` of outer `Local`.
2. 2.
}; };
const X: f32 = {
// Should lint this because this literal is not bound to any types.
let y = 1.;
// Should NOT lint this because this literal is bound to `_` of outer `Local`.
1.
};
} }
} }

View file

@ -61,79 +61,85 @@ LL | _ => 1.,
| ^^ help: consider adding suffix: `1.0_f64` | ^^ help: consider adding suffix: `1.0_f64`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:43:21 --> $DIR/default_numeric_fallback_f64.rs:44:21
| |
LL | let y = 1.; LL | let y = 1.;
| ^^ help: consider adding suffix: `1.0_f64` | ^^ help: consider adding suffix: `1.0_f64`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:51:21 --> $DIR/default_numeric_fallback_f64.rs:52:21
| |
LL | let y = 1.; LL | let y = 1.;
| ^^ help: consider adding suffix: `1.0_f64` | ^^ help: consider adding suffix: `1.0_f64`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:57:21 --> $DIR/default_numeric_fallback_f64.rs:58:21
| |
LL | let y = 1.; LL | let y = 1.;
| ^^ help: consider adding suffix: `1.0_f64` | ^^ help: consider adding suffix: `1.0_f64`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:69:9 --> $DIR/default_numeric_fallback_f64.rs:66:21
|
LL | let y = 1.;
| ^^ help: consider adding suffix: `1.0_f64`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:78:9
| |
LL | 1. LL | 1.
| ^^ help: consider adding suffix: `1.0_f64` | ^^ help: consider adding suffix: `1.0_f64`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:75:27 --> $DIR/default_numeric_fallback_f64.rs:84:27
| |
LL | let f = || -> _ { 1. }; LL | let f = || -> _ { 1. };
| ^^ help: consider adding suffix: `1.0_f64` | ^^ help: consider adding suffix: `1.0_f64`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:79:29 --> $DIR/default_numeric_fallback_f64.rs:88:29
| |
LL | let f = || -> f64 { 1. }; LL | let f = || -> f64 { 1. };
| ^^ help: consider adding suffix: `1.0_f64` | ^^ help: consider adding suffix: `1.0_f64`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:93:21 --> $DIR/default_numeric_fallback_f64.rs:102:21
| |
LL | generic_arg(1.); LL | generic_arg(1.);
| ^^ help: consider adding suffix: `1.0_f64` | ^^ help: consider adding suffix: `1.0_f64`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:96:32 --> $DIR/default_numeric_fallback_f64.rs:105:32
| |
LL | let x: _ = generic_arg(1.); LL | let x: _ = generic_arg(1.);
| ^^ help: consider adding suffix: `1.0_f64` | ^^ help: consider adding suffix: `1.0_f64`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:114:28 --> $DIR/default_numeric_fallback_f64.rs:123:28
| |
LL | GenericStruct { x: 1. }; LL | GenericStruct { x: 1. };
| ^^ help: consider adding suffix: `1.0_f64` | ^^ help: consider adding suffix: `1.0_f64`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:117:36 --> $DIR/default_numeric_fallback_f64.rs:126:36
| |
LL | let _ = GenericStruct { x: 1. }; LL | let _ = GenericStruct { x: 1. };
| ^^ help: consider adding suffix: `1.0_f64` | ^^ help: consider adding suffix: `1.0_f64`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:135:24 --> $DIR/default_numeric_fallback_f64.rs:144:24
| |
LL | GenericEnum::X(1.); LL | GenericEnum::X(1.);
| ^^ help: consider adding suffix: `1.0_f64` | ^^ help: consider adding suffix: `1.0_f64`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:155:23 --> $DIR/default_numeric_fallback_f64.rs:164:23
| |
LL | s.generic_arg(1.); LL | s.generic_arg(1.);
| ^^ help: consider adding suffix: `1.0_f64` | ^^ help: consider adding suffix: `1.0_f64`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_f64.rs:162:21 --> $DIR/default_numeric_fallback_f64.rs:171:21
| |
LL | let x = 22.; LL | let x = 22.;
| ^^^ help: consider adding suffix: `22.0_f64` | ^^^ help: consider adding suffix: `22.0_f64`
@ -143,5 +149,5 @@ LL | internal_macro!();
| |
= note: this error originates in the macro `internal_macro` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `internal_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 23 previous errors error: aborting due to 24 previous errors

View file

@ -33,6 +33,8 @@ mod basic_expr {
let x: [i32; 3] = [1, 2, 3]; let x: [i32; 3] = [1, 2, 3];
let x: (i32, i32) = if true { (1, 2) } else { (3, 4) }; let x: (i32, i32) = if true { (1, 2) } else { (3, 4) };
let x: _ = 1; let x: _ = 1;
let x: u64 = 1;
const CONST_X: i8 = 1;
} }
} }
@ -59,6 +61,14 @@ mod nested_local {
// Should NOT lint this because this literal is bound to `_` of outer `Local`. // Should NOT lint this because this literal is bound to `_` of outer `Local`.
2 2
}; };
const CONST_X: i32 = {
// Should lint this because this literal is not bound to any types.
let y = 1_i32;
// Should NOT lint this because this literal is bound to `_` of outer `Local`.
1
};
} }
} }

View file

@ -33,6 +33,8 @@ mod basic_expr {
let x: [i32; 3] = [1, 2, 3]; let x: [i32; 3] = [1, 2, 3];
let x: (i32, i32) = if true { (1, 2) } else { (3, 4) }; let x: (i32, i32) = if true { (1, 2) } else { (3, 4) };
let x: _ = 1; let x: _ = 1;
let x: u64 = 1;
const CONST_X: i8 = 1;
} }
} }
@ -59,6 +61,14 @@ mod nested_local {
// Should NOT lint this because this literal is bound to `_` of outer `Local`. // Should NOT lint this because this literal is bound to `_` of outer `Local`.
2 2
}; };
const CONST_X: i32 = {
// Should lint this because this literal is not bound to any types.
let y = 1;
// Should NOT lint this because this literal is bound to `_` of outer `Local`.
1
};
} }
} }

View file

@ -73,79 +73,85 @@ LL | _ => 2,
| ^ help: consider adding suffix: `2_i32` | ^ help: consider adding suffix: `2_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:43:21 --> $DIR/default_numeric_fallback_i32.rs:45:21
| |
LL | let y = 1; LL | let y = 1;
| ^ help: consider adding suffix: `1_i32` | ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:51:21 --> $DIR/default_numeric_fallback_i32.rs:53:21
| |
LL | let y = 1; LL | let y = 1;
| ^ help: consider adding suffix: `1_i32` | ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:57:21 --> $DIR/default_numeric_fallback_i32.rs:59:21
| |
LL | let y = 1; LL | let y = 1;
| ^ help: consider adding suffix: `1_i32` | ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:69:9 --> $DIR/default_numeric_fallback_i32.rs:67:21
|
LL | let y = 1;
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:79:9
| |
LL | 1 LL | 1
| ^ help: consider adding suffix: `1_i32` | ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:75:27 --> $DIR/default_numeric_fallback_i32.rs:85:27
| |
LL | let f = || -> _ { 1 }; LL | let f = || -> _ { 1 };
| ^ help: consider adding suffix: `1_i32` | ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:79:29 --> $DIR/default_numeric_fallback_i32.rs:89:29
| |
LL | let f = || -> i32 { 1 }; LL | let f = || -> i32 { 1 };
| ^ help: consider adding suffix: `1_i32` | ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:93:21 --> $DIR/default_numeric_fallback_i32.rs:103:21
| |
LL | generic_arg(1); LL | generic_arg(1);
| ^ help: consider adding suffix: `1_i32` | ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:96:32 --> $DIR/default_numeric_fallback_i32.rs:106:32
| |
LL | let x: _ = generic_arg(1); LL | let x: _ = generic_arg(1);
| ^ help: consider adding suffix: `1_i32` | ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:114:28 --> $DIR/default_numeric_fallback_i32.rs:124:28
| |
LL | GenericStruct { x: 1 }; LL | GenericStruct { x: 1 };
| ^ help: consider adding suffix: `1_i32` | ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:117:36 --> $DIR/default_numeric_fallback_i32.rs:127:36
| |
LL | let _ = GenericStruct { x: 1 }; LL | let _ = GenericStruct { x: 1 };
| ^ help: consider adding suffix: `1_i32` | ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:135:24 --> $DIR/default_numeric_fallback_i32.rs:145:24
| |
LL | GenericEnum::X(1); LL | GenericEnum::X(1);
| ^ help: consider adding suffix: `1_i32` | ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:155:23 --> $DIR/default_numeric_fallback_i32.rs:165:23
| |
LL | s.generic_arg(1); LL | s.generic_arg(1);
| ^ help: consider adding suffix: `1_i32` | ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:162:21 --> $DIR/default_numeric_fallback_i32.rs:172:21
| |
LL | let x = 22; LL | let x = 22;
| ^^ help: consider adding suffix: `22_i32` | ^^ help: consider adding suffix: `22_i32`
@ -155,5 +161,5 @@ LL | internal_macro!();
| |
= note: this error originates in the macro `internal_macro` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `internal_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 25 previous errors error: aborting due to 26 previous errors