]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/tests/ui/default_numeric_fallback_i32.fixed
New upstream version 1.63.0+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / default_numeric_fallback_i32.fixed
CommitLineData
136023e0
XL
1// run-rustfix
2// aux-build:macro_rules.rs
3
923072b8 4#![feature(lint_reasons)]
f20569fa 5#![warn(clippy::default_numeric_fallback)]
04454e1e
FG
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)]
f20569fa 14
136023e0
XL
15#[macro_use]
16extern crate macro_rules;
17
f20569fa
XL
18mod basic_expr {
19 fn test() {
20 // Should lint unsuffixed literals typed `i32`.
136023e0
XL
21 let x = 22_i32;
22 let x = [1_i32, 2_i32, 3_i32];
23 let x = if true { (1_i32, 2_i32) } else { (3_i32, 4_i32) };
24 let x = match 1_i32 {
25 1_i32 => 1_i32,
26 _ => 2_i32,
f20569fa
XL
27 };
28
f20569fa
XL
29 // Should NOT lint suffixed literals.
30 let x = 22_i32;
f20569fa
XL
31
32 // Should NOT lint literals in init expr if `Local` has a type annotation.
f20569fa
XL
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
39mod nested_local {
40 fn test() {
41 let x: _ = {
42 // Should lint this because this literal is not bound to any types.
136023e0 43 let y = 1_i32;
f20569fa
XL
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.
136023e0 51 let y = 1_i32;
f20569fa
XL
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.
136023e0 57 let y = 1_i32;
f20569fa
XL
58
59 // Should NOT lint this because this literal is bound to `_` of outer `Local`.
60 2
61 };
62 }
63}
64
65mod 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.
136023e0 69 1_i32
f20569fa
XL
70 }
71
72 fn test() {
73 // Should lint this because return type is inferred to `i32` and NOT bound to a concrete
74 // type.
136023e0 75 let f = || -> _ { 1_i32 };
f20569fa
XL
76
77 // Even though the output type is specified,
78 // this unsuffixed literal is linted to reduce heuristics and keep codebase simple.
136023e0 79 let f = || -> i32 { 1_i32 };
f20569fa
XL
80 }
81}
82
83mod 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.
136023e0 93 generic_arg(1_i32);
f20569fa
XL
94
95 // Should lint this because the argument type is inferred to `i32` and NOT bound to a concrete type.
136023e0 96 let x: _ = generic_arg(1_i32);
f20569fa
XL
97 }
98}
99
100mod 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.
136023e0 114 GenericStruct { x: 1_i32 };
f20569fa
XL
115
116 // Should lint this because the field type is inferred to `i32` and NOT bound to a concrete type.
136023e0
XL
117 let _ = GenericStruct { x: 1_i32 };
118 }
119}
120
121mod enum_ctor {
122 enum ConcreteEnum {
123 X(i32),
124 }
125
126 enum GenericEnum<T> {
127 X(T),
128 }
129
130 fn test() {
131 // Should NOT lint this because the field type is bound to a concrete type.
132 ConcreteEnum::X(1);
133
134 // Should lint this because the field type is inferred to `i32` and NOT bound to a concrete type.
135 GenericEnum::X(1_i32);
f20569fa
XL
136 }
137}
138
139mod method_calls {
04454e1e 140 struct StructForMethodCallTest;
f20569fa
XL
141
142 impl StructForMethodCallTest {
143 fn concrete_arg(&self, x: i32) {}
144
145 fn generic_arg<T>(&self, t: T) {}
146 }
147
148 fn test() {
149 let s = StructForMethodCallTest {};
150
151 // Should NOT lint this because the argument type is bound to a concrete type.
152 s.concrete_arg(1);
153
154 // Should lint this because the argument type is bound to a concrete type.
136023e0
XL
155 s.generic_arg(1_i32);
156 }
157}
158
159mod in_macro {
160 macro_rules! internal_macro {
161 () => {
162 let x = 22_i32;
163 };
164 }
165
166 // Should lint in internal macro.
167 fn internal() {
168 internal_macro!();
169 }
170
171 // Should NOT lint in external macro.
172 fn external() {
173 default_numeric_fallback!();
f20569fa
XL
174 }
175}
176
923072b8
FG
177fn check_expect_suppression() {
178 #[expect(clippy::default_numeric_fallback)]
179 let x = 21;
180}
181
f20569fa 182fn main() {}