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