Skip to content

Commit 8939915

Browse files
authored
Use parentheses when needed in nonminimal_bool lint (#14187)
Since comparisons on types not implementing `Ord` (such as `f32`) are not inverted, they must be enclosed in parentheses when they are negated. Fix #14184 changelog: [`nonminimal_bool`]: add required parentheses when negating a binary expression
2 parents 521a800 + b32ad4c commit 8939915

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

clippy_lints/src/booleans.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then};
33
use clippy_utils::eq_expr_value;
44
use clippy_utils::msrvs::{self, Msrv};
55
use clippy_utils::source::SpanRangeExt;
6+
use clippy_utils::sugg::Sugg;
67
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
78
use rustc_ast::ast::LitKind;
89
use rustc_attr_parsing::RustcVersion;
@@ -353,7 +354,8 @@ impl SuggestContext<'_, '_, '_> {
353354
self.output.push_str(&str);
354355
} else {
355356
self.output.push('!');
356-
self.output.push_str(&terminal.span.get_source_text(self.cx)?);
357+
self.output
358+
.push_str(&Sugg::hir_opt(self.cx, terminal)?.maybe_par().to_string());
357359
}
358360
},
359361
True | False | Not(_) => {

tests/ui/nonminimal_bool.rs

+6
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,9 @@ fn issue_12371(x: usize) -> bool {
183183
fn many_ops(a: bool, b: bool, c: bool, d: bool, e: bool, f: bool) -> bool {
184184
(a && c && f) || (!a && b && !d) || (!b && !c && !e) || (d && e && !f)
185185
}
186+
187+
fn issue14184(a: f32, b: bool) {
188+
if !(a < 2.0 && !b) {
189+
println!("Hi");
190+
}
191+
}

tests/ui/nonminimal_bool.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -213,5 +213,11 @@ error: this boolean expression can be simplified
213213
LL | if !b != !c {}
214214
| ^^^^^^^^ help: try: `b != c`
215215

216-
error: aborting due to 29 previous errors
216+
error: this boolean expression can be simplified
217+
--> tests/ui/nonminimal_bool.rs:188:8
218+
|
219+
LL | if !(a < 2.0 && !b) {
220+
| ^^^^^^^^^^^^^^^^ help: try: `!(a < 2.0) || b`
221+
222+
error: aborting due to 30 previous errors
217223

0 commit comments

Comments
 (0)