about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYoshitomo Nakanishi <yurayura.rounin.3@gmail.com>2021-02-15 23:33:27 +0900
committerYoshitomo Nakanishi <yurayura.rounin.3@gmail.com>2021-02-15 23:33:27 +0900
commitfb91c76586a3a01fef6a8c49edb9e4598fbd138f (patch)
tree0ce0ff91916b72ee705d73de33b2e3610ce6853e
parent0198ac7bdd807abeb8f6b423c22b1e594fb0b4bf (diff)
downloadrust-fb91c76586a3a01fef6a8c49edb9e4598fbd138f.tar.gz
rust-fb91c76586a3a01fef6a8c49edb9e4598fbd138f.zip
Add more tests for default_numeric_fallback
-rw-r--r--clippy_lints/src/default_numeric_fallback.rs2
-rw-r--r--tests/ui/default_numeric_fallback.rs112
-rw-r--r--tests/ui/default_numeric_fallback.stderr118
3 files changed, 169 insertions, 63 deletions
diff --git a/clippy_lints/src/default_numeric_fallback.rs b/clippy_lints/src/default_numeric_fallback.rs
index b6730afa4af..d755112a178 100644
--- a/clippy_lints/src/default_numeric_fallback.rs
+++ b/clippy_lints/src/default_numeric_fallback.rs
@@ -27,7 +27,7 @@ declare_clippy_lint! {
     /// **Why is this bad?** For those who are very careful about types, default numeric fallback
     /// can be a pitfall that cause unexpected runtime behavior.
     ///
-    /// **Known problems:** None.
+    /// **Known problems:** This lint can only be allowed at the function level or above.
     ///
     /// **Example:**
     /// ```rust
diff --git a/tests/ui/default_numeric_fallback.rs b/tests/ui/default_numeric_fallback.rs
index 420c1f0c931..c4881094469 100644
--- a/tests/ui/default_numeric_fallback.rs
+++ b/tests/ui/default_numeric_fallback.rs
@@ -4,55 +4,97 @@
 #![allow(clippy::no_effect)]
 #![allow(clippy::unnecessary_operation)]
 
-fn concrete_arg(x: i32) {}
+mod basic_expr {
+    fn test() {
+        // Should lint unsuffixed literals typed `i32`.
+        let x = 22;
+        let x = [1, 2, 3];
+        let x = if true { (1, 2) } else { (3, 4) };
+        let x = match 1 {
+            1 => 1,
+            _ => 2,
+        };
 
-fn generic_arg<T>(t: T) {}
+        // Should lint unsuffixed literals typed `f64`.
+        let x = 0.12;
 
-struct ConcreteStruct {
-    x: i32,
+        // Should NOT lint suffixed literals.
+        let x = 22_i32;
+        let x = 0.12_f64;
+
+        // Should NOT lint literals in init expr if `Local` has a type annotation.
+        let x: f64 = 0.1;
+        let x: [i32; 3] = [1, 2, 3];
+        let x: (i32, i32) = if true { (1, 2) } else { (3, 4) };
+        let x: _ = 1;
+    }
 }
 
-struct StructForMethodCallTest {
-    x: i32,
+mod nested_local {
+    fn test() {
+        let x: _ = {
+            // Should lint this because this literal is not bound to any types.
+            let y = 1;
+
+            // Should NOT lint this because this literal is bound to `_` of outer `Local`.
+            1
+        };
+    }
 }
 
-impl StructForMethodCallTest {
-    fn concrete_arg(&self, x: i32) {}
+mod function_def {
+    fn ret_i32() -> i32 {
+        // Even though the output type is specified,
+        // this unsuffixed literal is linted to reduce heuristics and keep codebase simple.
+        23
+    }
 
-    fn generic_arg<T>(&self, t: T) {}
+    fn test() {
+        // Should lint this because return type is inferred to `i32` and NOT bound to a concrete
+        // type.
+        let f = || -> _ { 1 };
+
+        // Even though the output type is specified,
+        // this unsuffixed literal is linted to reduce heuristics and keep codebase simple.
+        let f = || -> i32 { 1 };
+    }
 }
 
-fn main() {
-    let s = StructForMethodCallTest { x: 10_i32 };
+mod function_calls {
+    fn concrete_arg(x: i32) {}
+
+    fn generic_arg<T>(t: T) {}
 
-    // Bad.
-    let x = 1;
-    let x = 0.1;
+    fn test() {
+        // Should NOT lint this because the argument type is bound to a concrete type.
+        concrete_arg(1);
 
-    let x = if true { 1 } else { 2 };
+        // Should lint this because the argument type is inferred to `i32` and NOT bound to a concrete type.
+        generic_arg(1);
 
-    let x: _ = {
-        let y = 1;
-        1
-    };
+        // Should lint this because the argument type is inferred to `i32` and NOT bound to a concrete type.
+        let x: _ = generic_arg(1);
+    }
+}
+
+mod method_calls {
+    struct StructForMethodCallTest {}
 
-    generic_arg(10);
-    s.generic_arg(10);
-    let x: _ = generic_arg(10);
-    let x: _ = s.generic_arg(10);
+    impl StructForMethodCallTest {
+        fn concrete_arg(&self, x: i32) {}
 
-    // Good.
-    let x = 1_i32;
-    let x: i32 = 1;
-    let x: _ = 1;
-    let x = 0.1_f64;
-    let x: f64 = 0.1;
-    let x: _ = 0.1;
+        fn generic_arg<T>(&self, t: T) {}
+    }
 
-    let x: _ = if true { 1 } else { 2 };
+    fn test() {
+        let s = StructForMethodCallTest {};
 
-    concrete_arg(10);
-    s.concrete_arg(10);
-    let x = concrete_arg(10);
-    let x = s.concrete_arg(10);
+        // Should NOT lint this because the argument type is bound to a concrete type.
+        s.concrete_arg(1);
+
+        // Should lint this because the argument type is bound to a concrete type.
+        s.generic_arg(1);
+    }
 }
+
+fn main() {}
diff --git a/tests/ui/default_numeric_fallback.stderr b/tests/ui/default_numeric_fallback.stderr
index cb7c174ad8d..c71d05d7993 100644
--- a/tests/ui/default_numeric_fallback.stderr
+++ b/tests/ui/default_numeric_fallback.stderr
@@ -1,75 +1,139 @@
 error: default numeric fallback might occur
-  --> $DIR/default_numeric_fallback.rs:29:13
+  --> $DIR/default_numeric_fallback.rs:10:17
    |
-LL |     let x = 1;
-   |             ^
+LL |         let x = 22;
+   |                 ^^
    |
    = note: `-D clippy::default-numeric-fallback` implied by `-D warnings`
    = help: consider adding suffix to avoid default numeric fallback
 
 error: default numeric fallback might occur
-  --> $DIR/default_numeric_fallback.rs:30:13
+  --> $DIR/default_numeric_fallback.rs:11:18
+   |
+LL |         let x = [1, 2, 3];
+   |                  ^
+   |
+   = help: consider adding suffix to avoid default numeric fallback
+
+error: default numeric fallback might occur
+  --> $DIR/default_numeric_fallback.rs:11:21
+   |
+LL |         let x = [1, 2, 3];
+   |                     ^
+   |
+   = help: consider adding suffix to avoid default numeric fallback
+
+error: default numeric fallback might occur
+  --> $DIR/default_numeric_fallback.rs:11:24
    |
-LL |     let x = 0.1;
-   |             ^^^
+LL |         let x = [1, 2, 3];
+   |                        ^
    |
    = help: consider adding suffix to avoid default numeric fallback
 
 error: default numeric fallback might occur
-  --> $DIR/default_numeric_fallback.rs:32:23
+  --> $DIR/default_numeric_fallback.rs:12:28
+   |
+LL |         let x = if true { (1, 2) } else { (3, 4) };
+   |                            ^
+   |
+   = help: consider adding suffix to avoid default numeric fallback
+
+error: default numeric fallback might occur
+  --> $DIR/default_numeric_fallback.rs:12:31
+   |
+LL |         let x = if true { (1, 2) } else { (3, 4) };
+   |                               ^
+   |
+   = help: consider adding suffix to avoid default numeric fallback
+
+error: default numeric fallback might occur
+  --> $DIR/default_numeric_fallback.rs:12:44
+   |
+LL |         let x = if true { (1, 2) } else { (3, 4) };
+   |                                            ^
    |
-LL |     let x = if true { 1 } else { 2 };
+   = help: consider adding suffix to avoid default numeric fallback
+
+error: default numeric fallback might occur
+  --> $DIR/default_numeric_fallback.rs:12:47
+   |
+LL |         let x = if true { (1, 2) } else { (3, 4) };
+   |                                               ^
+   |
+   = help: consider adding suffix to avoid default numeric fallback
+
+error: default numeric fallback might occur
+  --> $DIR/default_numeric_fallback.rs:13:23
+   |
+LL |         let x = match 1 {
    |                       ^
    |
    = help: consider adding suffix to avoid default numeric fallback
 
 error: default numeric fallback might occur
-  --> $DIR/default_numeric_fallback.rs:32:34
+  --> $DIR/default_numeric_fallback.rs:14:13
    |
-LL |     let x = if true { 1 } else { 2 };
-   |                                  ^
+LL |             1 => 1,
+   |             ^
    |
    = help: consider adding suffix to avoid default numeric fallback
 
 error: default numeric fallback might occur
-  --> $DIR/default_numeric_fallback.rs:35:17
+  --> $DIR/default_numeric_fallback.rs:14:18
    |
-LL |         let y = 1;
-   |                 ^
+LL |             1 => 1,
+   |                  ^
    |
    = help: consider adding suffix to avoid default numeric fallback
 
 error: default numeric fallback might occur
-  --> $DIR/default_numeric_fallback.rs:39:17
+  --> $DIR/default_numeric_fallback.rs:15:18
    |
-LL |     generic_arg(10);
-   |                 ^^
+LL |             _ => 2,
+   |                  ^
    |
    = help: consider adding suffix to avoid default numeric fallback
 
 error: default numeric fallback might occur
-  --> $DIR/default_numeric_fallback.rs:40:19
+  --> $DIR/default_numeric_fallback.rs:19:17
    |
-LL |     s.generic_arg(10);
-   |                   ^^
+LL |         let x = 0.12;
+   |                 ^^^^
    |
    = help: consider adding suffix to avoid default numeric fallback
 
 error: default numeric fallback might occur
-  --> $DIR/default_numeric_fallback.rs:41:28
+  --> $DIR/default_numeric_fallback.rs:37:21
    |
-LL |     let x: _ = generic_arg(10);
-   |                            ^^
+LL |             let y = 1;
+   |                     ^
    |
    = help: consider adding suffix to avoid default numeric fallback
 
 error: default numeric fallback might occur
-  --> $DIR/default_numeric_fallback.rs:42:30
+  --> $DIR/default_numeric_fallback.rs:73:21
    |
-LL |     let x: _ = s.generic_arg(10);
-   |                              ^^
+LL |         generic_arg(1);
+   |                     ^
+   |
+   = help: consider adding suffix to avoid default numeric fallback
+
+error: default numeric fallback might occur
+  --> $DIR/default_numeric_fallback.rs:76:32
+   |
+LL |         let x: _ = generic_arg(1);
+   |                                ^
+   |
+   = help: consider adding suffix to avoid default numeric fallback
+
+error: default numeric fallback might occur
+  --> $DIR/default_numeric_fallback.rs:96:23
+   |
+LL |         s.generic_arg(1);
+   |                       ^
    |
    = help: consider adding suffix to avoid default numeric fallback
 
-error: aborting due to 9 previous errors
+error: aborting due to 17 previous errors