Skip to content

Commit e51e930

Browse files
committed
default_numeric_fallback do not lint on constants
1 parent 2d58817 commit e51e930

7 files changed

+93
-40
lines changed

clippy_lints/src/default_numeric_fallback.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use clippy_utils::diagnostics::span_lint_hir_and_then;
2-
use clippy_utils::numeric_literal;
32
use clippy_utils::source::snippet_opt;
3+
use clippy_utils::{get_parent_node, numeric_literal};
44
use if_chain::if_chain;
55
use rustc_ast::ast::{LitFloatType, LitIntType, LitKind};
66
use rustc_errors::Applicability;
77
use rustc_hir::{
88
intravisit::{walk_expr, walk_stmt, Visitor},
9-
Body, Expr, ExprKind, HirId, Lit, Stmt, StmtKind,
9+
Body, Expr, ExprKind, HirId, ItemKind, Lit, Node, Stmt, StmtKind,
1010
};
1111
use rustc_lint::{LateContext, LateLintPass, LintContext};
1212
use rustc_middle::{
@@ -55,7 +55,12 @@ declare_lint_pass!(DefaultNumericFallback => [DEFAULT_NUMERIC_FALLBACK]);
5555

5656
impl<'tcx> LateLintPass<'tcx> for DefaultNumericFallback {
5757
fn check_body(&mut self, cx: &LateContext<'tcx>, body: &'tcx Body<'_>) {
58-
let mut visitor = NumericFallbackVisitor::new(cx);
58+
let is_parent_const = if let Some(Node::Item(item)) = get_parent_node(cx.tcx, body.id().hir_id) {
59+
matches!(item.kind, ItemKind::Const(..))
60+
} else {
61+
false
62+
};
63+
let mut visitor = NumericFallbackVisitor::new(cx, is_parent_const);
5964
visitor.visit_body(body);
6065
}
6166
}
@@ -68,9 +73,13 @@ struct NumericFallbackVisitor<'a, 'tcx> {
6873
}
6974

7075
impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> {
71-
fn new(cx: &'a LateContext<'tcx>) -> Self {
76+
fn new(cx: &'a LateContext<'tcx>, is_parent_const: bool) -> Self {
7277
Self {
73-
ty_bounds: vec![TyBound::Nothing],
78+
ty_bounds: vec![if is_parent_const {
79+
TyBound::Any
80+
} else {
81+
TyBound::Nothing
82+
}],
7483
cx,
7584
}
7685
}
@@ -192,13 +201,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NumericFallbackVisitor<'a, 'tcx> {
192201

193202
fn visit_stmt(&mut self, stmt: &'tcx Stmt<'_>) {
194203
match stmt.kind {
195-
StmtKind::Local(local) => {
196-
if local.ty.is_some() {
197-
self.ty_bounds.push(TyBound::Any);
198-
} else {
199-
self.ty_bounds.push(TyBound::Nothing);
200-
}
201-
},
204+
StmtKind::Local(local) if local.ty.is_some() => self.ty_bounds.push(TyBound::Any),
202205

203206
_ => self.ty_bounds.push(TyBound::Nothing),
204207
}

tests/ui/default_numeric_fallback_f64.fixed

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ mod basic_expr {
3333
let x: [f64; 3] = [1., 2., 3.];
3434
let x: (f64, f64) = if true { (1., 2.) } else { (3., 4.) };
3535
let x: _ = 1.;
36+
const X: f32 = 1.;
3637
}
3738
}
3839

@@ -59,6 +60,14 @@ mod nested_local {
5960
// Should NOT lint this because this literal is bound to `_` of outer `Local`.
6061
2.
6162
};
63+
64+
const X: f32 = {
65+
// Should lint this because this literal is not bound to any types.
66+
let y = 1.0_f64;
67+
68+
// Should NOT lint this because this literal is bound to `_` of outer `Local`.
69+
1.
70+
};
6271
}
6372
}
6473

tests/ui/default_numeric_fallback_f64.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ mod basic_expr {
3333
let x: [f64; 3] = [1., 2., 3.];
3434
let x: (f64, f64) = if true { (1., 2.) } else { (3., 4.) };
3535
let x: _ = 1.;
36+
const X: f32 = 1.;
3637
}
3738
}
3839

@@ -59,6 +60,14 @@ mod nested_local {
5960
// Should NOT lint this because this literal is bound to `_` of outer `Local`.
6061
2.
6162
};
63+
64+
const X: f32 = {
65+
// Should lint this because this literal is not bound to any types.
66+
let y = 1.;
67+
68+
// Should NOT lint this because this literal is bound to `_` of outer `Local`.
69+
1.
70+
};
6271
}
6372
}
6473

tests/ui/default_numeric_fallback_f64.stderr

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,79 +61,85 @@ LL | _ => 1.,
6161
| ^^ help: consider adding suffix: `1.0_f64`
6262

6363
error: default numeric fallback might occur
64-
--> $DIR/default_numeric_fallback_f64.rs:43:21
64+
--> $DIR/default_numeric_fallback_f64.rs:44:21
6565
|
6666
LL | let y = 1.;
6767
| ^^ help: consider adding suffix: `1.0_f64`
6868

6969
error: default numeric fallback might occur
70-
--> $DIR/default_numeric_fallback_f64.rs:51:21
70+
--> $DIR/default_numeric_fallback_f64.rs:52:21
7171
|
7272
LL | let y = 1.;
7373
| ^^ help: consider adding suffix: `1.0_f64`
7474

7575
error: default numeric fallback might occur
76-
--> $DIR/default_numeric_fallback_f64.rs:57:21
76+
--> $DIR/default_numeric_fallback_f64.rs:58:21
7777
|
7878
LL | let y = 1.;
7979
| ^^ help: consider adding suffix: `1.0_f64`
8080

8181
error: default numeric fallback might occur
82-
--> $DIR/default_numeric_fallback_f64.rs:69:9
82+
--> $DIR/default_numeric_fallback_f64.rs:66:21
83+
|
84+
LL | let y = 1.;
85+
| ^^ help: consider adding suffix: `1.0_f64`
86+
87+
error: default numeric fallback might occur
88+
--> $DIR/default_numeric_fallback_f64.rs:78:9
8389
|
8490
LL | 1.
8591
| ^^ help: consider adding suffix: `1.0_f64`
8692

8793
error: default numeric fallback might occur
88-
--> $DIR/default_numeric_fallback_f64.rs:75:27
94+
--> $DIR/default_numeric_fallback_f64.rs:84:27
8995
|
9096
LL | let f = || -> _ { 1. };
9197
| ^^ help: consider adding suffix: `1.0_f64`
9298

9399
error: default numeric fallback might occur
94-
--> $DIR/default_numeric_fallback_f64.rs:79:29
100+
--> $DIR/default_numeric_fallback_f64.rs:88:29
95101
|
96102
LL | let f = || -> f64 { 1. };
97103
| ^^ help: consider adding suffix: `1.0_f64`
98104

99105
error: default numeric fallback might occur
100-
--> $DIR/default_numeric_fallback_f64.rs:93:21
106+
--> $DIR/default_numeric_fallback_f64.rs:102:21
101107
|
102108
LL | generic_arg(1.);
103109
| ^^ help: consider adding suffix: `1.0_f64`
104110

105111
error: default numeric fallback might occur
106-
--> $DIR/default_numeric_fallback_f64.rs:96:32
112+
--> $DIR/default_numeric_fallback_f64.rs:105:32
107113
|
108114
LL | let x: _ = generic_arg(1.);
109115
| ^^ help: consider adding suffix: `1.0_f64`
110116

111117
error: default numeric fallback might occur
112-
--> $DIR/default_numeric_fallback_f64.rs:114:28
118+
--> $DIR/default_numeric_fallback_f64.rs:123:28
113119
|
114120
LL | GenericStruct { x: 1. };
115121
| ^^ help: consider adding suffix: `1.0_f64`
116122

117123
error: default numeric fallback might occur
118-
--> $DIR/default_numeric_fallback_f64.rs:117:36
124+
--> $DIR/default_numeric_fallback_f64.rs:126:36
119125
|
120126
LL | let _ = GenericStruct { x: 1. };
121127
| ^^ help: consider adding suffix: `1.0_f64`
122128

123129
error: default numeric fallback might occur
124-
--> $DIR/default_numeric_fallback_f64.rs:135:24
130+
--> $DIR/default_numeric_fallback_f64.rs:144:24
125131
|
126132
LL | GenericEnum::X(1.);
127133
| ^^ help: consider adding suffix: `1.0_f64`
128134

129135
error: default numeric fallback might occur
130-
--> $DIR/default_numeric_fallback_f64.rs:155:23
136+
--> $DIR/default_numeric_fallback_f64.rs:164:23
131137
|
132138
LL | s.generic_arg(1.);
133139
| ^^ help: consider adding suffix: `1.0_f64`
134140

135141
error: default numeric fallback might occur
136-
--> $DIR/default_numeric_fallback_f64.rs:162:21
142+
--> $DIR/default_numeric_fallback_f64.rs:171:21
137143
|
138144
LL | let x = 22.;
139145
| ^^^ help: consider adding suffix: `22.0_f64`
@@ -143,5 +149,5 @@ LL | internal_macro!();
143149
|
144150
= note: this error originates in the macro `internal_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
145151

146-
error: aborting due to 23 previous errors
152+
error: aborting due to 24 previous errors
147153

tests/ui/default_numeric_fallback_i32.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ mod basic_expr {
3333
let x: [i32; 3] = [1, 2, 3];
3434
let x: (i32, i32) = if true { (1, 2) } else { (3, 4) };
3535
let x: _ = 1;
36+
let x: u64 = 1;
37+
const CONST_X: i8 = 1;
3638
}
3739
}
3840

@@ -59,6 +61,14 @@ mod nested_local {
5961
// Should NOT lint this because this literal is bound to `_` of outer `Local`.
6062
2
6163
};
64+
65+
const CONST_X: i32 = {
66+
// Should lint this because this literal is not bound to any types.
67+
let y = 1_i32;
68+
69+
// Should NOT lint this because this literal is bound to `_` of outer `Local`.
70+
1
71+
};
6272
}
6373
}
6474

tests/ui/default_numeric_fallback_i32.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ mod basic_expr {
3333
let x: [i32; 3] = [1, 2, 3];
3434
let x: (i32, i32) = if true { (1, 2) } else { (3, 4) };
3535
let x: _ = 1;
36+
let x: u64 = 1;
37+
const CONST_X: i8 = 1;
3638
}
3739
}
3840

@@ -59,6 +61,14 @@ mod nested_local {
5961
// Should NOT lint this because this literal is bound to `_` of outer `Local`.
6062
2
6163
};
64+
65+
const CONST_X: i32 = {
66+
// Should lint this because this literal is not bound to any types.
67+
let y = 1;
68+
69+
// Should NOT lint this because this literal is bound to `_` of outer `Local`.
70+
1
71+
};
6272
}
6373
}
6474

tests/ui/default_numeric_fallback_i32.stderr

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,79 +73,85 @@ LL | _ => 2,
7373
| ^ help: consider adding suffix: `2_i32`
7474

7575
error: default numeric fallback might occur
76-
--> $DIR/default_numeric_fallback_i32.rs:43:21
76+
--> $DIR/default_numeric_fallback_i32.rs:45:21
7777
|
7878
LL | let y = 1;
7979
| ^ help: consider adding suffix: `1_i32`
8080

8181
error: default numeric fallback might occur
82-
--> $DIR/default_numeric_fallback_i32.rs:51:21
82+
--> $DIR/default_numeric_fallback_i32.rs:53:21
8383
|
8484
LL | let y = 1;
8585
| ^ help: consider adding suffix: `1_i32`
8686

8787
error: default numeric fallback might occur
88-
--> $DIR/default_numeric_fallback_i32.rs:57:21
88+
--> $DIR/default_numeric_fallback_i32.rs:59:21
8989
|
9090
LL | let y = 1;
9191
| ^ help: consider adding suffix: `1_i32`
9292

9393
error: default numeric fallback might occur
94-
--> $DIR/default_numeric_fallback_i32.rs:69:9
94+
--> $DIR/default_numeric_fallback_i32.rs:67:21
95+
|
96+
LL | let y = 1;
97+
| ^ help: consider adding suffix: `1_i32`
98+
99+
error: default numeric fallback might occur
100+
--> $DIR/default_numeric_fallback_i32.rs:79:9
95101
|
96102
LL | 1
97103
| ^ help: consider adding suffix: `1_i32`
98104

99105
error: default numeric fallback might occur
100-
--> $DIR/default_numeric_fallback_i32.rs:75:27
106+
--> $DIR/default_numeric_fallback_i32.rs:85:27
101107
|
102108
LL | let f = || -> _ { 1 };
103109
| ^ help: consider adding suffix: `1_i32`
104110

105111
error: default numeric fallback might occur
106-
--> $DIR/default_numeric_fallback_i32.rs:79:29
112+
--> $DIR/default_numeric_fallback_i32.rs:89:29
107113
|
108114
LL | let f = || -> i32 { 1 };
109115
| ^ help: consider adding suffix: `1_i32`
110116

111117
error: default numeric fallback might occur
112-
--> $DIR/default_numeric_fallback_i32.rs:93:21
118+
--> $DIR/default_numeric_fallback_i32.rs:103:21
113119
|
114120
LL | generic_arg(1);
115121
| ^ help: consider adding suffix: `1_i32`
116122

117123
error: default numeric fallback might occur
118-
--> $DIR/default_numeric_fallback_i32.rs:96:32
124+
--> $DIR/default_numeric_fallback_i32.rs:106:32
119125
|
120126
LL | let x: _ = generic_arg(1);
121127
| ^ help: consider adding suffix: `1_i32`
122128

123129
error: default numeric fallback might occur
124-
--> $DIR/default_numeric_fallback_i32.rs:114:28
130+
--> $DIR/default_numeric_fallback_i32.rs:124:28
125131
|
126132
LL | GenericStruct { x: 1 };
127133
| ^ help: consider adding suffix: `1_i32`
128134

129135
error: default numeric fallback might occur
130-
--> $DIR/default_numeric_fallback_i32.rs:117:36
136+
--> $DIR/default_numeric_fallback_i32.rs:127:36
131137
|
132138
LL | let _ = GenericStruct { x: 1 };
133139
| ^ help: consider adding suffix: `1_i32`
134140

135141
error: default numeric fallback might occur
136-
--> $DIR/default_numeric_fallback_i32.rs:135:24
142+
--> $DIR/default_numeric_fallback_i32.rs:145:24
137143
|
138144
LL | GenericEnum::X(1);
139145
| ^ help: consider adding suffix: `1_i32`
140146

141147
error: default numeric fallback might occur
142-
--> $DIR/default_numeric_fallback_i32.rs:155:23
148+
--> $DIR/default_numeric_fallback_i32.rs:165:23
143149
|
144150
LL | s.generic_arg(1);
145151
| ^ help: consider adding suffix: `1_i32`
146152

147153
error: default numeric fallback might occur
148-
--> $DIR/default_numeric_fallback_i32.rs:162:21
154+
--> $DIR/default_numeric_fallback_i32.rs:172:21
149155
|
150156
LL | let x = 22;
151157
| ^^ help: consider adding suffix: `22_i32`
@@ -155,5 +161,5 @@ LL | internal_macro!();
155161
|
156162
= note: this error originates in the macro `internal_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
157163

158-
error: aborting due to 25 previous errors
164+
error: aborting due to 26 previous errors
159165

0 commit comments

Comments
 (0)