default_numeric_fallback: Fix FP with floating literal

This commit is contained in:
Yoshitomo Nakanishi
2021-07-08 11:37:12 +09:00
parent 28e769951f
commit 3bc5abef62
3 changed files with 63 additions and 12 deletions

View File

@@ -78,7 +78,7 @@ impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> {
if let Some(ty_bound) = self.ty_bounds.last(); if let Some(ty_bound) = self.ty_bounds.last();
if matches!(lit.node, if matches!(lit.node,
LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed)); LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed));
if !ty_bound.is_integral(); if !ty_bound.is_numeric();
then { then {
let suffix = match lit_ty.kind() { let suffix = match lit_ty.kind() {
ty::Int(IntTy::I32) => "i32", ty::Int(IntTy::I32) => "i32",
@@ -219,10 +219,10 @@ enum TyBound<'tcx> {
} }
impl<'tcx> TyBound<'tcx> { impl<'tcx> TyBound<'tcx> {
fn is_integral(self) -> bool { fn is_numeric(self) -> bool {
match self { match self {
TyBound::Any => true, TyBound::Any => true,
TyBound::Ty(t) => t.is_integral(), TyBound::Ty(t) => t.is_numeric(),
TyBound::Nothing => false, TyBound::Nothing => false,
} }
} }

View File

@@ -81,17 +81,25 @@ mod function_def {
} }
mod function_calls { mod function_calls {
fn concrete_arg(x: i32) {} fn concrete_arg_i32(x: i32) {}
fn concrete_arg_f64(f: f64) {}
fn generic_arg<T>(t: T) {} fn generic_arg<T>(t: T) {}
fn test() { fn test() {
// Should NOT lint this because the argument type is bound to a concrete type. // Should NOT lint this because the argument type is bound to a concrete type.
concrete_arg(1); concrete_arg_i32(1);
// Should NOT lint this because the argument type is bound to a concrete type.
concrete_arg_f64(1.);
// Should lint this because the argument type is inferred to `i32` and NOT bound to a concrete type. // Should lint this because the argument type is inferred to `i32` and NOT bound to a concrete type.
generic_arg(1); generic_arg(1);
// Should lint this because the argument type is inferred to `f32` and NOT bound to a concrete type.
generic_arg(1.0);
// Should lint this because the argument type is inferred to `i32` and NOT bound to a concrete type. // Should lint this because the argument type is inferred to `i32` and NOT bound to a concrete type.
let x: _ = generic_arg(1); let x: _ = generic_arg(1);
} }
@@ -118,6 +126,31 @@ mod struct_ctor {
} }
} }
mod enum_ctor {
enum ConcreteEnum {
X(i32),
Y(f64),
}
enum GenericEnum<T> {
X(T),
}
fn test() {
// Should NOT lint this because the field type is bound to a concrete type.
ConcreteEnum::X(1);
// Should NOT lint this because the field type is bound to a concrete type.
ConcreteEnum::Y(1.);
// Should lint this because the field type is inferred to `i32` and NOT bound to a concrete type.
GenericEnum::X(1);
// Should lint this because the field type is inferred to `f64` and NOT bound to a concrete type.
GenericEnum::X(1.);
}
}
mod method_calls { mod method_calls {
struct StructForMethodCallTest {} struct StructForMethodCallTest {}

View File

@@ -115,37 +115,55 @@ 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.rs:93:21 --> $DIR/default_numeric_fallback.rs:98: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.rs:96:32 --> $DIR/default_numeric_fallback.rs:101:21
|
LL | generic_arg(1.0);
| ^^^ help: consider adding suffix: `1.0_f64`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:104: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.rs:114:28 --> $DIR/default_numeric_fallback.rs:122: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.rs:117:36 --> $DIR/default_numeric_fallback.rs:125: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.rs:137:23 --> $DIR/default_numeric_fallback.rs:147:24
|
LL | GenericEnum::X(1);
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:150:24
|
LL | GenericEnum::X(1.);
| ^^ help: consider adding suffix: `1._f64`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:170: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.rs:144:21 --> $DIR/default_numeric_fallback.rs:177:21
| |
LL | let x = 22; LL | let x = 22;
| ^^ help: consider adding suffix: `22_i32` | ^^ help: consider adding suffix: `22_i32`
@@ -155,5 +173,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 28 previous errors