mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 21:23:56 +00:00
Extend NONMINIMAL_BOOL
lint
This commit is contained in:
parent
9fb41079ca
commit
a18e0a11f7
3 changed files with 59 additions and 1 deletions
|
@ -85,6 +85,32 @@ impl<'tcx> LateLintPass<'tcx> for NonminimalBool {
|
|||
) {
|
||||
NonminimalBoolVisitor { cx }.visit_body(body);
|
||||
}
|
||||
|
||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
|
||||
if let ExprKind::Unary(UnOp::Not, sub) = expr.kind
|
||||
&& !expr.span.from_expansion()
|
||||
&& let ExprKind::Binary(op, left, right) = sub.kind
|
||||
{
|
||||
let new_op = match op.node {
|
||||
BinOpKind::Eq => "!=",
|
||||
BinOpKind::Ne => "==",
|
||||
_ => return,
|
||||
};
|
||||
let Some(left) = snippet_opt(cx, left.span) else { return };
|
||||
let Some(right) = snippet_opt(cx, right.span) else {
|
||||
return;
|
||||
};
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
NONMINIMAL_BOOL,
|
||||
expr.span,
|
||||
"this boolean expression can be simplified",
|
||||
"try",
|
||||
format!("{left} {new_op} {right}"),
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
struct NonminimalBoolVisitor<'a, 'tcx> {
|
||||
cx: &'a LateContext<'tcx>,
|
||||
|
|
|
@ -156,3 +156,11 @@ fn issue11932() {
|
|||
x % 3 == 0
|
||||
};
|
||||
}
|
||||
|
||||
fn issue_5794() {
|
||||
let a = 0;
|
||||
if !(12 == a) {} //~ ERROR: this boolean expression can be simplified
|
||||
if !(a == 12) {} //~ ERROR: this boolean expression can be simplified
|
||||
if !(12 != a) {} //~ ERROR: this boolean expression can be simplified
|
||||
if !(a != 12) {} //~ ERROR: this boolean expression can be simplified
|
||||
}
|
||||
|
|
|
@ -114,5 +114,29 @@ error: this boolean expression can be simplified
|
|||
LL | if matches!(true, true) && true {
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `matches!(true, true)`
|
||||
|
||||
error: aborting due to 13 previous errors
|
||||
error: this boolean expression can be simplified
|
||||
--> $DIR/nonminimal_bool.rs:162:8
|
||||
|
|
||||
LL | if !(12 == a) {}
|
||||
| ^^^^^^^^^^ help: try: `12 != a`
|
||||
|
||||
error: this boolean expression can be simplified
|
||||
--> $DIR/nonminimal_bool.rs:163:8
|
||||
|
|
||||
LL | if !(a == 12) {}
|
||||
| ^^^^^^^^^^ help: try: `a != 12`
|
||||
|
||||
error: this boolean expression can be simplified
|
||||
--> $DIR/nonminimal_bool.rs:164:8
|
||||
|
|
||||
LL | if !(12 != a) {}
|
||||
| ^^^^^^^^^^ help: try: `12 == a`
|
||||
|
||||
error: this boolean expression can be simplified
|
||||
--> $DIR/nonminimal_bool.rs:165:8
|
||||
|
|
||||
LL | if !(a != 12) {}
|
||||
| ^^^^^^^^^^ help: try: `a == 12`
|
||||
|
||||
error: aborting due to 17 previous errors
|
||||
|
||||
|
|
Loading…
Reference in a new issue