Skip to content

Commit

Permalink
Code clean-up and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mgr-inz-rafal committed Mar 23, 2020
1 parent 3d3af07 commit ff96025
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 16 deletions.
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

0 comments on commit ff96025

Please sign in to comment.