Auto merge of #3817 - rust-lang:fix-bool_comparison-on-non-bool, r=oli-obk

Fix `bool_comparison` with non-`bool` expressions

Fixes #3703.
It just moves around the type check that was already there for some comparison to all of them, because if one type isn't `bool`, none of those comparison can be simplified.
This commit is contained in:
bors 2019-02-26 15:51:40 +00:00
commit 0957f7d3b7
2 changed files with 56 additions and 20 deletions

View file

@ -225,23 +225,23 @@ fn check_comparison<'a, 'tcx>(
use self::Expression::*;
if let ExprKind::Binary(_, ref left_side, ref right_side) = e.node {
let mut applicability = Applicability::MachineApplicable;
match (fetch_bool_expr(left_side), fetch_bool_expr(right_side)) {
(Bool(true), Other) => left_true.map_or((), |(h, m)| {
suggest_bool_comparison(cx, e, right_side, applicability, m, h)
}),
(Other, Bool(true)) => right_true.map_or((), |(h, m)| {
suggest_bool_comparison(cx, e, left_side, applicability, m, h)
}),
(Bool(false), Other) => left_false.map_or((), |(h, m)| {
suggest_bool_comparison(cx, e, right_side, applicability, m, h)
}),
(Other, Bool(false)) => right_false.map_or((), |(h, m)| {
suggest_bool_comparison(cx, e, left_side, applicability, m, h)
}),
(Other, Other) => no_literal.map_or((), |(h, m)| {
let (l_ty, r_ty) = (cx.tables.expr_ty(left_side), cx.tables.expr_ty(right_side));
if l_ty.is_bool() && r_ty.is_bool() {
let (l_ty, r_ty) = (cx.tables.expr_ty(left_side), cx.tables.expr_ty(right_side));
if l_ty.is_bool() && r_ty.is_bool() {
let mut applicability = Applicability::MachineApplicable;
match (fetch_bool_expr(left_side), fetch_bool_expr(right_side)) {
(Bool(true), Other) => left_true.map_or((), |(h, m)| {
suggest_bool_comparison(cx, e, right_side, applicability, m, h)
}),
(Other, Bool(true)) => right_true.map_or((), |(h, m)| {
suggest_bool_comparison(cx, e, left_side, applicability, m, h)
}),
(Bool(false), Other) => left_false.map_or((), |(h, m)| {
suggest_bool_comparison(cx, e, right_side, applicability, m, h)
}),
(Other, Bool(false)) => right_false.map_or((), |(h, m)| {
suggest_bool_comparison(cx, e, left_side, applicability, m, h)
}),
(Other, Other) => no_literal.map_or((), |(h, m)| {
let left_side = Sugg::hir_with_applicability(cx, left_side, "..", &mut applicability);
let right_side = Sugg::hir_with_applicability(cx, right_side, "..", &mut applicability);
span_lint_and_sugg(
@ -253,9 +253,9 @@ fn check_comparison<'a, 'tcx>(
h(left_side, right_side).to_string(),
applicability,
)
}
}),
_ => (),
}),
_ => (),
}
}
}
}

View file

@ -73,3 +73,39 @@ fn main() {
"no"
};
}
#[allow(dead_code)]
fn issue3703() {
struct Foo;
impl PartialEq<bool> for Foo {
fn eq(&self, _: &bool) -> bool {
true
}
}
impl PartialEq<Foo> for bool {
fn eq(&self, _: &Foo) -> bool {
true
}
}
impl PartialOrd<bool> for Foo {
fn partial_cmp(&self, _: &bool) -> Option<std::cmp::Ordering> {
None
}
}
impl PartialOrd<Foo> for bool {
fn partial_cmp(&self, _: &Foo) -> Option<std::cmp::Ordering> {
None
}
}
if Foo == true {}
if true == Foo {}
if Foo != true {}
if true != Foo {}
if Foo == false {}
if false == Foo {}
if Foo != false {}
if false != Foo {}
if Foo < false {}
if false < Foo {}
}