]> git.proxmox.com Git - rustc.git/blame - 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
CommitLineData
136023e0 1// run-rustfix
353b0b11 2// aux-build:proc_macros.rs
136023e0 3
f20569fa 4#![warn(clippy::default_numeric_fallback)]
04454e1e
FG
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,
353b0b11
FG
12 clippy::let_unit_value,
13 clippy::let_with_type_underscore
04454e1e 14)]
136023e0 15
353b0b11
FG
16extern crate proc_macros;
17use proc_macros::{external, inline_macros};
f20569fa
XL
18
19mod basic_expr {
20 fn test() {
f20569fa 21 // Should lint unsuffixed literals typed `f64`.
136023e0
XL
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 };
f20569fa
XL
28
29 // Should NOT lint suffixed literals.
f20569fa
XL
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;
136023e0
XL
34 let x: [f64; 3] = [1., 2., 3.];
35 let x: (f64, f64) = if true { (1., 2.) } else { (3., 4.) };
36 let x: _ = 1.;
2b03887a 37 const X: f32 = 1.;
f20569fa
XL
38 }
39}
40
41mod nested_local {
42 fn test() {
43 let x: _ = {
44 // Should lint this because this literal is not bound to any types.
136023e0 45 let y = 1.0_f64;
f20569fa
XL
46
47 // Should NOT lint this because this literal is bound to `_` of outer `Local`.
136023e0 48 1.
f20569fa
XL
49 };
50
51 let x: _ = if true {
52 // Should lint this because this literal is not bound to any types.
136023e0 53 let y = 1.0_f64;
f20569fa
XL
54
55 // Should NOT lint this because this literal is bound to `_` of outer `Local`.
136023e0 56 1.
f20569fa
XL
57 } else {
58 // Should lint this because this literal is not bound to any types.
136023e0 59 let y = 1.0_f64;
f20569fa
XL
60
61 // Should NOT lint this because this literal is bound to `_` of outer `Local`.
136023e0 62 2.
f20569fa 63 };
2b03887a
FG
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 };
f20569fa
XL
72 }
73}
74
75mod function_def {
136023e0 76 fn ret_f64() -> f64 {
f20569fa
XL
77 // Even though the output type is specified,
78 // this unsuffixed literal is linted to reduce heuristics and keep codebase simple.
136023e0 79 1.0_f64
f20569fa
XL
80 }
81
82 fn test() {
136023e0 83 // Should lint this because return type is inferred to `f64` and NOT bound to a concrete
f20569fa 84 // type.
136023e0 85 let f = || -> _ { 1.0_f64 };
f20569fa
XL
86
87 // Even though the output type is specified,
88 // this unsuffixed literal is linted to reduce heuristics and keep codebase simple.
136023e0 89 let f = || -> f64 { 1.0_f64 };
f20569fa
XL
90 }
91}
92
93mod function_calls {
136023e0 94 fn concrete_arg(f: f64) {}
f20569fa
XL
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.
136023e0 100 concrete_arg(1.);
f20569fa 101
136023e0
XL
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);
f20569fa 104
136023e0
XL
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);
f20569fa
XL
107 }
108}
109
110mod struct_ctor {
111 struct ConcreteStruct {
136023e0 112 x: f64,
f20569fa
XL
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.
136023e0
XL
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
131mod enum_ctor {
132 enum ConcreteEnum {
133 X(f64),
134 }
f20569fa 135
136023e0
XL
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.);
f20569fa 143
136023e0
XL
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);
f20569fa
XL
146 }
147}
148
149mod method_calls {
04454e1e 150 struct StructForMethodCallTest;
f20569fa
XL
151
152 impl StructForMethodCallTest {
136023e0 153 fn concrete_arg(&self, f: f64) {}
f20569fa
XL
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.
136023e0 162 s.concrete_arg(1.);
f20569fa
XL
163
164 // Should lint this because the argument type is bound to a concrete type.
136023e0
XL
165 s.generic_arg(1.0_f64);
166 }
167}
168
169mod in_macro {
353b0b11 170 use super::*;
136023e0
XL
171
172 // Should lint in internal macro.
353b0b11 173 #[inline_macros]
136023e0 174 fn internal() {
353b0b11 175 inline!(let x = 22.0_f64;);
136023e0
XL
176 }
177
178 // Should NOT lint in external macro.
179 fn external() {
353b0b11 180 external!(let x = 22.;);
f20569fa
XL
181 }
182}
183
184fn main() {}