2022-06-30 10:50:09 +02:00
|
|
|
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then};
|
2022-10-23 15:18:45 +02:00
|
|
|
use clippy_utils::eq_expr_value;
|
2021-03-25 19:29:11 +01:00
|
|
|
use clippy_utils::source::snippet_opt;
|
|
|
|
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
|
2020-03-01 12:23:33 +09:00
|
|
|
use rustc_ast::ast::LitKind;
|
2018-12-29 16:04:45 +01:00
|
|
|
use rustc_errors::Applicability;
|
2022-01-15 16:07:52 -06:00
|
|
|
use rustc_hir::intravisit::{walk_expr, FnKind, Visitor};
|
2023-01-22 18:00:33 +00:00
|
|
|
use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, UnOp};
|
2023-04-11 15:31:08 +02:00
|
|
|
use rustc_lint::{LateContext, LateLintPass, Level};
|
2023-11-25 17:45:27 +00:00
|
|
|
use rustc_session::declare_lint_pass;
|
2023-01-22 18:00:33 +00:00
|
|
|
use rustc_span::def_id::LocalDefId;
|
2023-11-02 14:10:12 +11:00
|
|
|
use rustc_span::{sym, Span};
|
2016-03-23 12:19:13 +01:00
|
|
|
|
2018-03-28 15:24:26 +02:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 12:16:06 +02:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for boolean expressions that can be written more
|
2019-03-05 11:50:33 -05:00
|
|
|
/// concisely.
|
|
|
|
///
|
2021-07-29 12:16:06 +02:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Readability of boolean expressions suffers from
|
2019-03-05 11:50:33 -05:00
|
|
|
/// unnecessary duplication.
|
|
|
|
///
|
2021-07-29 12:16:06 +02:00
|
|
|
/// ### Known problems
|
|
|
|
/// Ignores short circuiting behavior of `||` and
|
2019-03-05 11:50:33 -05:00
|
|
|
/// `&&`. Ignores `|`, `&` and `^`.
|
|
|
|
///
|
2021-07-29 12:16:06 +02:00
|
|
|
/// ### Example
|
2019-03-05 17:23:50 -05:00
|
|
|
/// ```ignore
|
2022-06-04 13:34:07 +02:00
|
|
|
/// if a && true {}
|
|
|
|
/// if !(a == b) {}
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Use instead:
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// if a {}
|
|
|
|
/// if a != b {}
|
2019-03-05 11:50:33 -05:00
|
|
|
/// ```
|
2021-12-06 12:33:31 +01:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2016-08-06 10:18:36 +02:00
|
|
|
pub NONMINIMAL_BOOL,
|
2018-03-28 15:24:26 +02:00
|
|
|
complexity,
|
2016-08-06 10:18:36 +02:00
|
|
|
"boolean expressions that can be written more concisely"
|
2016-03-23 12:19:13 +01:00
|
|
|
}
|
|
|
|
|
2018-03-28 15:24:26 +02:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 12:16:06 +02:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for boolean expressions that contain terminals that
|
2019-03-05 11:50:33 -05:00
|
|
|
/// can be eliminated.
|
|
|
|
///
|
2021-07-29 12:16:06 +02:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// This is most likely a logic bug.
|
2019-03-05 11:50:33 -05:00
|
|
|
///
|
2021-07-29 12:16:06 +02:00
|
|
|
/// ### Known problems
|
|
|
|
/// Ignores short circuiting behavior.
|
2019-03-05 11:50:33 -05:00
|
|
|
///
|
2021-07-29 12:16:06 +02:00
|
|
|
/// ### Example
|
2022-06-04 13:34:07 +02:00
|
|
|
/// ```rust,ignore
|
|
|
|
/// // The `b` is unnecessary, the expression is equivalent to `if a`.
|
2019-03-05 11:50:33 -05:00
|
|
|
/// if a && b || a { ... }
|
|
|
|
/// ```
|
2022-06-04 13:34:07 +02:00
|
|
|
///
|
|
|
|
/// Use instead:
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// if a {}
|
|
|
|
/// ```
|
2021-12-06 12:33:31 +01:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2022-08-11 19:42:16 +02:00
|
|
|
pub OVERLY_COMPLEX_BOOL_EXPR,
|
2018-03-28 15:24:26 +02:00
|
|
|
correctness,
|
2016-08-06 10:18:36 +02:00
|
|
|
"boolean expressions that contain terminals which can be eliminated"
|
2016-03-24 10:45:24 +01:00
|
|
|
}
|
|
|
|
|
2017-11-10 19:55:15 +00:00
|
|
|
// For each pairs, both orders are considered.
|
2019-05-18 00:58:25 +02:00
|
|
|
const METHODS_WITH_NEGATION: [(&str, &str); 2] = [("is_some", "is_none"), ("is_err", "is_ok")];
|
2017-11-07 21:43:24 +00:00
|
|
|
|
2022-08-11 19:42:16 +02:00
|
|
|
declare_lint_pass!(NonminimalBool => [NONMINIMAL_BOOL, OVERLY_COMPLEX_BOOL_EXPR]);
|
2016-03-23 12:19:13 +01:00
|
|
|
|
2020-06-25 23:41:36 +03:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for NonminimalBool {
|
2017-01-13 17:04:56 +01:00
|
|
|
fn check_fn(
|
|
|
|
&mut self,
|
2020-06-25 23:41:36 +03:00
|
|
|
cx: &LateContext<'tcx>,
|
2020-02-18 22:28:18 +09:00
|
|
|
_: FnKind<'tcx>,
|
2019-12-30 13:02:10 +09:00
|
|
|
_: &'tcx FnDecl<'_>,
|
2019-12-22 15:42:41 +01:00
|
|
|
body: &'tcx Body<'_>,
|
2017-01-13 17:04:56 +01:00
|
|
|
_: Span,
|
2023-01-22 18:00:33 +00:00
|
|
|
_: LocalDefId,
|
2017-01-13 17:04:56 +01:00
|
|
|
) {
|
2021-06-03 08:41:37 +02:00
|
|
|
NonminimalBoolVisitor { cx }.visit_body(body);
|
2016-03-23 12:19:13 +01:00
|
|
|
}
|
2024-02-08 22:36:41 +01:00
|
|
|
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
|
2024-02-09 20:47:31 +01:00
|
|
|
match expr.kind {
|
|
|
|
ExprKind::Unary(UnOp::Not, sub) => check_inverted_condition(cx, expr.span, sub),
|
|
|
|
// This check the case where an element in a boolean comparison is inverted, like:
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// let a = true;
|
|
|
|
// !a == false;
|
|
|
|
// ```
|
|
|
|
ExprKind::Binary(op, left, right) if matches!(op.node, BinOpKind::Eq | BinOpKind::Ne) => {
|
|
|
|
check_inverted_bool_in_condition(cx, expr.span, op.node, left, right);
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inverted_bin_op_eq_str(op: BinOpKind) -> Option<&'static str> {
|
|
|
|
match op {
|
|
|
|
BinOpKind::Eq => Some("!="),
|
|
|
|
BinOpKind::Ne => Some("=="),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bin_op_eq_str(op: BinOpKind) -> Option<&'static str> {
|
|
|
|
match op {
|
|
|
|
BinOpKind::Eq => Some("=="),
|
|
|
|
BinOpKind::Ne => Some("!="),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_inverted_condition(cx: &LateContext<'_>, expr_span: Span, sub_expr: &Expr<'_>) {
|
|
|
|
if !expr_span.from_expansion()
|
|
|
|
&& let ExprKind::Binary(op, left, right) = sub_expr.kind
|
|
|
|
&& let Some(left) = snippet_opt(cx, left.span)
|
|
|
|
&& let Some(right) = snippet_opt(cx, right.span)
|
|
|
|
{
|
|
|
|
let Some(op) = inverted_bin_op_eq_str(op.node) else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
NONMINIMAL_BOOL,
|
|
|
|
expr_span,
|
|
|
|
"this boolean expression can be simplified",
|
|
|
|
"try",
|
|
|
|
format!("{left} {op} {right}",),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_inverted_bool_in_condition(
|
|
|
|
cx: &LateContext<'_>,
|
|
|
|
expr_span: Span,
|
|
|
|
op: BinOpKind,
|
|
|
|
left: &Expr<'_>,
|
|
|
|
right: &Expr<'_>,
|
|
|
|
) {
|
|
|
|
if expr_span.from_expansion()
|
|
|
|
&& (!cx.typeck_results().node_types()[left.hir_id].is_bool()
|
|
|
|
|| !cx.typeck_results().node_types()[right.hir_id].is_bool())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let suggestion = match (left.kind, right.kind) {
|
|
|
|
(ExprKind::Unary(UnOp::Not, left_sub), ExprKind::Unary(UnOp::Not, right_sub)) => {
|
|
|
|
let Some(left) = snippet_opt(cx, left_sub.span) else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
let Some(right) = snippet_opt(cx, right_sub.span) else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
let Some(op) = bin_op_eq_str(op) else { return };
|
|
|
|
format!("{left} {op} {right}")
|
|
|
|
},
|
|
|
|
(ExprKind::Unary(UnOp::Not, left_sub), _) => {
|
|
|
|
let Some(left) = snippet_opt(cx, left_sub.span) else {
|
|
|
|
return;
|
2024-02-08 22:36:41 +01:00
|
|
|
};
|
|
|
|
let Some(right) = snippet_opt(cx, right.span) else {
|
|
|
|
return;
|
|
|
|
};
|
2024-02-09 20:47:31 +01:00
|
|
|
let Some(op) = inverted_bin_op_eq_str(op) else { return };
|
|
|
|
format!("{left} {op} {right}")
|
|
|
|
},
|
|
|
|
(_, ExprKind::Unary(UnOp::Not, right_sub)) => {
|
|
|
|
let Some(left) = snippet_opt(cx, left.span) else { return };
|
|
|
|
let Some(right) = snippet_opt(cx, right_sub.span) else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
let Some(op) = inverted_bin_op_eq_str(op) else { return };
|
|
|
|
format!("{left} {op} {right}")
|
|
|
|
},
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
NONMINIMAL_BOOL,
|
|
|
|
expr_span,
|
|
|
|
"this boolean expression can be simplified",
|
|
|
|
"try",
|
|
|
|
suggestion,
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
2016-03-23 12:19:13 +01:00
|
|
|
}
|
2024-02-09 20:47:31 +01:00
|
|
|
|
2019-06-20 01:36:23 +07:00
|
|
|
struct NonminimalBoolVisitor<'a, 'tcx> {
|
2020-06-25 23:41:36 +03:00
|
|
|
cx: &'a LateContext<'tcx>,
|
2016-12-06 11:32:21 +01:00
|
|
|
}
|
2016-03-23 12:19:13 +01:00
|
|
|
|
|
|
|
use quine_mc_cluskey::Bool;
|
2019-06-20 01:36:23 +07:00
|
|
|
struct Hir2Qmm<'a, 'tcx, 'v> {
|
2019-12-27 16:12:26 +09:00
|
|
|
terminals: Vec<&'v Expr<'v>>,
|
2020-06-25 23:41:36 +03:00
|
|
|
cx: &'a LateContext<'tcx>,
|
2016-03-23 14:50:08 +01:00
|
|
|
}
|
2016-03-23 12:19:13 +01:00
|
|
|
|
2016-03-23 14:50:08 +01:00
|
|
|
impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> {
|
2019-12-27 16:12:26 +09:00
|
|
|
fn extract(&mut self, op: BinOpKind, a: &[&'v Expr<'_>], mut v: Vec<Bool>) -> Result<Vec<Bool>, String> {
|
2016-03-23 12:19:13 +01:00
|
|
|
for a in a {
|
2019-09-27 17:16:06 +02:00
|
|
|
if let ExprKind::Binary(binop, lhs, rhs) = &a.kind {
|
2016-03-23 12:19:13 +01:00
|
|
|
if binop.node == op {
|
|
|
|
v = self.extract(op, &[lhs, rhs], v)?;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v.push(self.run(a)?);
|
|
|
|
}
|
|
|
|
Ok(v)
|
|
|
|
}
|
|
|
|
|
2019-12-27 16:12:26 +09:00
|
|
|
fn run(&mut self, e: &'v Expr<'_>) -> Result<Bool, String> {
|
2019-06-21 08:14:07 +02:00
|
|
|
fn negate(bin_op_kind: BinOpKind) -> Option<BinOpKind> {
|
|
|
|
match bin_op_kind {
|
|
|
|
BinOpKind::Eq => Some(BinOpKind::Ne),
|
|
|
|
BinOpKind::Ne => Some(BinOpKind::Eq),
|
|
|
|
BinOpKind::Gt => Some(BinOpKind::Le),
|
|
|
|
BinOpKind::Ge => Some(BinOpKind::Lt),
|
|
|
|
BinOpKind::Lt => Some(BinOpKind::Ge),
|
|
|
|
BinOpKind::Le => Some(BinOpKind::Gt),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 09:37:16 +01:00
|
|
|
// prevent folding of `cfg!` macros and the like
|
2019-08-19 09:30:32 -07:00
|
|
|
if !e.span.from_expansion() {
|
2019-09-27 17:16:06 +02:00
|
|
|
match &e.kind {
|
2021-08-06 17:14:27 +02:00
|
|
|
ExprKind::Unary(UnOp::Not, inner) => return Ok(Bool::Not(Box::new(self.run(inner)?))),
|
2018-12-29 17:34:56 +01:00
|
|
|
ExprKind::Binary(binop, lhs, rhs) => match &binop.node {
|
2020-07-17 08:47:04 +00:00
|
|
|
BinOpKind::Or => {
|
|
|
|
return Ok(Bool::Or(self.extract(BinOpKind::Or, &[lhs, rhs], Vec::new())?));
|
|
|
|
},
|
|
|
|
BinOpKind::And => {
|
|
|
|
return Ok(Bool::And(self.extract(BinOpKind::And, &[lhs, rhs], Vec::new())?));
|
|
|
|
},
|
2017-09-05 11:33:04 +02:00
|
|
|
_ => (),
|
2016-12-20 18:21:30 +01:00
|
|
|
},
|
2018-12-29 17:34:56 +01:00
|
|
|
ExprKind::Lit(lit) => match lit.node {
|
2017-09-05 11:33:04 +02:00
|
|
|
LitKind::Bool(true) => return Ok(Bool::True),
|
|
|
|
LitKind::Bool(false) => return Ok(Bool::False),
|
|
|
|
_ => (),
|
2016-12-20 18:21:30 +01:00
|
|
|
},
|
2016-04-14 20:14:03 +02:00
|
|
|
_ => (),
|
2016-03-24 09:37:16 +01:00
|
|
|
}
|
2016-03-23 12:19:13 +01:00
|
|
|
}
|
2016-03-24 16:02:26 +01:00
|
|
|
for (n, expr) in self.terminals.iter().enumerate() {
|
2020-08-28 16:10:16 +02:00
|
|
|
if eq_expr_value(self.cx, e, expr) {
|
2022-05-21 13:24:00 +02:00
|
|
|
#[expect(clippy::cast_possible_truncation)]
|
2017-09-05 11:33:04 +02:00
|
|
|
return Ok(Bool::Term(n as u8));
|
2016-03-24 16:02:26 +01:00
|
|
|
}
|
2018-06-01 15:13:53 +02:00
|
|
|
|
2023-11-16 19:13:24 +01:00
|
|
|
if let ExprKind::Binary(e_binop, e_lhs, e_rhs) = &e.kind
|
|
|
|
&& implements_ord(self.cx, e_lhs)
|
|
|
|
&& let ExprKind::Binary(expr_binop, expr_lhs, expr_rhs) = &expr.kind
|
|
|
|
&& negate(e_binop.node) == Some(expr_binop.node)
|
|
|
|
&& eq_expr_value(self.cx, e_lhs, expr_lhs)
|
|
|
|
&& eq_expr_value(self.cx, e_rhs, expr_rhs)
|
|
|
|
{
|
|
|
|
#[expect(clippy::cast_possible_truncation)]
|
|
|
|
return Ok(Bool::Not(Box::new(Bool::Term(n as u8))));
|
2016-03-24 16:29:20 +01:00
|
|
|
}
|
2016-03-23 14:50:08 +01:00
|
|
|
}
|
|
|
|
let n = self.terminals.len();
|
|
|
|
self.terminals.push(e);
|
2016-03-23 12:19:13 +01:00
|
|
|
if n < 32 {
|
2022-05-21 13:24:00 +02:00
|
|
|
#[expect(clippy::cast_possible_truncation)]
|
2017-09-05 11:33:04 +02:00
|
|
|
Ok(Bool::Term(n as u8))
|
2016-03-23 12:19:13 +01:00
|
|
|
} else {
|
|
|
|
Err("too many literals".to_owned())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-20 01:36:23 +07:00
|
|
|
struct SuggestContext<'a, 'tcx, 'v> {
|
2019-12-27 16:12:26 +09:00
|
|
|
terminals: &'v [&'v Expr<'v>],
|
2020-06-25 23:41:36 +03:00
|
|
|
cx: &'a LateContext<'tcx>,
|
2017-11-17 21:18:32 +00:00
|
|
|
output: String,
|
2017-11-16 21:08:08 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 21:18:32 +00:00
|
|
|
impl<'a, 'tcx, 'v> SuggestContext<'a, 'tcx, 'v> {
|
2017-11-29 16:03:05 +01:00
|
|
|
fn recurse(&mut self, suggestion: &Bool) -> Option<()> {
|
2020-02-21 09:39:38 +01:00
|
|
|
use quine_mc_cluskey::Bool::{And, False, Not, Or, Term, True};
|
2018-12-29 17:34:56 +01:00
|
|
|
match suggestion {
|
2016-03-23 12:19:13 +01:00
|
|
|
True => {
|
2017-11-17 21:18:32 +00:00
|
|
|
self.output.push_str("true");
|
2016-12-20 18:21:30 +01:00
|
|
|
},
|
2016-03-23 12:19:13 +01:00
|
|
|
False => {
|
2017-11-17 21:18:32 +00:00
|
|
|
self.output.push_str("false");
|
2016-12-20 18:21:30 +01:00
|
|
|
},
|
2018-12-29 17:34:56 +01:00
|
|
|
Not(inner) => match **inner {
|
2017-09-05 11:33:04 +02:00
|
|
|
And(_) | Or(_) => {
|
2017-11-17 21:18:32 +00:00
|
|
|
self.output.push('!');
|
2017-11-17 21:42:25 +00:00
|
|
|
self.output.push('(');
|
|
|
|
self.recurse(inner);
|
|
|
|
self.output.push(')');
|
2017-09-05 11:33:04 +02:00
|
|
|
},
|
2017-11-16 21:08:08 +00:00
|
|
|
Term(n) => {
|
2017-11-17 21:42:25 +00:00
|
|
|
let terminal = self.terminals[n as usize];
|
2019-09-24 08:06:58 +02:00
|
|
|
if let Some(str) = simplify_not(self.cx, terminal) {
|
2021-06-03 08:41:37 +02:00
|
|
|
self.output.push_str(&str);
|
2017-11-16 21:08:08 +00:00
|
|
|
} else {
|
2017-11-17 21:18:32 +00:00
|
|
|
self.output.push('!');
|
2019-09-25 06:20:40 +02:00
|
|
|
let snip = snippet_opt(self.cx, terminal.span)?;
|
2017-11-17 21:52:11 +00:00
|
|
|
self.output.push_str(&snip);
|
2017-11-16 21:08:08 +00:00
|
|
|
}
|
2017-09-05 11:33:04 +02:00
|
|
|
},
|
2017-11-16 21:08:08 +00:00
|
|
|
True | False | Not(_) => {
|
2017-11-17 21:18:32 +00:00
|
|
|
self.output.push('!');
|
2017-11-29 16:03:05 +01:00
|
|
|
self.recurse(inner)?;
|
2017-09-05 11:33:04 +02:00
|
|
|
},
|
2016-12-20 18:21:30 +01:00
|
|
|
},
|
2018-12-29 17:34:56 +01:00
|
|
|
And(v) => {
|
2017-11-16 21:08:08 +00:00
|
|
|
for (index, inner) in v.iter().enumerate() {
|
|
|
|
if index > 0 {
|
2017-11-17 21:18:32 +00:00
|
|
|
self.output.push_str(" && ");
|
2017-11-16 21:08:08 +00:00
|
|
|
}
|
2016-03-29 17:18:47 +02:00
|
|
|
if let Or(_) = *inner {
|
2017-11-17 21:42:25 +00:00
|
|
|
self.output.push('(');
|
|
|
|
self.recurse(inner);
|
|
|
|
self.output.push(')');
|
2016-03-29 17:18:47 +02:00
|
|
|
} else {
|
2017-11-17 21:42:25 +00:00
|
|
|
self.recurse(inner);
|
2016-03-29 17:18:47 +02:00
|
|
|
}
|
2016-03-23 12:49:16 +01:00
|
|
|
}
|
2016-12-20 18:21:30 +01:00
|
|
|
},
|
2018-12-29 17:34:56 +01:00
|
|
|
Or(v) => {
|
2020-01-14 07:08:45 +09:00
|
|
|
for (index, inner) in v.iter().rev().enumerate() {
|
2017-11-16 21:08:08 +00:00
|
|
|
if index > 0 {
|
2017-11-17 21:18:32 +00:00
|
|
|
self.output.push_str(" || ");
|
2017-11-16 21:08:08 +00:00
|
|
|
}
|
2017-11-17 21:42:25 +00:00
|
|
|
self.recurse(inner);
|
2016-03-23 12:19:13 +01:00
|
|
|
}
|
2016-12-20 18:21:30 +01:00
|
|
|
},
|
2018-12-29 17:34:56 +01:00
|
|
|
&Term(n) => {
|
2022-09-21 13:02:37 -04:00
|
|
|
let snip = snippet_opt(self.cx, self.terminals[n as usize].span.source_callsite())?;
|
2017-11-17 21:18:32 +00:00
|
|
|
self.output.push_str(&snip);
|
2016-12-20 18:21:30 +01:00
|
|
|
},
|
2016-03-23 12:19:13 +01:00
|
|
|
}
|
2017-11-29 16:03:05 +01:00
|
|
|
Some(())
|
2016-03-23 12:19:13 +01:00
|
|
|
}
|
2017-11-17 21:18:32 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 23:41:36 +03:00
|
|
|
fn simplify_not(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<String> {
|
2019-09-27 17:16:06 +02:00
|
|
|
match &expr.kind {
|
2019-09-24 08:06:58 +02:00
|
|
|
ExprKind::Binary(binop, lhs, rhs) => {
|
|
|
|
if !implements_ord(cx, lhs) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
match binop.node {
|
|
|
|
BinOpKind::Eq => Some(" != "),
|
|
|
|
BinOpKind::Ne => Some(" == "),
|
|
|
|
BinOpKind::Lt => Some(" >= "),
|
|
|
|
BinOpKind::Gt => Some(" <= "),
|
|
|
|
BinOpKind::Le => Some(" > "),
|
|
|
|
BinOpKind::Ge => Some(" < "),
|
|
|
|
_ => None,
|
|
|
|
}
|
2019-09-25 06:20:40 +02:00
|
|
|
.and_then(|op| {
|
|
|
|
Some(format!(
|
2022-10-06 09:44:38 +02:00
|
|
|
"{}{op}{}",
|
2019-09-25 06:20:40 +02:00
|
|
|
snippet_opt(cx, lhs.span)?,
|
|
|
|
snippet_opt(cx, rhs.span)?
|
|
|
|
))
|
|
|
|
})
|
2019-09-24 08:06:58 +02:00
|
|
|
},
|
2022-09-01 18:43:35 +09:00
|
|
|
ExprKind::MethodCall(path, receiver, [], _) => {
|
|
|
|
let type_of_receiver = cx.typeck_results().expr_ty(receiver);
|
2021-10-02 18:51:01 -05:00
|
|
|
if !is_type_diagnostic_item(cx, type_of_receiver, sym::Option)
|
|
|
|
&& !is_type_diagnostic_item(cx, type_of_receiver, sym::Result)
|
2020-04-12 15:23:54 +02:00
|
|
|
{
|
2019-09-24 08:06:58 +02:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
METHODS_WITH_NEGATION
|
|
|
|
.iter()
|
2021-04-22 11:31:13 +02:00
|
|
|
.copied()
|
2019-09-24 08:06:58 +02:00
|
|
|
.flat_map(|(a, b)| vec![(a, b), (b, a)])
|
2019-10-15 12:29:28 -07:00
|
|
|
.find(|&(a, _)| {
|
2021-12-15 14:39:23 +11:00
|
|
|
let path: &str = path.ident.name.as_str();
|
2019-10-15 12:29:28 -07:00
|
|
|
a == path
|
|
|
|
})
|
2022-10-06 09:44:38 +02:00
|
|
|
.and_then(|(_, neg_method)| Some(format!("{}.{neg_method}()", snippet_opt(cx, receiver.span)?)))
|
2019-09-24 08:06:58 +02:00
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 23:41:36 +03:00
|
|
|
fn suggest(cx: &LateContext<'_>, suggestion: &Bool, terminals: &[&Expr<'_>]) -> String {
|
2017-11-17 21:18:32 +00:00
|
|
|
let mut suggest_context = SuggestContext {
|
2018-03-15 16:07:15 +01:00
|
|
|
terminals,
|
|
|
|
cx,
|
2017-11-17 21:18:32 +00:00
|
|
|
output: String::new(),
|
|
|
|
};
|
2017-11-17 21:42:25 +00:00
|
|
|
suggest_context.recurse(suggestion);
|
2019-09-27 07:59:34 +02:00
|
|
|
suggest_context.output
|
2016-03-23 12:19:13 +01:00
|
|
|
}
|
|
|
|
|
2016-03-23 12:52:17 +01:00
|
|
|
fn simple_negate(b: Bool) -> Bool {
|
2020-02-21 09:39:38 +01:00
|
|
|
use quine_mc_cluskey::Bool::{And, False, Not, Or, Term, True};
|
2016-03-23 12:52:17 +01:00
|
|
|
match b {
|
|
|
|
True => False,
|
|
|
|
False => True,
|
|
|
|
t @ Term(_) => Not(Box::new(t)),
|
|
|
|
And(mut v) => {
|
|
|
|
for el in &mut v {
|
|
|
|
*el = simple_negate(::std::mem::replace(el, True));
|
|
|
|
}
|
|
|
|
Or(v)
|
2016-12-20 18:21:30 +01:00
|
|
|
},
|
2016-03-23 12:52:17 +01:00
|
|
|
Or(mut v) => {
|
|
|
|
for el in &mut v {
|
|
|
|
*el = simple_negate(::std::mem::replace(el, True));
|
|
|
|
}
|
|
|
|
And(v)
|
2016-12-20 18:21:30 +01:00
|
|
|
},
|
2016-03-23 12:52:17 +01:00
|
|
|
Not(inner) => *inner,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 15:37:17 +01:00
|
|
|
#[derive(Default)]
|
|
|
|
struct Stats {
|
|
|
|
terminals: [usize; 32],
|
|
|
|
negations: usize,
|
|
|
|
ops: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn terminal_stats(b: &Bool) -> Stats {
|
|
|
|
fn recurse(b: &Bool, stats: &mut Stats) {
|
2018-12-29 17:34:56 +01:00
|
|
|
match b {
|
2016-03-24 15:37:17 +01:00
|
|
|
True | False => stats.ops += 1,
|
2018-12-29 17:34:56 +01:00
|
|
|
Not(inner) => {
|
2016-03-24 15:37:17 +01:00
|
|
|
match **inner {
|
|
|
|
And(_) | Or(_) => stats.ops += 1, // brackets are also operations
|
|
|
|
_ => stats.negations += 1,
|
|
|
|
}
|
|
|
|
recurse(inner, stats);
|
2016-12-20 18:21:30 +01:00
|
|
|
},
|
2018-12-29 17:34:56 +01:00
|
|
|
And(v) | Or(v) => {
|
2016-03-24 15:37:17 +01:00
|
|
|
stats.ops += v.len() - 1;
|
|
|
|
for inner in v {
|
|
|
|
recurse(inner, stats);
|
|
|
|
}
|
2016-12-20 18:21:30 +01:00
|
|
|
},
|
2018-12-29 17:34:56 +01:00
|
|
|
&Term(n) => stats.terminals[n as usize] += 1,
|
2016-03-24 10:45:24 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-21 09:39:38 +01:00
|
|
|
use quine_mc_cluskey::Bool::{And, False, Not, Or, Term, True};
|
2016-03-24 15:37:17 +01:00
|
|
|
let mut stats = Stats::default();
|
2016-03-24 10:45:24 +01:00
|
|
|
recurse(b, &mut stats);
|
|
|
|
stats
|
|
|
|
}
|
|
|
|
|
2016-03-23 12:19:13 +01:00
|
|
|
impl<'a, 'tcx> NonminimalBoolVisitor<'a, 'tcx> {
|
2019-12-27 16:12:26 +09:00
|
|
|
fn bool_expr(&self, e: &'tcx Expr<'_>) {
|
2016-03-23 14:50:08 +01:00
|
|
|
let mut h2q = Hir2Qmm {
|
|
|
|
terminals: Vec::new(),
|
2016-12-06 11:32:21 +01:00
|
|
|
cx: self.cx,
|
2016-03-23 14:50:08 +01:00
|
|
|
};
|
2016-03-23 12:19:13 +01:00
|
|
|
if let Ok(expr) = h2q.run(e) {
|
2016-04-01 21:24:26 +05:30
|
|
|
if h2q.terminals.len() > 8 {
|
|
|
|
// QMC has exponentially slow behavior as the number of terminals increases
|
|
|
|
// 8 is reasonable, it takes approximately 0.2 seconds.
|
|
|
|
// See #825
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-24 10:45:24 +01:00
|
|
|
let stats = terminal_stats(&expr);
|
2016-03-23 12:52:17 +01:00
|
|
|
let mut simplified = expr.simplify();
|
2019-09-28 20:29:35 +09:00
|
|
|
for simple in Bool::Not(Box::new(expr)).simplify() {
|
2016-03-24 10:54:48 +01:00
|
|
|
match simple {
|
2016-12-20 18:21:30 +01:00
|
|
|
Bool::Not(_) | Bool::True | Bool::False => {},
|
2016-03-24 10:54:48 +01:00
|
|
|
_ => simplified.push(Bool::Not(Box::new(simple.clone()))),
|
|
|
|
}
|
2016-03-23 12:52:17 +01:00
|
|
|
let simple_negated = simple_negate(simple);
|
|
|
|
if simplified.iter().any(|s| *s == simple_negated) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
simplified.push(simple_negated);
|
|
|
|
}
|
2020-02-18 20:50:10 +09:00
|
|
|
let mut improvements = Vec::with_capacity(simplified.len());
|
2016-03-24 15:37:17 +01:00
|
|
|
'simplified: for suggestion in &simplified {
|
2016-04-26 17:05:39 +02:00
|
|
|
let simplified_stats = terminal_stats(suggestion);
|
2016-03-24 15:37:17 +01:00
|
|
|
let mut improvement = false;
|
|
|
|
for i in 0..32 {
|
2016-06-06 01:42:39 +02:00
|
|
|
// ignore any "simplifications" that end up requiring a terminal more often
|
|
|
|
// than in the original expression
|
2016-03-24 15:37:17 +01:00
|
|
|
if stats.terminals[i] < simplified_stats.terminals[i] {
|
|
|
|
continue 'simplified;
|
2016-03-23 12:19:13 +01:00
|
|
|
}
|
2016-03-24 15:37:17 +01:00
|
|
|
if stats.terminals[i] != 0 && simplified_stats.terminals[i] == 0 {
|
2022-06-30 10:50:09 +02:00
|
|
|
span_lint_hir_and_then(
|
2017-08-09 09:30:56 +02:00
|
|
|
self.cx,
|
2022-08-11 19:42:16 +02:00
|
|
|
OVERLY_COMPLEX_BOOL_EXPR,
|
2022-06-30 10:50:09 +02:00
|
|
|
e.hir_id,
|
2017-08-09 09:30:56 +02:00
|
|
|
e.span,
|
|
|
|
"this boolean expression contains a logic bug",
|
2020-04-17 08:08:00 +02:00
|
|
|
|diag| {
|
|
|
|
diag.span_help(
|
2017-08-09 09:30:56 +02:00
|
|
|
h2q.terminals[i].span,
|
|
|
|
"this expression can be optimized out by applying boolean operations to the \
|
2017-09-05 11:33:04 +02:00
|
|
|
outer expression",
|
2017-08-09 09:30:56 +02:00
|
|
|
);
|
2020-04-17 08:08:00 +02:00
|
|
|
diag.span_suggestion(
|
2017-08-09 09:30:56 +02:00
|
|
|
e.span,
|
|
|
|
"it would look like the following",
|
2019-09-27 07:59:34 +02:00
|
|
|
suggest(self.cx, suggestion, &h2q.terminals),
|
2018-09-18 20:01:17 +03:00
|
|
|
// nonminimal_bool can produce minimal but
|
|
|
|
// not human readable expressions (#3141)
|
2018-09-15 19:14:08 +03:00
|
|
|
Applicability::Unspecified,
|
2017-08-09 09:30:56 +02:00
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2016-03-24 15:37:17 +01:00
|
|
|
// don't also lint `NONMINIMAL_BOOL`
|
|
|
|
return;
|
2016-03-24 10:45:24 +01:00
|
|
|
}
|
2016-06-06 01:42:39 +02:00
|
|
|
// if the number of occurrences of a terminal decreases or any of the stats
|
|
|
|
// decreases while none increases
|
2017-11-05 04:55:56 +09:00
|
|
|
improvement |= (stats.terminals[i] > simplified_stats.terminals[i])
|
|
|
|
|| (stats.negations > simplified_stats.negations && stats.ops == simplified_stats.ops)
|
|
|
|
|| (stats.ops > simplified_stats.ops && stats.negations == simplified_stats.negations);
|
2016-03-24 10:45:24 +01:00
|
|
|
}
|
2016-03-24 15:37:17 +01:00
|
|
|
if improvement {
|
|
|
|
improvements.push(suggestion);
|
2016-03-24 10:45:24 +01:00
|
|
|
}
|
2016-03-23 12:19:13 +01:00
|
|
|
}
|
2023-11-08 18:24:49 +00:00
|
|
|
let nonminimal_bool_lint = |mut suggestions: Vec<_>| {
|
2023-04-11 15:31:08 +02:00
|
|
|
if self.cx.tcx.lint_level_at_node(NONMINIMAL_BOOL, e.hir_id).0 != Level::Allow {
|
2023-11-08 18:24:49 +00:00
|
|
|
suggestions.sort();
|
2023-04-11 15:31:08 +02:00
|
|
|
span_lint_hir_and_then(
|
|
|
|
self.cx,
|
|
|
|
NONMINIMAL_BOOL,
|
|
|
|
e.hir_id,
|
|
|
|
e.span,
|
|
|
|
"this boolean expression can be simplified",
|
|
|
|
|diag| {
|
|
|
|
diag.span_suggestions(
|
|
|
|
e.span,
|
|
|
|
"try",
|
2023-07-02 14:35:19 +02:00
|
|
|
suggestions,
|
2023-04-11 15:31:08 +02:00
|
|
|
// nonminimal_bool can produce minimal but
|
|
|
|
// not human readable expressions (#3141)
|
|
|
|
Applicability::Unspecified,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2017-11-14 21:14:08 +00:00
|
|
|
};
|
|
|
|
if improvements.is_empty() {
|
2019-09-24 08:06:58 +02:00
|
|
|
let mut visitor = NotSimplificationVisitor { cx: self.cx };
|
|
|
|
visitor.visit_expr(e);
|
2017-11-14 21:14:08 +00:00
|
|
|
} else {
|
|
|
|
nonminimal_bool_lint(
|
|
|
|
improvements
|
|
|
|
.into_iter()
|
2019-09-27 07:59:34 +02:00
|
|
|
.map(|suggestion| suggest(self.cx, suggestion, &h2q.terminals))
|
2018-11-27 21:14:15 +01:00
|
|
|
.collect(),
|
2017-08-09 09:30:56 +02:00
|
|
|
);
|
2016-03-24 15:37:17 +01:00
|
|
|
}
|
2016-03-23 12:19:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 11:32:21 +01:00
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for NonminimalBoolVisitor<'a, 'tcx> {
|
2019-12-27 16:12:26 +09:00
|
|
|
fn visit_expr(&mut self, e: &'tcx Expr<'_>) {
|
2021-12-06 12:33:31 +01:00
|
|
|
if !e.span.from_expansion() {
|
|
|
|
match &e.kind {
|
|
|
|
ExprKind::Binary(binop, _, _) if binop.node == BinOpKind::Or || binop.node == BinOpKind::And => {
|
2018-11-27 21:14:15 +01:00
|
|
|
self.bool_expr(e);
|
2021-12-06 12:33:31 +01:00
|
|
|
},
|
|
|
|
ExprKind::Unary(UnOp::Not, inner) => {
|
2023-11-02 17:35:56 +01:00
|
|
|
if let ExprKind::Unary(UnOp::Not, ex) = inner.kind
|
|
|
|
&& !self.cx.typeck_results().node_types()[ex.hir_id].is_bool()
|
|
|
|
{
|
2023-06-02 11:41:57 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-12-06 12:33:31 +01:00
|
|
|
if self.cx.typeck_results().node_types()[inner.hir_id].is_bool() {
|
|
|
|
self.bool_expr(e);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
2016-03-23 12:19:13 +01:00
|
|
|
}
|
2021-12-06 12:33:31 +01:00
|
|
|
walk_expr(self, e);
|
2016-03-23 12:19:13 +01:00
|
|
|
}
|
|
|
|
}
|
2018-06-01 15:13:53 +02:00
|
|
|
|
2022-11-21 20:34:47 +01:00
|
|
|
fn implements_ord(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
|
2020-07-17 08:47:04 +00:00
|
|
|
let ty = cx.typeck_results().expr_ty(expr);
|
2022-10-23 15:18:45 +02:00
|
|
|
cx.tcx
|
|
|
|
.get_diagnostic_item(sym::Ord)
|
|
|
|
.map_or(false, |id| implements_trait(cx, ty, id, &[]))
|
2018-06-01 15:13:53 +02:00
|
|
|
}
|
2019-09-24 08:06:58 +02:00
|
|
|
|
|
|
|
struct NotSimplificationVisitor<'a, 'tcx> {
|
2020-06-25 23:41:36 +03:00
|
|
|
cx: &'a LateContext<'tcx>,
|
2019-09-24 08:06:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for NotSimplificationVisitor<'a, 'tcx> {
|
2019-12-27 16:12:26 +09:00
|
|
|
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
|
2023-11-02 17:35:56 +01:00
|
|
|
if let ExprKind::Unary(UnOp::Not, inner) = &expr.kind
|
2024-01-06 14:12:41 +08:00
|
|
|
&& !expr.span.from_expansion()
|
2023-11-02 17:35:56 +01:00
|
|
|
&& !inner.span.from_expansion()
|
|
|
|
&& let Some(suggestion) = simplify_not(self.cx, inner)
|
|
|
|
&& self.cx.tcx.lint_level_at_node(NONMINIMAL_BOOL, expr.hir_id).0 != Level::Allow
|
2023-03-24 14:04:35 +01:00
|
|
|
{
|
|
|
|
span_lint_and_sugg(
|
|
|
|
self.cx,
|
|
|
|
NONMINIMAL_BOOL,
|
|
|
|
expr.span,
|
|
|
|
"this boolean expression can be simplified",
|
|
|
|
"try",
|
|
|
|
suggestion,
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
2019-09-24 08:06:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
walk_expr(self, expr);
|
|
|
|
}
|
|
|
|
}
|