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