Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Code clean-up and formatting
  • Loading branch information
mgr-inz-rafal committed Mar 23, 2020
commit ff9602515e4a6ca0792cead4102b8aa1f5cb8a34
36 changes: 23 additions & 13 deletions clippy_lints/src/needless_bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
//! This lint is **warn** by default

use crate::utils::sugg::Sugg;
use crate::utils::{higher, parent_node_is_if_expr, span_lint, span_lint_and_help, span_lint_and_sugg, snippet_with_applicability};
use crate::utils::{higher, parent_node_is_if_expr, snippet_with_applicability, span_lint, span_lint_and_sugg};
use if_chain::if_chain;
use rustc_ast::ast::LitKind;
use rustc_errors::Applicability;
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, StmtKind, UnOp};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::source_map::Spanned;
use rustc_span::Span;

declare_clippy_lint! {
/// **What it does:** Checks for expressions of the form `if c { true } else {
Expand Down Expand Up @@ -189,7 +190,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoolComparison {
}
}

fn is_unary_not<'tcx>(e: &'tcx Expr<'_>) -> (bool, rustc_span::Span) {
struct ExpressionInfoWithSpan {
one_side_is_unary_not: bool,
left_span: Span,
right_span: Span,
}

fn is_unary_not(e: &Expr<'_>) -> (bool, Span) {
if_chain! {
if let ExprKind::Unary(unop, operand) = e.kind;
if let UnOp::UnNot = unop;
Expand All @@ -200,12 +207,15 @@ fn is_unary_not<'tcx>(e: &'tcx Expr<'_>) -> (bool, rustc_span::Span) {
(false, e.span)
}

fn one_side_is_unary_not<'tcx>(left_side: &'tcx Expr<'_>, right_side: &'tcx Expr<'_>) -> (bool, rustc_span::Span, rustc_span::Span) {
fn one_side_is_unary_not<'tcx>(left_side: &'tcx Expr<'_>, right_side: &'tcx Expr<'_>) -> ExpressionInfoWithSpan {
let left = is_unary_not(left_side);
let right = is_unary_not(right_side);

let retval = left.0 ^ right.0;
(retval, left.1, right.1)
ExpressionInfoWithSpan {
one_side_is_unary_not: left.0 ^ right.0,
left_span: left.1,
right_span: right.1,
}
}

fn check_comparison<'a, 'tcx>(
Expand All @@ -224,20 +234,20 @@ fn check_comparison<'a, 'tcx>(
if l_ty.is_bool() && r_ty.is_bool() {
let mut applicability = Applicability::MachineApplicable;

if let BinOpKind::Eq = op.node
{
let xxx = one_side_is_unary_not(&left_side, &right_side);
if xxx.0
{
if let BinOpKind::Eq = op.node {
let expression_info = one_side_is_unary_not(&left_side, &right_side);
if expression_info.one_side_is_unary_not {
span_lint_and_sugg(
cx,
BOOL_COMPARISON,
e.span,
"This comparison might be written more concisely",
"try simplifying it as shown",
format!("{} != {}",
snippet_with_applicability(cx, xxx.1, "..", &mut applicability),
snippet_with_applicability(cx, xxx.2, "..", &mut applicability)),
format!(
"{} != {}",
snippet_with_applicability(cx, expression_info.left_span, "..", &mut applicability),
snippet_with_applicability(cx, expression_info.right_span, "..", &mut applicability)
),
applicability,
)
}
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/bool_comparison.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ fn issue3703() {
if false < Foo {}
}

#[allow(dead_code)]
fn issue4983() {
let a = true;
let b = false;
Expand All @@ -120,4 +121,9 @@ fn issue4983() {
if a != b {};
if a == b {};
if !a == !b {};

if b != a {};
if b != a {};
if b == a {};
if !b == !a {};
}
1 change: 1 addition & 0 deletions tests/ui/bool_comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ fn issue3703() {
if false < Foo {}
}

#[allow(dead_code)]
fn issue4983() {
let a = true;
let b = false;
Expand Down
18 changes: 15 additions & 3 deletions tests/ui/bool_comparison.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,28 @@ LL | if x > y {
| ^^^^^ help: try simplifying it as shown: `x & !y`

error: This comparison might be written more concisely
--> $DIR/bool_comparison.rs:119:8
--> $DIR/bool_comparison.rs:120:8
|
LL | if a == !b {};
| ^^^^^^^ help: try simplifying it as shown: `a != b`

error: This comparison might be written more concisely
--> $DIR/bool_comparison.rs:120:8
--> $DIR/bool_comparison.rs:121:8
|
LL | if !a == b {};
| ^^^^^^^ help: try simplifying it as shown: `a != b`

error: aborting due to 16 previous errors
error: This comparison might be written more concisely
--> $DIR/bool_comparison.rs:125:8
|
LL | if b == !a {};
| ^^^^^^^ help: try simplifying it as shown: `b != a`

error: This comparison might be written more concisely
--> $DIR/bool_comparison.rs:126:8
|
LL | if !b == a {};
| ^^^^^^^ help: try simplifying it as shown: `b != a`

error: aborting due to 18 previous errors