Skip to content

Commit c0c0686

Browse files
committed
Fix bool_comparison with non-bool expressions
1 parent 50d9473 commit c0c0686

File tree

2 files changed

+56
-20
lines changed

2 files changed

+56
-20
lines changed

clippy_lints/src/needless_bool.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -225,23 +225,23 @@ fn check_comparison<'a, 'tcx>(
225225
use self::Expression::*;
226226

227227
if let ExprKind::Binary(_, ref left_side, ref right_side) = e.node {
228-
let mut applicability = Applicability::MachineApplicable;
229-
match (fetch_bool_expr(left_side), fetch_bool_expr(right_side)) {
230-
(Bool(true), Other) => left_true.map_or((), |(h, m)| {
231-
suggest_bool_comparison(cx, e, right_side, applicability, m, h)
232-
}),
233-
(Other, Bool(true)) => right_true.map_or((), |(h, m)| {
234-
suggest_bool_comparison(cx, e, left_side, applicability, m, h)
235-
}),
236-
(Bool(false), Other) => left_false.map_or((), |(h, m)| {
237-
suggest_bool_comparison(cx, e, right_side, applicability, m, h)
238-
}),
239-
(Other, Bool(false)) => right_false.map_or((), |(h, m)| {
240-
suggest_bool_comparison(cx, e, left_side, applicability, m, h)
241-
}),
242-
(Other, Other) => no_literal.map_or((), |(h, m)| {
243-
let (l_ty, r_ty) = (cx.tables.expr_ty(left_side), cx.tables.expr_ty(right_side));
244-
if l_ty.is_bool() && r_ty.is_bool() {
228+
let (l_ty, r_ty) = (cx.tables.expr_ty(left_side), cx.tables.expr_ty(right_side));
229+
if l_ty.is_bool() && r_ty.is_bool() {
230+
let mut applicability = Applicability::MachineApplicable;
231+
match (fetch_bool_expr(left_side), fetch_bool_expr(right_side)) {
232+
(Bool(true), Other) => left_true.map_or((), |(h, m)| {
233+
suggest_bool_comparison(cx, e, right_side, applicability, m, h)
234+
}),
235+
(Other, Bool(true)) => right_true.map_or((), |(h, m)| {
236+
suggest_bool_comparison(cx, e, left_side, applicability, m, h)
237+
}),
238+
(Bool(false), Other) => left_false.map_or((), |(h, m)| {
239+
suggest_bool_comparison(cx, e, right_side, applicability, m, h)
240+
}),
241+
(Other, Bool(false)) => right_false.map_or((), |(h, m)| {
242+
suggest_bool_comparison(cx, e, left_side, applicability, m, h)
243+
}),
244+
(Other, Other) => no_literal.map_or((), |(h, m)| {
245245
let left_side = Sugg::hir_with_applicability(cx, left_side, "..", &mut applicability);
246246
let right_side = Sugg::hir_with_applicability(cx, right_side, "..", &mut applicability);
247247
span_lint_and_sugg(
@@ -253,9 +253,9 @@ fn check_comparison<'a, 'tcx>(
253253
h(left_side, right_side).to_string(),
254254
applicability,
255255
)
256-
}
257-
}),
258-
_ => (),
256+
}),
257+
_ => (),
258+
}
259259
}
260260
}
261261
}

tests/ui/bool_comparison.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,39 @@ fn main() {
7373
"no"
7474
};
7575
}
76+
77+
#[allow(dead_code)]
78+
fn issue3703() {
79+
struct Foo;
80+
impl PartialEq<bool> for Foo {
81+
fn eq(&self, _: &bool) -> bool {
82+
true
83+
}
84+
}
85+
impl PartialEq<Foo> for bool {
86+
fn eq(&self, _: &Foo) -> bool {
87+
true
88+
}
89+
}
90+
impl PartialOrd<bool> for Foo {
91+
fn partial_cmp(&self, _: &bool) -> Option<std::cmp::Ordering> {
92+
None
93+
}
94+
}
95+
impl PartialOrd<Foo> for bool {
96+
fn partial_cmp(&self, _: &Foo) -> Option<std::cmp::Ordering> {
97+
None
98+
}
99+
}
100+
101+
if Foo == true {}
102+
if true == Foo {}
103+
if Foo != true {}
104+
if true != Foo {}
105+
if Foo == false {}
106+
if false == Foo {}
107+
if Foo != false {}
108+
if false != Foo {}
109+
if Foo < false {}
110+
if false < Foo {}
111+
}

0 commit comments

Comments
 (0)