Skip to content

Commit 3bc5abe

Browse files
committed
default_numeric_fallback: Fix FP with floating literal
1 parent 28e7699 commit 3bc5abe

File tree

3 files changed

+63
-12
lines changed

3 files changed

+63
-12
lines changed

clippy_lints/src/default_numeric_fallback.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> {
7878
if let Some(ty_bound) = self.ty_bounds.last();
7979
if matches!(lit.node,
8080
LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed));
81-
if !ty_bound.is_integral();
81+
if !ty_bound.is_numeric();
8282
then {
8383
let suffix = match lit_ty.kind() {
8484
ty::Int(IntTy::I32) => "i32",
@@ -219,10 +219,10 @@ enum TyBound<'tcx> {
219219
}
220220

221221
impl<'tcx> TyBound<'tcx> {
222-
fn is_integral(self) -> bool {
222+
fn is_numeric(self) -> bool {
223223
match self {
224224
TyBound::Any => true,
225-
TyBound::Ty(t) => t.is_integral(),
225+
TyBound::Ty(t) => t.is_numeric(),
226226
TyBound::Nothing => false,
227227
}
228228
}

tests/ui/default_numeric_fallback.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,25 @@ mod function_def {
8181
}
8282

8383
mod function_calls {
84-
fn concrete_arg(x: i32) {}
84+
fn concrete_arg_i32(x: i32) {}
85+
86+
fn concrete_arg_f64(f: f64) {}
8587

8688
fn generic_arg<T>(t: T) {}
8789

8890
fn test() {
8991
// Should NOT lint this because the argument type is bound to a concrete type.
90-
concrete_arg(1);
92+
concrete_arg_i32(1);
93+
94+
// Should NOT lint this because the argument type is bound to a concrete type.
95+
concrete_arg_f64(1.);
9196

9297
// Should lint this because the argument type is inferred to `i32` and NOT bound to a concrete type.
9398
generic_arg(1);
9499

100+
// Should lint this because the argument type is inferred to `f32` and NOT bound to a concrete type.
101+
generic_arg(1.0);
102+
95103
// Should lint this because the argument type is inferred to `i32` and NOT bound to a concrete type.
96104
let x: _ = generic_arg(1);
97105
}
@@ -118,6 +126,31 @@ mod struct_ctor {
118126
}
119127
}
120128

129+
mod enum_ctor {
130+
enum ConcreteEnum {
131+
X(i32),
132+
Y(f64),
133+
}
134+
135+
enum GenericEnum<T> {
136+
X(T),
137+
}
138+
139+
fn test() {
140+
// Should NOT lint this because the field type is bound to a concrete type.
141+
ConcreteEnum::X(1);
142+
143+
// Should NOT lint this because the field type is bound to a concrete type.
144+
ConcreteEnum::Y(1.);
145+
146+
// Should lint this because the field type is inferred to `i32` and NOT bound to a concrete type.
147+
GenericEnum::X(1);
148+
149+
// Should lint this because the field type is inferred to `f64` and NOT bound to a concrete type.
150+
GenericEnum::X(1.);
151+
}
152+
}
153+
121154
mod method_calls {
122155
struct StructForMethodCallTest {}
123156

tests/ui/default_numeric_fallback.stderr

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,37 +115,55 @@ LL | let f = || -> i32 { 1 };
115115
| ^ help: consider adding suffix: `1_i32`
116116

117117
error: default numeric fallback might occur
118-
--> $DIR/default_numeric_fallback.rs:93:21
118+
--> $DIR/default_numeric_fallback.rs:98:21
119119
|
120120
LL | generic_arg(1);
121121
| ^ help: consider adding suffix: `1_i32`
122122

123123
error: default numeric fallback might occur
124-
--> $DIR/default_numeric_fallback.rs:96:32
124+
--> $DIR/default_numeric_fallback.rs:101:21
125+
|
126+
LL | generic_arg(1.0);
127+
| ^^^ help: consider adding suffix: `1.0_f64`
128+
129+
error: default numeric fallback might occur
130+
--> $DIR/default_numeric_fallback.rs:104:32
125131
|
126132
LL | let x: _ = generic_arg(1);
127133
| ^ help: consider adding suffix: `1_i32`
128134

129135
error: default numeric fallback might occur
130-
--> $DIR/default_numeric_fallback.rs:114:28
136+
--> $DIR/default_numeric_fallback.rs:122:28
131137
|
132138
LL | GenericStruct { x: 1 };
133139
| ^ help: consider adding suffix: `1_i32`
134140

135141
error: default numeric fallback might occur
136-
--> $DIR/default_numeric_fallback.rs:117:36
142+
--> $DIR/default_numeric_fallback.rs:125:36
137143
|
138144
LL | let _ = GenericStruct { x: 1 };
139145
| ^ help: consider adding suffix: `1_i32`
140146

141147
error: default numeric fallback might occur
142-
--> $DIR/default_numeric_fallback.rs:137:23
148+
--> $DIR/default_numeric_fallback.rs:147:24
149+
|
150+
LL | GenericEnum::X(1);
151+
| ^ help: consider adding suffix: `1_i32`
152+
153+
error: default numeric fallback might occur
154+
--> $DIR/default_numeric_fallback.rs:150:24
155+
|
156+
LL | GenericEnum::X(1.);
157+
| ^^ help: consider adding suffix: `1._f64`
158+
159+
error: default numeric fallback might occur
160+
--> $DIR/default_numeric_fallback.rs:170:23
143161
|
144162
LL | s.generic_arg(1);
145163
| ^ help: consider adding suffix: `1_i32`
146164

147165
error: default numeric fallback might occur
148-
--> $DIR/default_numeric_fallback.rs:144:21
166+
--> $DIR/default_numeric_fallback.rs:177:21
149167
|
150168
LL | let x = 22;
151169
| ^^ help: consider adding suffix: `22_i32`
@@ -155,5 +173,5 @@ LL | internal_macro!();
155173
|
156174
= note: this error originates in the macro `internal_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
157175

158-
error: aborting due to 25 previous errors
176+
error: aborting due to 28 previous errors
159177

0 commit comments

Comments
 (0)