]> git.lizzy.rs Git - rust.git/blob - tests/ui/default_numeric_fallback.rs
Merge remote-tracking branch 'upstream/beta' into backport_remerge
[rust.git] / tests / ui / default_numeric_fallback.rs
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
10 #[macro_use]
11 extern crate macro_rules;
12
13 mod basic_expr {
14     fn test() {
15         // Should lint unsuffixed literals typed `i32`.
16         let x = 22;
17         let x = [1, 2, 3];
18         let x = if true { (1, 2) } else { (3, 4) };
19         let x = match 1 {
20             1 => 1,
21             _ => 2,
22         };
23
24         // Should lint unsuffixed literals typed `f64`.
25         let x = 0.12;
26
27         // Should NOT lint suffixed literals.
28         let x = 22_i32;
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: [i32; 3] = [1, 2, 3];
34         let x: (i32, i32) = 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_i32() -> i32 {
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 `i32` 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 = || -> i32 { 1 };
80     }
81 }
82
83 mod function_calls {
84     fn concrete_arg(x: i32) {}
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 `i32` and NOT bound to a concrete type.
93         generic_arg(1);
94
95         // Should lint this because the argument type is inferred to `i32` 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: i32,
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 `i32` and NOT bound to a concrete type.
114         GenericStruct { x: 1 };
115
116         // Should lint this because the field type is inferred to `i32` and NOT bound to a concrete type.
117         let _ = GenericStruct { x: 1 };
118     }
119 }
120
121 mod method_calls {
122     struct StructForMethodCallTest {}
123
124     impl StructForMethodCallTest {
125         fn concrete_arg(&self, x: i32) {}
126
127         fn generic_arg<T>(&self, t: T) {}
128     }
129
130     fn test() {
131         let s = StructForMethodCallTest {};
132
133         // Should NOT lint this because the argument type is bound to a concrete type.
134         s.concrete_arg(1);
135
136         // Should lint this because the argument type is bound to a concrete type.
137         s.generic_arg(1);
138     }
139 }
140
141 mod in_macro {
142     macro_rules! internal_macro {
143         () => {
144             let x = 22;
145         };
146     }
147
148     // Should lint in internal macro.
149     fn internal() {
150         internal_macro!();
151     }
152
153     // Should NOT lint in external macro.
154     fn external() {
155         default_numeric_fallback!();
156     }
157 }
158
159 fn main() {}