Skip to content

Commit fb91c76

Browse files
committed
Add more tests for default_numeric_fallback
1 parent 0198ac7 commit fb91c76

File tree

3 files changed

+169
-63
lines changed

3 files changed

+169
-63
lines changed

clippy_lints/src/default_numeric_fallback.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ declare_clippy_lint! {
2727
/// **Why is this bad?** For those who are very careful about types, default numeric fallback
2828
/// can be a pitfall that cause unexpected runtime behavior.
2929
///
30-
/// **Known problems:** None.
30+
/// **Known problems:** This lint can only be allowed at the function level or above.
3131
///
3232
/// **Example:**
3333
/// ```rust

tests/ui/default_numeric_fallback.rs

Lines changed: 77 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,97 @@
44
#![allow(clippy::no_effect)]
55
#![allow(clippy::unnecessary_operation)]
66

7-
fn concrete_arg(x: i32) {}
7+
mod basic_expr {
8+
fn test() {
9+
// Should lint unsuffixed literals typed `i32`.
10+
let x = 22;
11+
let x = [1, 2, 3];
12+
let x = if true { (1, 2) } else { (3, 4) };
13+
let x = match 1 {
14+
1 => 1,
15+
_ => 2,
16+
};
817

9-
fn generic_arg<T>(t: T) {}
18+
// Should lint unsuffixed literals typed `f64`.
19+
let x = 0.12;
1020

11-
struct ConcreteStruct {
12-
x: i32,
21+
// Should NOT lint suffixed literals.
22+
let x = 22_i32;
23+
let x = 0.12_f64;
24+
25+
// Should NOT lint literals in init expr if `Local` has a type annotation.
26+
let x: f64 = 0.1;
27+
let x: [i32; 3] = [1, 2, 3];
28+
let x: (i32, i32) = if true { (1, 2) } else { (3, 4) };
29+
let x: _ = 1;
30+
}
1331
}
1432

15-
struct StructForMethodCallTest {
16-
x: i32,
33+
mod nested_local {
34+
fn test() {
35+
let x: _ = {
36+
// Should lint this because this literal is not bound to any types.
37+
let y = 1;
38+
39+
// Should NOT lint this because this literal is bound to `_` of outer `Local`.
40+
1
41+
};
42+
}
1743
}
1844

19-
impl StructForMethodCallTest {
20-
fn concrete_arg(&self, x: i32) {}
45+
mod function_def {
46+
fn ret_i32() -> i32 {
47+
// Even though the output type is specified,
48+
// this unsuffixed literal is linted to reduce heuristics and keep codebase simple.
49+
23
50+
}
2151

22-
fn generic_arg<T>(&self, t: T) {}
52+
fn test() {
53+
// Should lint this because return type is inferred to `i32` and NOT bound to a concrete
54+
// type.
55+
let f = || -> _ { 1 };
56+
57+
// Even though the output type is specified,
58+
// this unsuffixed literal is linted to reduce heuristics and keep codebase simple.
59+
let f = || -> i32 { 1 };
60+
}
2361
}
2462

25-
fn main() {
26-
let s = StructForMethodCallTest { x: 10_i32 };
63+
mod function_calls {
64+
fn concrete_arg(x: i32) {}
65+
66+
fn generic_arg<T>(t: T) {}
2767

28-
// Bad.
29-
let x = 1;
30-
let x = 0.1;
68+
fn test() {
69+
// Should NOT lint this because the argument type is bound to a concrete type.
70+
concrete_arg(1);
3171

32-
let x = if true { 1 } else { 2 };
72+
// Should lint this because the argument type is inferred to `i32` and NOT bound to a concrete type.
73+
generic_arg(1);
3374

34-
let x: _ = {
35-
let y = 1;
36-
1
37-
};
75+
// Should lint this because the argument type is inferred to `i32` and NOT bound to a concrete type.
76+
let x: _ = generic_arg(1);
77+
}
78+
}
79+
80+
mod method_calls {
81+
struct StructForMethodCallTest {}
3882

39-
generic_arg(10);
40-
s.generic_arg(10);
41-
let x: _ = generic_arg(10);
42-
let x: _ = s.generic_arg(10);
83+
impl StructForMethodCallTest {
84+
fn concrete_arg(&self, x: i32) {}
4385

44-
// Good.
45-
let x = 1_i32;
46-
let x: i32 = 1;
47-
let x: _ = 1;
48-
let x = 0.1_f64;
49-
let x: f64 = 0.1;
50-
let x: _ = 0.1;
86+
fn generic_arg<T>(&self, t: T) {}
87+
}
5188

52-
let x: _ = if true { 1 } else { 2 };
89+
fn test() {
90+
let s = StructForMethodCallTest {};
5391

54-
concrete_arg(10);
55-
s.concrete_arg(10);
56-
let x = concrete_arg(10);
57-
let x = s.concrete_arg(10);
92+
// Should NOT lint this because the argument type is bound to a concrete type.
93+
s.concrete_arg(1);
94+
95+
// Should lint this because the argument type is bound to a concrete type.
96+
s.generic_arg(1);
97+
}
5898
}
99+
100+
fn main() {}
Lines changed: 91 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,139 @@
11
error: default numeric fallback might occur
2-
--> $DIR/default_numeric_fallback.rs:29:13
2+
--> $DIR/default_numeric_fallback.rs:10:17
33
|
4-
LL | let x = 1;
5-
| ^
4+
LL | let x = 22;
5+
| ^^
66
|
77
= note: `-D clippy::default-numeric-fallback` implied by `-D warnings`
88
= help: consider adding suffix to avoid default numeric fallback
99

1010
error: default numeric fallback might occur
11-
--> $DIR/default_numeric_fallback.rs:30:13
11+
--> $DIR/default_numeric_fallback.rs:11:18
12+
|
13+
LL | let x = [1, 2, 3];
14+
| ^
15+
|
16+
= help: consider adding suffix to avoid default numeric fallback
17+
18+
error: default numeric fallback might occur
19+
--> $DIR/default_numeric_fallback.rs:11:21
20+
|
21+
LL | let x = [1, 2, 3];
22+
| ^
23+
|
24+
= help: consider adding suffix to avoid default numeric fallback
25+
26+
error: default numeric fallback might occur
27+
--> $DIR/default_numeric_fallback.rs:11:24
1228
|
13-
LL | let x = 0.1;
14-
| ^^^
29+
LL | let x = [1, 2, 3];
30+
| ^
1531
|
1632
= help: consider adding suffix to avoid default numeric fallback
1733

1834
error: default numeric fallback might occur
19-
--> $DIR/default_numeric_fallback.rs:32:23
35+
--> $DIR/default_numeric_fallback.rs:12:28
36+
|
37+
LL | let x = if true { (1, 2) } else { (3, 4) };
38+
| ^
39+
|
40+
= help: consider adding suffix to avoid default numeric fallback
41+
42+
error: default numeric fallback might occur
43+
--> $DIR/default_numeric_fallback.rs:12:31
44+
|
45+
LL | let x = if true { (1, 2) } else { (3, 4) };
46+
| ^
47+
|
48+
= help: consider adding suffix to avoid default numeric fallback
49+
50+
error: default numeric fallback might occur
51+
--> $DIR/default_numeric_fallback.rs:12:44
52+
|
53+
LL | let x = if true { (1, 2) } else { (3, 4) };
54+
| ^
2055
|
21-
LL | let x = if true { 1 } else { 2 };
56+
= help: consider adding suffix to avoid default numeric fallback
57+
58+
error: default numeric fallback might occur
59+
--> $DIR/default_numeric_fallback.rs:12:47
60+
|
61+
LL | let x = if true { (1, 2) } else { (3, 4) };
62+
| ^
63+
|
64+
= help: consider adding suffix to avoid default numeric fallback
65+
66+
error: default numeric fallback might occur
67+
--> $DIR/default_numeric_fallback.rs:13:23
68+
|
69+
LL | let x = match 1 {
2270
| ^
2371
|
2472
= help: consider adding suffix to avoid default numeric fallback
2573

2674
error: default numeric fallback might occur
27-
--> $DIR/default_numeric_fallback.rs:32:34
75+
--> $DIR/default_numeric_fallback.rs:14:13
2876
|
29-
LL | let x = if true { 1 } else { 2 };
30-
| ^
77+
LL | 1 => 1,
78+
| ^
3179
|
3280
= help: consider adding suffix to avoid default numeric fallback
3381

3482
error: default numeric fallback might occur
35-
--> $DIR/default_numeric_fallback.rs:35:17
83+
--> $DIR/default_numeric_fallback.rs:14:18
3684
|
37-
LL | let y = 1;
38-
| ^
85+
LL | 1 => 1,
86+
| ^
3987
|
4088
= help: consider adding suffix to avoid default numeric fallback
4189

4290
error: default numeric fallback might occur
43-
--> $DIR/default_numeric_fallback.rs:39:17
91+
--> $DIR/default_numeric_fallback.rs:15:18
4492
|
45-
LL | generic_arg(10);
46-
| ^^
93+
LL | _ => 2,
94+
| ^
4795
|
4896
= help: consider adding suffix to avoid default numeric fallback
4997

5098
error: default numeric fallback might occur
51-
--> $DIR/default_numeric_fallback.rs:40:19
99+
--> $DIR/default_numeric_fallback.rs:19:17
52100
|
53-
LL | s.generic_arg(10);
54-
| ^^
101+
LL | let x = 0.12;
102+
| ^^^^
55103
|
56104
= help: consider adding suffix to avoid default numeric fallback
57105

58106
error: default numeric fallback might occur
59-
--> $DIR/default_numeric_fallback.rs:41:28
107+
--> $DIR/default_numeric_fallback.rs:37:21
60108
|
61-
LL | let x: _ = generic_arg(10);
62-
| ^^
109+
LL | let y = 1;
110+
| ^
63111
|
64112
= help: consider adding suffix to avoid default numeric fallback
65113

66114
error: default numeric fallback might occur
67-
--> $DIR/default_numeric_fallback.rs:42:30
115+
--> $DIR/default_numeric_fallback.rs:73:21
68116
|
69-
LL | let x: _ = s.generic_arg(10);
70-
| ^^
117+
LL | generic_arg(1);
118+
| ^
119+
|
120+
= help: consider adding suffix to avoid default numeric fallback
121+
122+
error: default numeric fallback might occur
123+
--> $DIR/default_numeric_fallback.rs:76:32
124+
|
125+
LL | let x: _ = generic_arg(1);
126+
| ^
127+
|
128+
= help: consider adding suffix to avoid default numeric fallback
129+
130+
error: default numeric fallback might occur
131+
--> $DIR/default_numeric_fallback.rs:96:23
132+
|
133+
LL | s.generic_arg(1);
134+
| ^
71135
|
72136
= help: consider adding suffix to avoid default numeric fallback
73137

74-
error: aborting due to 9 previous errors
138+
error: aborting due to 17 previous errors
75139

0 commit comments

Comments
 (0)