Skip to content

Commit fd516a0

Browse files
fix incorrect suggestion for !(a >= b) as i32 == c
1 parent a81f1c8 commit fd516a0

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

clippy_lints/src/booleans.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,18 @@ fn check_inverted_bool_in_condition(
176176
);
177177
}
178178

179-
fn check_simplify_not(cx: &LateContext<'_>, expr: &Expr<'_>) {
179+
fn check_simplify_not(cx: &LateContext<'_>, expr: &Expr<'_>, need_parens: bool) {
180180
if let ExprKind::Unary(UnOp::Not, inner) = &expr.kind
181181
&& !expr.span.from_expansion()
182182
&& !inner.span.from_expansion()
183183
&& let Some(suggestion) = simplify_not(cx, inner)
184184
&& cx.tcx.lint_level_at_node(NONMINIMAL_BOOL, expr.hir_id).0 != Level::Allow
185185
{
186+
let suggestion = if need_parens {
187+
format!("({})", suggestion)
188+
} else {
189+
suggestion
190+
};
186191
span_lint_and_sugg(
187192
cx,
188193
NONMINIMAL_BOOL,
@@ -476,7 +481,7 @@ fn terminal_stats(b: &Bool) -> Stats {
476481
}
477482

478483
impl<'a, 'tcx> NonminimalBoolVisitor<'a, 'tcx> {
479-
fn bool_expr(&self, e: &'tcx Expr<'_>) {
484+
fn bool_expr(&self, e: &'tcx Expr<'_>, need_parens: bool) {
480485
let mut h2q = Hir2Qmm {
481486
terminals: Vec::new(),
482487
cx: self.cx,
@@ -569,7 +574,7 @@ impl<'a, 'tcx> NonminimalBoolVisitor<'a, 'tcx> {
569574
}
570575
};
571576
if improvements.is_empty() {
572-
check_simplify_not(self.cx, e);
577+
check_simplify_not(self.cx, e, need_parens);
573578
} else {
574579
nonminimal_bool_lint(
575580
improvements
@@ -586,8 +591,15 @@ impl<'a, 'tcx> Visitor<'tcx> for NonminimalBoolVisitor<'a, 'tcx> {
586591
fn visit_expr(&mut self, e: &'tcx Expr<'_>) {
587592
if !e.span.from_expansion() {
588593
match &e.kind {
594+
ExprKind::Cast(inner, _) => {
595+
if self.cx.typeck_results().expr_ty(inner).is_bool() {
596+
// we are casting bool into some type, so we may need a parenthesis (#12761)
597+
self.bool_expr(inner, true);
598+
return;
599+
}
600+
},
589601
ExprKind::Binary(binop, _, _) if binop.node == BinOpKind::Or || binop.node == BinOpKind::And => {
590-
self.bool_expr(e);
602+
self.bool_expr(e, false);
591603
},
592604
ExprKind::Unary(UnOp::Not, inner) => {
593605
if let ExprKind::Unary(UnOp::Not, ex) = inner.kind
@@ -596,7 +608,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NonminimalBoolVisitor<'a, 'tcx> {
596608
return;
597609
}
598610
if self.cx.typeck_results().node_types()[inner.hir_id].is_bool() {
599-
self.bool_expr(e);
611+
self.bool_expr(e, false);
600612
}
601613
},
602614
_ => {},

tests/ui/nonminimal_bool_methods.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,11 @@ fn issue_12625() {
115115
if a as u64 > b {} //~ ERROR: this boolean expression can be simplified
116116
}
117117

118+
fn issue_12761() {
119+
let a = 0;
120+
let b = 0;
121+
let c = 0;
122+
if (a < b) as i32 == c {} //~ ERROR: this boolean expression can be simplified
123+
}
124+
118125
fn main() {}

tests/ui/nonminimal_bool_methods.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,11 @@ fn issue_12625() {
115115
if !(a as u64 <= b) {} //~ ERROR: this boolean expression can be simplified
116116
}
117117

118+
fn issue_12761() {
119+
let a = 0;
120+
let b = 0;
121+
let c = 0;
122+
if !(a >= b) as i32 == c {} //~ ERROR: this boolean expression can be simplified
123+
}
124+
118125
fn main() {}

tests/ui/nonminimal_bool_methods.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,11 @@ error: this boolean expression can be simplified
9797
LL | if !(a as u64 <= b) {}
9898
| ^^^^^^^^^^^^^^^^ help: try: `a as u64 > b`
9999

100-
error: aborting due to 16 previous errors
100+
error: this boolean expression can be simplified
101+
--> tests/ui/nonminimal_bool_methods.rs:122:8
102+
|
103+
LL | if !(a >= b) as i32 == c {}
104+
| ^^^^^^^^^ help: try: `(a < b)`
105+
106+
error: aborting due to 17 previous errors
101107

0 commit comments

Comments
 (0)