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