]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/bool_to_int_with_if.rs
Merge commit '7248d06384c6a90de58c04c1f46be88821278d8b' into sync-from-clippy
[rust.git] / src / tools / clippy / tests / ui / bool_to_int_with_if.rs
1 // run-rustfix
2
3 #![warn(clippy::bool_to_int_with_if)]
4 #![allow(unused, dead_code, clippy::unnecessary_operation, clippy::no_effect)]
5
6 fn main() {
7     let a = true;
8     let b = false;
9
10     let x = 1;
11     let y = 2;
12
13     // Should lint
14     // precedence
15     if a {
16         1
17     } else {
18         0
19     };
20     if a {
21         0
22     } else {
23         1
24     };
25     if !a {
26         1
27     } else {
28         0
29     };
30     if a || b {
31         1
32     } else {
33         0
34     };
35     if cond(a, b) {
36         1
37     } else {
38         0
39     };
40     if x + y < 4 {
41         1
42     } else {
43         0
44     };
45
46     // if else if
47     if a {
48         123
49     } else if b {
50         1
51     } else {
52         0
53     };
54
55     // if else if inverted
56     if a {
57         123
58     } else if b {
59         0
60     } else {
61         1
62     };
63
64     // Shouldn't lint
65
66     if a {
67         1
68     } else if b {
69         0
70     } else {
71         3
72     };
73
74     if a {
75         3
76     } else if b {
77         1
78     } else {
79         -2
80     };
81
82     if a {
83         3
84     } else {
85         0
86     };
87     if a {
88         side_effect();
89         1
90     } else {
91         0
92     };
93     if a {
94         1
95     } else {
96         side_effect();
97         0
98     };
99
100     // multiple else ifs
101     if a {
102         123
103     } else if b {
104         1
105     } else if a | b {
106         0
107     } else {
108         123
109     };
110
111     some_fn(a);
112 }
113
114 // Lint returns and type inference
115 fn some_fn(a: bool) -> u8 {
116     if a { 1 } else { 0 }
117 }
118
119 fn side_effect() {}
120
121 fn cond(a: bool, b: bool) -> bool {
122     a || b
123 }