]> git.lizzy.rs Git - rust.git/blob - tests/ui/default_numeric_fallback.rs
Auto merge of #7166 - TaKO8Ki:refactor_misc_early_module, r=llogiq
[rust.git] / tests / ui / default_numeric_fallback.rs
1 #![warn(clippy::default_numeric_fallback)]
2 #![allow(unused)]
3 #![allow(clippy::never_loop)]
4 #![allow(clippy::no_effect)]
5 #![allow(clippy::unnecessary_operation)]
6 #![allow(clippy::branches_sharing_code)]
7
8 mod basic_expr {
9     fn test() {
10         // Should lint unsuffixed literals typed `i32`.
11         let x = 22;
12         let x = [1, 2, 3];
13         let x = if true { (1, 2) } else { (3, 4) };
14         let x = match 1 {
15             1 => 1,
16             _ => 2,
17         };
18
19         // Should lint unsuffixed literals typed `f64`.
20         let x = 0.12;
21
22         // Should NOT lint suffixed literals.
23         let x = 22_i32;
24         let x = 0.12_f64;
25
26         // Should NOT lint literals in init expr if `Local` has a type annotation.
27         let x: f64 = 0.1;
28         let x: [i32; 3] = [1, 2, 3];
29         let x: (i32, i32) = if true { (1, 2) } else { (3, 4) };
30         let x: _ = 1;
31     }
32 }
33
34 mod nested_local {
35     fn test() {
36         let x: _ = {
37             // Should lint this because this literal is not bound to any types.
38             let y = 1;
39
40             // Should NOT lint this because this literal is bound to `_` of outer `Local`.
41             1
42         };
43
44         let x: _ = if true {
45             // Should lint this because this literal is not bound to any types.
46             let y = 1;
47
48             // Should NOT lint this because this literal is bound to `_` of outer `Local`.
49             1
50         } else {
51             // Should lint this because this literal is not bound to any types.
52             let y = 1;
53
54             // Should NOT lint this because this literal is bound to `_` of outer `Local`.
55             2
56         };
57     }
58 }
59
60 mod function_def {
61     fn ret_i32() -> i32 {
62         // Even though the output type is specified,
63         // this unsuffixed literal is linted to reduce heuristics and keep codebase simple.
64         1
65     }
66
67     fn test() {
68         // Should lint this because return type is inferred to `i32` and NOT bound to a concrete
69         // type.
70         let f = || -> _ { 1 };
71
72         // Even though the output type is specified,
73         // this unsuffixed literal is linted to reduce heuristics and keep codebase simple.
74         let f = || -> i32 { 1 };
75     }
76 }
77
78 mod function_calls {
79     fn concrete_arg(x: i32) {}
80
81     fn generic_arg<T>(t: T) {}
82
83     fn test() {
84         // Should NOT lint this because the argument type is bound to a concrete type.
85         concrete_arg(1);
86
87         // Should lint this because the argument type is inferred to `i32` and NOT bound to a concrete type.
88         generic_arg(1);
89
90         // Should lint this because the argument type is inferred to `i32` and NOT bound to a concrete type.
91         let x: _ = generic_arg(1);
92     }
93 }
94
95 mod struct_ctor {
96     struct ConcreteStruct {
97         x: i32,
98     }
99
100     struct GenericStruct<T> {
101         x: T,
102     }
103
104     fn test() {
105         // Should NOT lint this because the field type is bound to a concrete type.
106         ConcreteStruct { x: 1 };
107
108         // Should lint this because the field type is inferred to `i32` and NOT bound to a concrete type.
109         GenericStruct { x: 1 };
110
111         // Should lint this because the field type is inferred to `i32` and NOT bound to a concrete type.
112         let _ = GenericStruct { x: 1 };
113     }
114 }
115
116 mod method_calls {
117     struct StructForMethodCallTest {}
118
119     impl StructForMethodCallTest {
120         fn concrete_arg(&self, x: i32) {}
121
122         fn generic_arg<T>(&self, t: T) {}
123     }
124
125     fn test() {
126         let s = StructForMethodCallTest {};
127
128         // Should NOT lint this because the argument type is bound to a concrete type.
129         s.concrete_arg(1);
130
131         // Should lint this because the argument type is bound to a concrete type.
132         s.generic_arg(1);
133     }
134 }
135
136 fn main() {}