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