From 4cfb0966a19ade111bcd57abf53fb1a8f543a127 Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Sun, 4 Aug 2019 22:08:28 +0200 Subject: [PATCH] Fix needless_bool suggestion with if-else-if-else Closes #4334 --- clippy_lints/src/needless_bool.rs | 14 +++++++------ tests/ui/needless_bool.rs | 1 + tests/ui/needless_bool.stderr | 34 +++++++++++++++---------------- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/clippy_lints/src/needless_bool.rs b/clippy_lints/src/needless_bool.rs index 167f00fee..199420071 100644 --- a/clippy_lints/src/needless_bool.rs +++ b/clippy_lints/src/needless_bool.rs @@ -118,13 +118,15 @@ fn parent_node_is_if_expr<'a, 'b>(expr: &Expr, cx: &LateContext<'a, 'b>) -> bool let parent_id = cx.tcx.hir().get_parent_node(expr.hir_id); let parent_node = cx.tcx.hir().get(parent_id); - if let rustc::hir::Node::Expr(e) = parent_node { - if higher::if_block(&e).is_some() { - return true; - } + match parent_node { + rustc::hir::Node::Expr(e) => { + higher::if_block(&e).is_some() + }, + rustc::hir::Node::Arm(e) => { + higher::if_block(&e.body).is_some() + }, + _ => false } - - false } declare_lint_pass!(BoolComparison => [BOOL_COMPARISON]); diff --git a/tests/ui/needless_bool.rs b/tests/ui/needless_bool.rs index 757055257..15582f087 100644 --- a/tests/ui/needless_bool.rs +++ b/tests/ui/needless_bool.rs @@ -1,4 +1,5 @@ #![warn(clippy::needless_bool)] +#![allow(unused, dead_code, clippy::no_effect)] use std::cell::Cell; diff --git a/tests/ui/needless_bool.stderr b/tests/ui/needless_bool.stderr index 30284675f..3c89e51d4 100644 --- a/tests/ui/needless_bool.stderr +++ b/tests/ui/needless_bool.stderr @@ -1,5 +1,5 @@ error: this if-then-else expression will always return true - --> $DIR/needless_bool.rs:31:5 + --> $DIR/needless_bool.rs:32:5 | LL | / if x { LL | | true @@ -11,7 +11,7 @@ LL | | }; = note: `-D clippy::needless-bool` implied by `-D warnings` error: this if-then-else expression will always return false - --> $DIR/needless_bool.rs:36:5 + --> $DIR/needless_bool.rs:37:5 | LL | / if x { LL | | false @@ -21,7 +21,7 @@ LL | | }; | |_____^ error: this if-then-else expression returns a bool literal - --> $DIR/needless_bool.rs:41:5 + --> $DIR/needless_bool.rs:42:5 | LL | / if x { LL | | true @@ -31,7 +31,7 @@ LL | | }; | |_____^ help: you can reduce it to: `x` error: this if-then-else expression returns a bool literal - --> $DIR/needless_bool.rs:46:5 + --> $DIR/needless_bool.rs:47:5 | LL | / if x { LL | | false @@ -41,7 +41,7 @@ LL | | }; | |_____^ help: you can reduce it to: `!x` error: this if-then-else expression returns a bool literal - --> $DIR/needless_bool.rs:51:5 + --> $DIR/needless_bool.rs:52:5 | LL | / if x && y { LL | | false @@ -51,7 +51,7 @@ LL | | }; | |_____^ help: you can reduce it to: `!(x && y)` error: this if-then-else expression will always return true - --> $DIR/needless_bool.rs:74:5 + --> $DIR/needless_bool.rs:75:5 | LL | / if x { LL | | return true; @@ -61,7 +61,7 @@ LL | | }; | |_____^ error: this if-then-else expression will always return false - --> $DIR/needless_bool.rs:83:5 + --> $DIR/needless_bool.rs:84:5 | LL | / if x { LL | | return false; @@ -71,7 +71,7 @@ LL | | }; | |_____^ error: this if-then-else expression returns a bool literal - --> $DIR/needless_bool.rs:92:5 + --> $DIR/needless_bool.rs:93:5 | LL | / if x { LL | | return true; @@ -81,7 +81,7 @@ LL | | }; | |_____^ help: you can reduce it to: `return x` error: this if-then-else expression returns a bool literal - --> $DIR/needless_bool.rs:101:5 + --> $DIR/needless_bool.rs:102:5 | LL | / if x && y { LL | | return true; @@ -91,7 +91,7 @@ LL | | }; | |_____^ help: you can reduce it to: `return x && y` error: this if-then-else expression returns a bool literal - --> $DIR/needless_bool.rs:110:5 + --> $DIR/needless_bool.rs:111:5 | LL | / if x { LL | | return false; @@ -101,7 +101,7 @@ LL | | }; | |_____^ help: you can reduce it to: `return !x` error: this if-then-else expression returns a bool literal - --> $DIR/needless_bool.rs:119:5 + --> $DIR/needless_bool.rs:120:5 | LL | / if x && y { LL | | return false; @@ -111,7 +111,7 @@ LL | | }; | |_____^ help: you can reduce it to: `return !(x && y)` error: equality checks against true are unnecessary - --> $DIR/needless_bool.rs:127:8 + --> $DIR/needless_bool.rs:128:8 | LL | if x == true {}; | ^^^^^^^^^ help: try simplifying it as shown: `x` @@ -119,25 +119,25 @@ LL | if x == true {}; = note: `-D clippy::bool-comparison` implied by `-D warnings` error: equality checks against false can be replaced by a negation - --> $DIR/needless_bool.rs:131:8 + --> $DIR/needless_bool.rs:132:8 | LL | if x == false {}; | ^^^^^^^^^^ help: try simplifying it as shown: `!x` error: equality checks against true are unnecessary - --> $DIR/needless_bool.rs:141:8 + --> $DIR/needless_bool.rs:142:8 | LL | if x == true {}; | ^^^^^^^^^ help: try simplifying it as shown: `x` error: equality checks against false can be replaced by a negation - --> $DIR/needless_bool.rs:142:8 + --> $DIR/needless_bool.rs:143:8 | LL | if x == false {}; | ^^^^^^^^^^ help: try simplifying it as shown: `!x` error: this if-then-else expression returns a bool literal - --> $DIR/needless_bool.rs:151:12 + --> $DIR/needless_bool.rs:152:12 | LL | } else if returns_bool() { | ____________^ @@ -145,7 +145,7 @@ LL | | false LL | | } else { LL | | true LL | | }; - | |_____^ help: you can reduce it to: `!returns_bool()` + | |_____^ help: you can reduce it to: `{ !returns_bool() }` error: aborting due to 16 previous errors