summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/bool_to_int_with_if.fixed
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools/clippy/tests/ui/bool_to_int_with_if.fixed')
-rw-r--r--src/tools/clippy/tests/ui/bool_to_int_with_if.fixed85
1 files changed, 85 insertions, 0 deletions
diff --git a/src/tools/clippy/tests/ui/bool_to_int_with_if.fixed b/src/tools/clippy/tests/ui/bool_to_int_with_if.fixed
new file mode 100644
index 000000000..9c1098dc4
--- /dev/null
+++ b/src/tools/clippy/tests/ui/bool_to_int_with_if.fixed
@@ -0,0 +1,85 @@
+// run-rustfix
+
+#![warn(clippy::bool_to_int_with_if)]
+#![allow(unused, dead_code, clippy::unnecessary_operation, clippy::no_effect)]
+
+fn main() {
+ let a = true;
+ let b = false;
+
+ let x = 1;
+ let y = 2;
+
+ // Should lint
+ // precedence
+ i32::from(a);
+ i32::from(!a);
+ i32::from(a || b);
+ i32::from(cond(a, b));
+ i32::from(x + y < 4);
+
+ // if else if
+ if a {
+ 123
+ } else {i32::from(b)};
+
+ // Shouldn't lint
+
+ if a {
+ 1
+ } else if b {
+ 0
+ } else {
+ 3
+ };
+
+ if a {
+ 3
+ } else if b {
+ 1
+ } else {
+ -2
+ };
+
+ if a {
+ 3
+ } else {
+ 0
+ };
+ if a {
+ side_effect();
+ 1
+ } else {
+ 0
+ };
+ if a {
+ 1
+ } else {
+ side_effect();
+ 0
+ };
+
+ // multiple else ifs
+ if a {
+ 123
+ } else if b {
+ 1
+ } else if a | b {
+ 0
+ } else {
+ 123
+ };
+
+ some_fn(a);
+}
+
+// Lint returns and type inference
+fn some_fn(a: bool) -> u8 {
+ u8::from(a)
+}
+
+fn side_effect() {}
+
+fn cond(a: bool, b: bool) -> bool {
+ a || b
+}