Skip to content

Commit

Permalink
default_numeric_fallback: Fix FP with floating literal
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Nak committed Jul 8, 2021
1 parent 28e7699 commit 3bc5abe
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 12 deletions.
6 changes: 3 additions & 3 deletions clippy_lints/src/default_numeric_fallback.rs
Expand Up @@ -78,7 +78,7 @@ 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 !ty_bound.is_integral();
if !ty_bound.is_numeric();
then {
let suffix = match lit_ty.kind() {
ty::Int(IntTy::I32) => "i32",
Expand Down Expand Up @@ -219,10 +219,10 @@ enum TyBound<'tcx> {
}

impl<'tcx> TyBound<'tcx> {
fn is_integral(self) -> bool {
fn is_numeric(self) -> bool {
match self {
TyBound::Any => true,
TyBound::Ty(t) => t.is_integral(),
TyBound::Ty(t) => t.is_numeric(),
TyBound::Nothing => false,
}
}
Expand Down
37 changes: 35 additions & 2 deletions tests/ui/default_numeric_fallback.rs
Expand Up @@ -81,17 +81,25 @@ mod function_def {
}

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 test() {
// 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.
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.
let x: _ = generic_arg(1);
}
Expand All @@ -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 {
struct StructForMethodCallTest {}

Expand Down
32 changes: 25 additions & 7 deletions tests/ui/default_numeric_fallback.stderr
Expand Up @@ -115,37 +115,55 @@ LL | let f = || -> i32 { 1 };
| ^ help: consider adding suffix: `1_i32`

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

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);
| ^ help: consider adding suffix: `1_i32`

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

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 };
| ^ help: consider adding suffix: `1_i32`

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);
| ^ help: consider adding suffix: `1_i32`

error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:144:21
--> $DIR/default_numeric_fallback.rs:177:21
|
LL | let x = 22;
| ^^ help: consider adding suffix: `22_i32`
Expand All @@ -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)

error: aborting due to 25 previous errors
error: aborting due to 28 previous errors

0 comments on commit 3bc5abe

Please sign in to comment.