]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/default_numeric_fallback_f64.rs
Rollup merge of #102581 - jyn514:src-detection, r=Mark-Simulacrum
[rust.git] / src / tools / clippy / 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     }
37 }
38
39 mod nested_local {
40     fn test() {
41         let x: _ = {
42             // Should lint this because this literal is not bound to any types.
43             let y = 1.;
44
45             // Should NOT lint this because this literal is bound to `_` of outer `Local`.
46             1.
47         };
48
49         let x: _ = if true {
50             // Should lint this because this literal is not bound to any types.
51             let y = 1.;
52
53             // Should NOT lint this because this literal is bound to `_` of outer `Local`.
54             1.
55         } else {
56             // Should lint this because this literal is not bound to any types.
57             let y = 1.;
58
59             // Should NOT lint this because this literal is bound to `_` of outer `Local`.
60             2.
61         };
62     }
63 }
64
65 mod function_def {
66     fn ret_f64() -> f64 {
67         // Even though the output type is specified,
68         // this unsuffixed literal is linted to reduce heuristics and keep codebase simple.
69         1.
70     }
71
72     fn test() {
73         // Should lint this because return type is inferred to `f64` and NOT bound to a concrete
74         // type.
75         let f = || -> _ { 1. };
76
77         // Even though the output type is specified,
78         // this unsuffixed literal is linted to reduce heuristics and keep codebase simple.
79         let f = || -> f64 { 1. };
80     }
81 }
82
83 mod function_calls {
84     fn concrete_arg(f: f64) {}
85
86     fn generic_arg<T>(t: T) {}
87
88     fn test() {
89         // Should NOT lint this because the argument type is bound to a concrete type.
90         concrete_arg(1.);
91
92         // Should lint this because the argument type is inferred to `f64` and NOT bound to a concrete type.
93         generic_arg(1.);
94
95         // Should lint this because the argument type is inferred to `f64` and NOT bound to a concrete type.
96         let x: _ = generic_arg(1.);
97     }
98 }
99
100 mod struct_ctor {
101     struct ConcreteStruct {
102         x: f64,
103     }
104
105     struct GenericStruct<T> {
106         x: T,
107     }
108
109     fn test() {
110         // Should NOT lint this because the field type is bound to a concrete type.
111         ConcreteStruct { x: 1. };
112
113         // Should lint this because the field type is inferred to `f64` and NOT bound to a concrete type.
114         GenericStruct { x: 1. };
115
116         // Should lint this because the field type is inferred to `f64` and NOT bound to a concrete type.
117         let _ = GenericStruct { x: 1. };
118     }
119 }
120
121 mod enum_ctor {
122     enum ConcreteEnum {
123         X(f64),
124     }
125
126     enum GenericEnum<T> {
127         X(T),
128     }
129
130     fn test() {
131         // Should NOT lint this because the field type is bound to a concrete type.
132         ConcreteEnum::X(1.);
133
134         // Should lint this because the field type is inferred to `f64` and NOT bound to a concrete type.
135         GenericEnum::X(1.);
136     }
137 }
138
139 mod method_calls {
140     struct StructForMethodCallTest;
141
142     impl StructForMethodCallTest {
143         fn concrete_arg(&self, f: f64) {}
144
145         fn generic_arg<T>(&self, t: T) {}
146     }
147
148     fn test() {
149         let s = StructForMethodCallTest {};
150
151         // Should NOT lint this because the argument type is bound to a concrete type.
152         s.concrete_arg(1.);
153
154         // Should lint this because the argument type is bound to a concrete type.
155         s.generic_arg(1.);
156     }
157 }
158
159 mod in_macro {
160     macro_rules! internal_macro {
161         () => {
162             let x = 22.;
163         };
164     }
165
166     // Should lint in internal macro.
167     fn internal() {
168         internal_macro!();
169     }
170
171     // Should NOT lint in external macro.
172     fn external() {
173         default_numeric_fallback!();
174     }
175 }
176
177 fn main() {}