]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/default_numeric_fallback_i32.rs
Auto merge of #102717 - beetrees:repr128-c-style-debuginfo, 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 #![feature(lint_reasons)]
5 #![warn(clippy::default_numeric_fallback)]
6 #![allow(
7     unused,
8     clippy::never_loop,
9     clippy::no_effect,
10     clippy::unnecessary_operation,
11     clippy::branches_sharing_code,
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 `i32`.
21         let x = 22;
22         let x = [1, 2, 3];
23         let x = if true { (1, 2) } else { (3, 4) };
24         let x = match 1 {
25             1 => 1,
26             _ => 2,
27         };
28
29         // Should NOT lint suffixed literals.
30         let x = 22_i32;
31
32         // Should NOT lint literals in init expr if `Local` has a type annotation.
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         let x: u64 = 1;
37         const CONST_X: i8 = 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;
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;
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;
60
61             // Should NOT lint this because this literal is bound to `_` of outer `Local`.
62             2
63         };
64
65         const CONST_X: i32 = {
66             // Should lint this because this literal is not bound to any types.
67             let y = 1;
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_i32() -> i32 {
77         // Even though the output type is specified,
78         // this unsuffixed literal is linted to reduce heuristics and keep codebase simple.
79         1
80     }
81
82     fn test() {
83         // Should lint this because return type is inferred to `i32` and NOT bound to a concrete
84         // type.
85         let f = || -> _ { 1 };
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 = || -> i32 { 1 };
90     }
91 }
92
93 mod function_calls {
94     fn concrete_arg(x: i32) {}
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 `i32` and NOT bound to a concrete type.
103         generic_arg(1);
104
105         // Should lint this because the argument type is inferred to `i32` and NOT bound to a concrete type.
106         let x: _ = generic_arg(1);
107     }
108 }
109
110 mod struct_ctor {
111     struct ConcreteStruct {
112         x: i32,
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 `i32` and NOT bound to a concrete type.
124         GenericStruct { x: 1 };
125
126         // Should lint this because the field type is inferred to `i32` and NOT bound to a concrete type.
127         let _ = GenericStruct { x: 1 };
128     }
129 }
130
131 mod enum_ctor {
132     enum ConcreteEnum {
133         X(i32),
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 `i32` and NOT bound to a concrete type.
145         GenericEnum::X(1);
146     }
147 }
148
149 mod method_calls {
150     struct StructForMethodCallTest;
151
152     impl StructForMethodCallTest {
153         fn concrete_arg(&self, x: i32) {}
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);
166     }
167 }
168
169 mod in_macro {
170     macro_rules! internal_macro {
171         () => {
172             let x = 22;
173         };
174     }
175
176     // Should lint in internal macro.
177     fn internal() {
178         internal_macro!();
179     }
180
181     // Should NOT lint in external macro.
182     fn external() {
183         default_numeric_fallback!();
184     }
185 }
186
187 fn check_expect_suppression() {
188     #[expect(clippy::default_numeric_fallback)]
189     let x = 21;
190 }
191
192 fn main() {}