Add more tests for default_numeric_fallback
This commit is contained in:
parent
0198ac7bdd
commit
fb91c76586
@ -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
|
||||
|
@ -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) {}
|
||||
|
||||
// Bad.
|
||||
let x = 1;
|
||||
let x = 0.1;
|
||||
fn generic_arg<T>(t: T) {}
|
||||
|
||||
let x = if true { 1 } else { 2 };
|
||||
fn test() {
|
||||
// Should NOT lint this because the argument type is bound to a concrete type.
|
||||
concrete_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.
|
||||
generic_arg(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);
|
||||
// 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 {}
|
||||
|
||||
impl StructForMethodCallTest {
|
||||
fn concrete_arg(&self, x: i32) {}
|
||||
|
||||
fn generic_arg<T>(&self, t: T) {}
|
||||
}
|
||||
|
||||
fn test() {
|
||||
let s = StructForMethodCallTest {};
|
||||
|
||||
// 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() {}
|
||||
|
@ -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 = 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: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
|
||||
|
||||
error: default numeric fallback might occur
|
||||
--> $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: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: aborting due to 9 previous errors
|
||||
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 17 previous errors
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user