]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/bool_to_int_with_if.rs
Rollup merge of #105801 - zertosh:path_mut_os_str_doc_test, r=dtolnay
[rust.git] / src / tools / clippy / tests / ui / bool_to_int_with_if.rs
1 // run-rustfix
2
3 #![feature(let_chains)]
4 #![warn(clippy::bool_to_int_with_if)]
5 #![allow(unused, dead_code, clippy::unnecessary_operation, clippy::no_effect)]
6
7 fn main() {
8     let a = true;
9     let b = false;
10
11     let x = 1;
12     let y = 2;
13
14     // Should lint
15     // precedence
16     if a {
17         1
18     } else {
19         0
20     };
21     if a {
22         0
23     } else {
24         1
25     };
26     if !a {
27         1
28     } else {
29         0
30     };
31     if a || b {
32         1
33     } else {
34         0
35     };
36     if cond(a, b) {
37         1
38     } else {
39         0
40     };
41     if x + y < 4 {
42         1
43     } else {
44         0
45     };
46
47     // if else if
48     if a {
49         123
50     } else if b {
51         1
52     } else {
53         0
54     };
55
56     // if else if inverted
57     if a {
58         123
59     } else if b {
60         0
61     } else {
62         1
63     };
64
65     // Shouldn't lint
66
67     if a {
68         1
69     } else if b {
70         0
71     } else {
72         3
73     };
74
75     if a {
76         3
77     } else if b {
78         1
79     } else {
80         -2
81     };
82
83     if a {
84         3
85     } else {
86         0
87     };
88     if a {
89         side_effect();
90         1
91     } else {
92         0
93     };
94     if a {
95         1
96     } else {
97         side_effect();
98         0
99     };
100
101     // multiple else ifs
102     if a {
103         123
104     } else if b {
105         1
106     } else if a | b {
107         0
108     } else {
109         123
110     };
111
112     pub const SHOULD_NOT_LINT: usize = if true { 1 } else { 0 };
113
114     some_fn(a);
115 }
116
117 // Lint returns and type inference
118 fn some_fn(a: bool) -> u8 {
119     if a { 1 } else { 0 }
120 }
121
122 fn side_effect() {}
123
124 fn cond(a: bool, b: bool) -> bool {
125     a || b
126 }
127
128 enum Enum {
129     A,
130     B,
131 }
132
133 fn if_let(a: Enum, b: Enum) {
134     if let Enum::A = a {
135         1
136     } else {
137         0
138     };
139
140     if let Enum::A = a && let Enum::B = b {
141         1
142     } else {
143         0
144     };
145 }