Add more tests for default_numeric_fallback

This commit is contained in:
Yoshitomo Nakanishi
2021-02-15 23:33:27 +09:00
parent 0198ac7bdd
commit fb91c76586
3 changed files with 172 additions and 66 deletions

View File

@@ -27,7 +27,7 @@ declare_clippy_lint! {
/// **Why is this bad?** For those who are very careful about types, default numeric fallback /// **Why is this bad?** For those who are very careful about types, default numeric fallback
/// can be a pitfall that cause unexpected runtime behavior. /// 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:** /// **Example:**
/// ```rust /// ```rust

View File

@@ -4,17 +4,81 @@
#![allow(clippy::no_effect)] #![allow(clippy::no_effect)]
#![allow(clippy::unnecessary_operation)] #![allow(clippy::unnecessary_operation)]
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,
};
// Should lint unsuffixed literals typed `f64`.
let x = 0.12;
// 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;
}
}
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
};
}
}
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 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 };
}
}
mod function_calls {
fn concrete_arg(x: i32) {} fn concrete_arg(x: i32) {}
fn generic_arg<T>(t: T) {} fn generic_arg<T>(t: T) {}
struct ConcreteStruct { fn test() {
x: i32, // Should NOT lint this because the argument type is bound to a concrete type.
concrete_arg(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 `i32` and NOT bound to a concrete type.
let x: _ = generic_arg(1);
}
} }
struct StructForMethodCallTest { mod method_calls {
x: i32, struct StructForMethodCallTest {}
}
impl StructForMethodCallTest { impl StructForMethodCallTest {
fn concrete_arg(&self, x: i32) {} fn concrete_arg(&self, x: i32) {}
@@ -22,37 +86,15 @@ impl StructForMethodCallTest {
fn generic_arg<T>(&self, t: T) {} fn generic_arg<T>(&self, t: T) {}
} }
fn main() { fn test() {
let s = StructForMethodCallTest { x: 10_i32 }; let s = StructForMethodCallTest {};
// Bad. // Should NOT lint this because the argument type is bound to a concrete type.
let x = 1; s.concrete_arg(1);
let x = 0.1;
let x = if true { 1 } else { 2 }; // Should lint this because the argument type is bound to a concrete type.
s.generic_arg(1);
let x: _ = {
let y = 1;
1
};
generic_arg(10);
s.generic_arg(10);
let x: _ = generic_arg(10);
let x: _ = s.generic_arg(10);
// 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;
let x: _ = if true { 1 } else { 2 };
concrete_arg(10);
s.concrete_arg(10);
let x = concrete_arg(10);
let x = s.concrete_arg(10);
} }
}
fn main() {}

View File

@@ -1,38 +1,110 @@
error: default numeric fallback might occur 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` = note: `-D clippy::default-numeric-fallback` implied by `-D warnings`
= help: consider adding suffix to avoid default numeric fallback = help: consider adding suffix to avoid default numeric fallback
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:30:13 --> $DIR/default_numeric_fallback.rs:11:18
| |
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
|
LL | let x = if true { 1 } else { 2 };
| ^ | ^
| |
= help: consider adding suffix to avoid default numeric fallback = help: consider adding suffix to avoid default numeric fallback
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:32:34 --> $DIR/default_numeric_fallback.rs:11:21
| |
LL | let x = if true { 1 } else { 2 }; LL | let x = [1, 2, 3];
| ^ | ^
| |
= help: consider adding suffix to avoid default numeric fallback = help: consider adding suffix to avoid default numeric fallback
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:35:17 --> $DIR/default_numeric_fallback.rs:11:24
|
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: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) };
| ^
|
= 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:14:13
|
LL | 1 => 1,
| ^
|
= help: consider adding suffix to avoid default numeric fallback
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:14:18
|
LL | 1 => 1,
| ^
|
= help: consider adding suffix to avoid default numeric fallback
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:15:18
|
LL | _ => 2,
| ^
|
= help: consider adding suffix to avoid default numeric fallback
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:19:17
|
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:37:21
| |
LL | let y = 1; LL | let y = 1;
| ^ | ^
@@ -40,36 +112,28 @@ LL | let y = 1;
= help: consider adding suffix to avoid default numeric fallback = help: consider adding suffix to avoid default numeric fallback
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:39:17 --> $DIR/default_numeric_fallback.rs:73:21
| |
LL | generic_arg(10); LL | generic_arg(1);
| ^^ | ^
| |
= help: consider adding suffix to avoid default numeric fallback = help: consider adding suffix to avoid default numeric fallback
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:40:19 --> $DIR/default_numeric_fallback.rs:76:32
| |
LL | s.generic_arg(10); LL | let x: _ = generic_arg(1);
| ^^ | ^
| |
= help: consider adding suffix to avoid default numeric fallback = help: consider adding suffix to avoid default numeric fallback
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:41:28 --> $DIR/default_numeric_fallback.rs:96:23
| |
LL | let x: _ = generic_arg(10); LL | s.generic_arg(1);
| ^^ | ^
| |
= help: consider adding suffix to avoid default numeric fallback = help: consider adding suffix to avoid default numeric fallback
error: default numeric fallback might occur error: aborting due to 17 previous errors
--> $DIR/default_numeric_fallback.rs:42:30
|
LL | let x: _ = s.generic_arg(10);
| ^^
|
= help: consider adding suffix to avoid default numeric fallback
error: aborting due to 9 previous errors