From 3bc5abef6219f15929ffb6ff8ddedd998f013c63 Mon Sep 17 00:00:00 2001 From: Yoshitomo Nakanishi Date: Thu, 8 Jul 2021 11:37:12 +0900 Subject: [PATCH] default_numeric_fallback: Fix FP with floating literal --- clippy_lints/src/default_numeric_fallback.rs | 6 ++-- tests/ui/default_numeric_fallback.rs | 37 ++++++++++++++++++-- tests/ui/default_numeric_fallback.stderr | 32 +++++++++++++---- 3 files changed, 63 insertions(+), 12 deletions(-) diff --git a/clippy_lints/src/default_numeric_fallback.rs b/clippy_lints/src/default_numeric_fallback.rs index a125376bffa9..ea28644b9a49 100644 --- a/clippy_lints/src/default_numeric_fallback.rs +++ b/clippy_lints/src/default_numeric_fallback.rs @@ -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", @@ -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, } } diff --git a/tests/ui/default_numeric_fallback.rs b/tests/ui/default_numeric_fallback.rs index c0625fd1b75e..58a7f377374d 100644 --- a/tests/ui/default_numeric_fallback.rs +++ b/tests/ui/default_numeric_fallback.rs @@ -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) {} 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); } @@ -118,6 +126,31 @@ mod struct_ctor { } } +mod enum_ctor { + enum ConcreteEnum { + X(i32), + Y(f64), + } + + enum GenericEnum { + 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 {} diff --git a/tests/ui/default_numeric_fallback.stderr b/tests/ui/default_numeric_fallback.stderr index 5862cd936ac1..3965385189e4 100644 --- a/tests/ui/default_numeric_fallback.stderr +++ b/tests/ui/default_numeric_fallback.stderr @@ -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` @@ -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