Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 3d3af07

Browse files
committed
Provide appropriate suggestion
1 parent 12796cd commit 3d3af07

File tree

4 files changed

+42
-24
lines changed

4 files changed

+42
-24
lines changed

clippy_lints/src/needless_bool.rs

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! This lint is **warn** by default
44
55
use crate::utils::sugg::Sugg;
6-
use crate::utils::{higher, parent_node_is_if_expr, span_lint, span_lint_and_help, span_lint_and_sugg};
6+
use crate::utils::{higher, parent_node_is_if_expr, span_lint, span_lint_and_help, span_lint_and_sugg, snippet_with_applicability};
77
use if_chain::if_chain;
88
use rustc_ast::ast::LitKind;
99
use rustc_errors::Applicability;
@@ -189,19 +189,23 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoolComparison {
189189
}
190190
}
191191

192-
fn is_unary_not<'tcx>(e: &'tcx Expr<'_>) -> bool {
192+
fn is_unary_not<'tcx>(e: &'tcx Expr<'_>) -> (bool, rustc_span::Span) {
193193
if_chain! {
194-
if let ExprKind::Unary(unop, _) = e.kind;
194+
if let ExprKind::Unary(unop, operand) = e.kind;
195195
if let UnOp::UnNot = unop;
196196
then {
197-
return true;
197+
return (true, operand.span);
198198
}
199199
};
200-
false
200+
(false, e.span)
201201
}
202202

203-
fn one_side_is_unary_not<'tcx>(left_side: &'tcx Expr<'_>, right_side: &'tcx Expr<'_>) -> bool {
204-
is_unary_not(left_side) ^ is_unary_not(right_side)
203+
fn one_side_is_unary_not<'tcx>(left_side: &'tcx Expr<'_>, right_side: &'tcx Expr<'_>) -> (bool, rustc_span::Span, rustc_span::Span) {
204+
let left = is_unary_not(left_side);
205+
let right = is_unary_not(right_side);
206+
207+
let retval = left.0 ^ right.0;
208+
(retval, left.1, right.1)
205209
}
206210

207211
fn check_comparison<'a, 'tcx>(
@@ -218,14 +222,27 @@ fn check_comparison<'a, 'tcx>(
218222
if let ExprKind::Binary(op, ref left_side, ref right_side) = e.kind {
219223
let (l_ty, r_ty) = (cx.tables.expr_ty(left_side), cx.tables.expr_ty(right_side));
220224
if l_ty.is_bool() && r_ty.is_bool() {
221-
if_chain! {
222-
if let BinOpKind::Eq = op.node;
223-
if one_side_is_unary_not(&left_side, &right_side);
224-
then {
225-
span_lint_and_help(cx, BOOL_COMPARISON, e.span, "Here comes", "the suggestion");
226-
}
227-
};
228225
let mut applicability = Applicability::MachineApplicable;
226+
227+
if let BinOpKind::Eq = op.node
228+
{
229+
let xxx = one_side_is_unary_not(&left_side, &right_side);
230+
if xxx.0
231+
{
232+
span_lint_and_sugg(
233+
cx,
234+
BOOL_COMPARISON,
235+
e.span,
236+
"This comparison might be written more concisely",
237+
"try simplifying it as shown",
238+
format!("{} != {}",
239+
snippet_with_applicability(cx, xxx.1, "..", &mut applicability),
240+
snippet_with_applicability(cx, xxx.2, "..", &mut applicability)),
241+
applicability,
242+
)
243+
}
244+
}
245+
229246
match (fetch_bool_expr(left_side), fetch_bool_expr(right_side)) {
230247
(Bool(true), Other) => left_true.map_or((), |(h, m)| {
231248
suggest_bool_comparison(cx, e, right_side, applicability, m, h)

tests/ui/bool_comparison.fixed

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ fn issue4983() {
116116
let a = true;
117117
let b = false;
118118

119-
if a == !b {};
120-
if !a == b {};
119+
if a != b {};
120+
if a != b {};
121121
if a == b {};
122122
if !a == !b {};
123123
}

tests/ui/bool_comparison.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,9 @@ fn issue4983() {
120120
if !a == b {};
121121
if a == b {};
122122
if !a == !b {};
123+
124+
if b == !a {};
125+
if !b == a {};
126+
if b == a {};
127+
if !b == !a {};
123128
}

tests/ui/bool_comparison.stderr

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,17 @@ error: order comparisons between booleans can be simplified
8484
LL | if x > y {
8585
| ^^^^^ help: try simplifying it as shown: `x & !y`
8686

87-
error: Here comes
87+
error: This comparison might be written more concisely
8888
--> $DIR/bool_comparison.rs:119:8
8989
|
9090
LL | if a == !b {};
91-
| ^^^^^^^
92-
|
93-
= help: the suggestion
91+
| ^^^^^^^ help: try simplifying it as shown: `a != b`
9492

95-
error: Here comes
93+
error: This comparison might be written more concisely
9694
--> $DIR/bool_comparison.rs:120:8
9795
|
9896
LL | if !a == b {};
99-
| ^^^^^^^
100-
|
101-
= help: the suggestion
97+
| ^^^^^^^ help: try simplifying it as shown: `a != b`
10298

10399
error: aborting due to 16 previous errors
104100

0 commit comments

Comments
 (0)