Skip to content

Commit 04aa3f7

Browse files
committed
default_numeric_fallback: Add more tests for floating literal
1 parent 3bc5abe commit 04aa3f7

File tree

4 files changed

+349
-66
lines changed

4 files changed

+349
-66
lines changed
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
// aux-build:macro_rules.rs
2+
3+
#![warn(clippy::default_numeric_fallback)]
4+
#![allow(unused)]
5+
#![allow(clippy::never_loop)]
6+
#![allow(clippy::no_effect)]
7+
#![allow(clippy::unnecessary_operation)]
8+
#![allow(clippy::branches_sharing_code)]
9+
#![allow(clippy::branches_sharing_code)]
10+
#![allow(clippy::match_single_binding)]
11+
12+
#[macro_use]
13+
extern crate macro_rules;
14+
15+
mod basic_expr {
16+
fn test() {
17+
// Should lint unsuffixed literals typed `f64`.
18+
let x = 0.12;
19+
let x = [1., 2., 3.];
20+
let x = if true { (1., 2.) } else { (3., 4.) };
21+
let x = match 1. {
22+
_ => 1.,
23+
};
24+
25+
// Should NOT lint suffixed literals.
26+
let x = 0.12_f64;
27+
28+
// Should NOT lint literals in init expr if `Local` has a type annotation.
29+
let x: f64 = 0.1;
30+
let x: [f64; 3] = [1., 2., 3.];
31+
let x: (f64, f64) = if true { (1., 2.) } else { (3., 4.) };
32+
let x: _ = 1.;
33+
}
34+
}
35+
36+
mod nested_local {
37+
fn test() {
38+
let x: _ = {
39+
// Should lint this because this literal is not bound to any types.
40+
let y = 1.;
41+
42+
// Should NOT lint this because this literal is bound to `_` of outer `Local`.
43+
1.
44+
};
45+
46+
let x: _ = if true {
47+
// Should lint this because this literal is not bound to any types.
48+
let y = 1.;
49+
50+
// Should NOT lint this because this literal is bound to `_` of outer `Local`.
51+
1.
52+
} else {
53+
// Should lint this because this literal is not bound to any types.
54+
let y = 1.;
55+
56+
// Should NOT lint this because this literal is bound to `_` of outer `Local`.
57+
2.
58+
};
59+
}
60+
}
61+
62+
mod function_def {
63+
fn ret_f64() -> f64 {
64+
// Even though the output type is specified,
65+
// this unsuffixed literal is linted to reduce heuristics and keep codebase simple.
66+
1.
67+
}
68+
69+
fn test() {
70+
// Should lint this because return type is inferred to `f64` and NOT bound to a concrete
71+
// type.
72+
let f = || -> _ { 1. };
73+
74+
// Even though the output type is specified,
75+
// this unsuffixed literal is linted to reduce heuristics and keep codebase simple.
76+
let f = || -> f64 { 1. };
77+
}
78+
}
79+
80+
mod function_calls {
81+
fn concrete_arg(f: f64) {}
82+
83+
fn generic_arg<T>(t: T) {}
84+
85+
fn test() {
86+
// Should NOT lint this because the argument type is bound to a concrete type.
87+
concrete_arg(1.);
88+
89+
// Should lint this because the argument type is inferred to `f64` and NOT bound to a concrete type.
90+
generic_arg(1.);
91+
92+
// Should lint this because the argument type is inferred to `f64` and NOT bound to a concrete type.
93+
let x: _ = generic_arg(1.);
94+
}
95+
}
96+
97+
mod struct_ctor {
98+
struct ConcreteStruct {
99+
x: f64,
100+
}
101+
102+
struct GenericStruct<T> {
103+
x: T,
104+
}
105+
106+
fn test() {
107+
// Should NOT lint this because the field type is bound to a concrete type.
108+
ConcreteStruct { x: 1. };
109+
110+
// Should lint this because the field type is inferred to `f64` and NOT bound to a concrete type.
111+
GenericStruct { x: 1. };
112+
113+
// Should lint this because the field type is inferred to `f64` and NOT bound to a concrete type.
114+
let _ = GenericStruct { x: 1. };
115+
}
116+
}
117+
118+
mod enum_ctor {
119+
enum ConcreteEnum {
120+
X(f64),
121+
}
122+
123+
enum GenericEnum<T> {
124+
X(T),
125+
}
126+
127+
fn test() {
128+
// Should NOT lint this because the field type is bound to a concrete type.
129+
ConcreteEnum::X(1.);
130+
131+
// Should lint this because the field type is inferred to `f64` and NOT bound to a concrete type.
132+
GenericEnum::X(1.);
133+
}
134+
}
135+
136+
mod method_calls {
137+
struct StructForMethodCallTest {}
138+
139+
impl StructForMethodCallTest {
140+
fn concrete_arg(&self, f: f64) {}
141+
142+
fn generic_arg<T>(&self, t: T) {}
143+
}
144+
145+
fn test() {
146+
let s = StructForMethodCallTest {};
147+
148+
// Should NOT lint this because the argument type is bound to a concrete type.
149+
s.concrete_arg(1.);
150+
151+
// Should lint this because the argument type is bound to a concrete type.
152+
s.generic_arg(1.);
153+
}
154+
}
155+
156+
mod in_macro {
157+
macro_rules! internal_macro {
158+
() => {
159+
let x = 22.;
160+
};
161+
}
162+
163+
// Should lint in internal macro.
164+
fn internal() {
165+
internal_macro!();
166+
}
167+
168+
// Should NOT lint in external macro.
169+
fn external() {
170+
default_numeric_fallback!();
171+
}
172+
}
173+
174+
fn main() {}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
error: default numeric fallback might occur
2+
--> $DIR/default_numeric_fallback_f64.rs:18:17
3+
|
4+
LL | let x = 0.12;
5+
| ^^^^ help: consider adding suffix: `0.12_f64`
6+
|
7+
= note: `-D clippy::default-numeric-fallback` implied by `-D warnings`
8+
9+
error: default numeric fallback might occur
10+
--> $DIR/default_numeric_fallback_f64.rs:19:18
11+
|
12+
LL | let x = [1., 2., 3.];
13+
| ^^ help: consider adding suffix: `1._f64`
14+
15+
error: default numeric fallback might occur
16+
--> $DIR/default_numeric_fallback_f64.rs:19:22
17+
|
18+
LL | let x = [1., 2., 3.];
19+
| ^^ help: consider adding suffix: `2._f64`
20+
21+
error: default numeric fallback might occur
22+
--> $DIR/default_numeric_fallback_f64.rs:19:26
23+
|
24+
LL | let x = [1., 2., 3.];
25+
| ^^ help: consider adding suffix: `3._f64`
26+
27+
error: default numeric fallback might occur
28+
--> $DIR/default_numeric_fallback_f64.rs:20:28
29+
|
30+
LL | let x = if true { (1., 2.) } else { (3., 4.) };
31+
| ^^ help: consider adding suffix: `1._f64`
32+
33+
error: default numeric fallback might occur
34+
--> $DIR/default_numeric_fallback_f64.rs:20:32
35+
|
36+
LL | let x = if true { (1., 2.) } else { (3., 4.) };
37+
| ^^ help: consider adding suffix: `2._f64`
38+
39+
error: default numeric fallback might occur
40+
--> $DIR/default_numeric_fallback_f64.rs:20:46
41+
|
42+
LL | let x = if true { (1., 2.) } else { (3., 4.) };
43+
| ^^ help: consider adding suffix: `3._f64`
44+
45+
error: default numeric fallback might occur
46+
--> $DIR/default_numeric_fallback_f64.rs:20:50
47+
|
48+
LL | let x = if true { (1., 2.) } else { (3., 4.) };
49+
| ^^ help: consider adding suffix: `4._f64`
50+
51+
error: default numeric fallback might occur
52+
--> $DIR/default_numeric_fallback_f64.rs:21:23
53+
|
54+
LL | let x = match 1. {
55+
| ^^ help: consider adding suffix: `1._f64`
56+
57+
error: default numeric fallback might occur
58+
--> $DIR/default_numeric_fallback_f64.rs:22:18
59+
|
60+
LL | _ => 1.,
61+
| ^^ help: consider adding suffix: `1._f64`
62+
63+
error: default numeric fallback might occur
64+
--> $DIR/default_numeric_fallback_f64.rs:40:21
65+
|
66+
LL | let y = 1.;
67+
| ^^ help: consider adding suffix: `1._f64`
68+
69+
error: default numeric fallback might occur
70+
--> $DIR/default_numeric_fallback_f64.rs:48:21
71+
|
72+
LL | let y = 1.;
73+
| ^^ help: consider adding suffix: `1._f64`
74+
75+
error: default numeric fallback might occur
76+
--> $DIR/default_numeric_fallback_f64.rs:54:21
77+
|
78+
LL | let y = 1.;
79+
| ^^ help: consider adding suffix: `1._f64`
80+
81+
error: default numeric fallback might occur
82+
--> $DIR/default_numeric_fallback_f64.rs:66:9
83+
|
84+
LL | 1.
85+
| ^^ help: consider adding suffix: `1._f64`
86+
87+
error: default numeric fallback might occur
88+
--> $DIR/default_numeric_fallback_f64.rs:72:27
89+
|
90+
LL | let f = || -> _ { 1. };
91+
| ^^ help: consider adding suffix: `1._f64`
92+
93+
error: default numeric fallback might occur
94+
--> $DIR/default_numeric_fallback_f64.rs:76:29
95+
|
96+
LL | let f = || -> f64 { 1. };
97+
| ^^ help: consider adding suffix: `1._f64`
98+
99+
error: default numeric fallback might occur
100+
--> $DIR/default_numeric_fallback_f64.rs:90:21
101+
|
102+
LL | generic_arg(1.);
103+
| ^^ help: consider adding suffix: `1._f64`
104+
105+
error: default numeric fallback might occur
106+
--> $DIR/default_numeric_fallback_f64.rs:93:32
107+
|
108+
LL | let x: _ = generic_arg(1.);
109+
| ^^ help: consider adding suffix: `1._f64`
110+
111+
error: default numeric fallback might occur
112+
--> $DIR/default_numeric_fallback_f64.rs:111:28
113+
|
114+
LL | GenericStruct { x: 1. };
115+
| ^^ help: consider adding suffix: `1._f64`
116+
117+
error: default numeric fallback might occur
118+
--> $DIR/default_numeric_fallback_f64.rs:114:36
119+
|
120+
LL | let _ = GenericStruct { x: 1. };
121+
| ^^ help: consider adding suffix: `1._f64`
122+
123+
error: default numeric fallback might occur
124+
--> $DIR/default_numeric_fallback_f64.rs:132:24
125+
|
126+
LL | GenericEnum::X(1.);
127+
| ^^ help: consider adding suffix: `1._f64`
128+
129+
error: default numeric fallback might occur
130+
--> $DIR/default_numeric_fallback_f64.rs:152:23
131+
|
132+
LL | s.generic_arg(1.);
133+
| ^^ help: consider adding suffix: `1._f64`
134+
135+
error: default numeric fallback might occur
136+
--> $DIR/default_numeric_fallback_f64.rs:159:21
137+
|
138+
LL | let x = 22.;
139+
| ^^^ help: consider adding suffix: `22._f64`
140+
...
141+
LL | internal_macro!();
142+
| ------------------ in this macro invocation
143+
|
144+
= note: this error originates in the macro `internal_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
145+
146+
error: aborting due to 23 previous errors
147+

tests/ui/default_numeric_fallback.rs renamed to tests/ui/default_numeric_fallback_i32.rs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,10 @@ mod basic_expr {
2121
_ => 2,
2222
};
2323

24-
// Should lint unsuffixed literals typed `f64`.
25-
let x = 0.12;
26-
2724
// Should NOT lint suffixed literals.
2825
let x = 22_i32;
29-
let x = 0.12_f64;
3026

3127
// Should NOT lint literals in init expr if `Local` has a type annotation.
32-
let x: f64 = 0.1;
3328
let x: [i32; 3] = [1, 2, 3];
3429
let x: (i32, i32) = if true { (1, 2) } else { (3, 4) };
3530
let x: _ = 1;
@@ -81,25 +76,17 @@ mod function_def {
8176
}
8277

8378
mod function_calls {
84-
fn concrete_arg_i32(x: i32) {}
85-
86-
fn concrete_arg_f64(f: f64) {}
79+
fn concrete_arg(x: i32) {}
8780

8881
fn generic_arg<T>(t: T) {}
8982

9083
fn test() {
9184
// Should NOT lint this because the argument type is bound to a concrete type.
92-
concrete_arg_i32(1);
93-
94-
// Should NOT lint this because the argument type is bound to a concrete type.
95-
concrete_arg_f64(1.);
85+
concrete_arg(1);
9686

9787
// Should lint this because the argument type is inferred to `i32` and NOT bound to a concrete type.
9888
generic_arg(1);
9989

100-
// Should lint this because the argument type is inferred to `f32` and NOT bound to a concrete type.
101-
generic_arg(1.0);
102-
10390
// Should lint this because the argument type is inferred to `i32` and NOT bound to a concrete type.
10491
let x: _ = generic_arg(1);
10592
}
@@ -129,7 +116,6 @@ mod struct_ctor {
129116
mod enum_ctor {
130117
enum ConcreteEnum {
131118
X(i32),
132-
Y(f64),
133119
}
134120

135121
enum GenericEnum<T> {
@@ -140,14 +126,8 @@ mod enum_ctor {
140126
// Should NOT lint this because the field type is bound to a concrete type.
141127
ConcreteEnum::X(1);
142128

143-
// Should NOT lint this because the field type is bound to a concrete type.
144-
ConcreteEnum::Y(1.);
145-
146129
// Should lint this because the field type is inferred to `i32` and NOT bound to a concrete type.
147130
GenericEnum::X(1);
148-
149-
// Should lint this because the field type is inferred to `f64` and NOT bound to a concrete type.
150-
GenericEnum::X(1.);
151131
}
152132
}
153133

0 commit comments

Comments
 (0)