]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/tests/ui/default_numeric_fallback_i32.fixed
New upstream version 1.62.1+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
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::let_unit_value
12)]
f20569fa 13
136023e0
XL
14#[macro_use]
15extern crate macro_rules;
16
f20569fa
XL
17mod basic_expr {
18 fn test() {
19 // Should lint unsuffixed literals typed `i32`.
136023e0
XL
20 let x = 22_i32;
21 let x = [1_i32, 2_i32, 3_i32];
22 let x = if true { (1_i32, 2_i32) } else { (3_i32, 4_i32) };
23 let x = match 1_i32 {
24 1_i32 => 1_i32,
25 _ => 2_i32,
f20569fa
XL
26 };
27
f20569fa
XL
28 // Should NOT lint suffixed literals.
29 let x = 22_i32;
f20569fa
XL
30
31 // Should NOT lint literals in init expr if `Local` has a type annotation.
f20569fa
XL
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
38mod nested_local {
39 fn test() {
40 let x: _ = {
41 // Should lint this because this literal is not bound to any types.
136023e0 42 let y = 1_i32;
f20569fa
XL
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.
136023e0 50 let y = 1_i32;
f20569fa
XL
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.
136023e0 56 let y = 1_i32;
f20569fa
XL
57
58 // Should NOT lint this because this literal is bound to `_` of outer `Local`.
59 2
60 };
61 }
62}
63
64mod 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.
136023e0 68 1_i32
f20569fa
XL
69 }
70
71 fn test() {
72 // Should lint this because return type is inferred to `i32` and NOT bound to a concrete
73 // type.
136023e0 74 let f = || -> _ { 1_i32 };
f20569fa
XL
75
76 // Even though the output type is specified,
77 // this unsuffixed literal is linted to reduce heuristics and keep codebase simple.
136023e0 78 let f = || -> i32 { 1_i32 };
f20569fa
XL
79 }
80}
81
82mod 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.
136023e0 92 generic_arg(1_i32);
f20569fa
XL
93
94 // Should lint this because the argument type is inferred to `i32` and NOT bound to a concrete type.
136023e0 95 let x: _ = generic_arg(1_i32);
f20569fa
XL
96 }
97}
98
99mod 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.
136023e0 113 GenericStruct { x: 1_i32 };
f20569fa
XL
114
115 // Should lint this because the field type is inferred to `i32` and NOT bound to a concrete type.
136023e0
XL
116 let _ = GenericStruct { x: 1_i32 };
117 }
118}
119
120mod 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_i32);
f20569fa
XL
135 }
136}
137
138mod method_calls {
04454e1e 139 struct StructForMethodCallTest;
f20569fa
XL
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.
136023e0
XL
154 s.generic_arg(1_i32);
155 }
156}
157
158mod in_macro {
159 macro_rules! internal_macro {
160 () => {
161 let x = 22_i32;
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!();
f20569fa
XL
173 }
174}
175
176fn main() {}