-use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
-use rustc::hir::*;
-use rustc::hir::intravisit::*;
-use syntax::ast::{LitKind, NodeId, DUMMY_NODE_ID};
-use syntax::codemap::{dummy_spanned, Span, DUMMY_SP};
-use syntax::util::ThinVec;
-use utils::{in_macro, paths, match_type, snippet_opt, span_lint_and_then, SpanlessEq};
+use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
+use crate::rustc::{declare_tool_lint, lint_array};
+use crate::rustc::hir::*;
+use crate::rustc::hir::intravisit::*;
+use crate::syntax::ast::{LitKind, NodeId, DUMMY_NODE_ID};
+use crate::syntax::source_map::{dummy_spanned, Span, DUMMY_SP};
+use crate::rustc_data_structures::thin_vec::ThinVec;
+use crate::utils::{in_macro, paths, match_type, snippet_opt, span_lint_and_then, SpanlessEq, get_trait_def_id, implements_trait};
+use crate::rustc_errors::Applicability;
/// **What it does:** Checks for boolean expressions that can be written more
/// concisely.
}
impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> {
- fn extract(&mut self, op: BinOp_, a: &[&'v Expr], mut v: Vec<Bool>) -> Result<Vec<Bool>, String> {
+ fn extract(&mut self, op: BinOpKind, a: &[&'v Expr], mut v: Vec<Bool>) -> Result<Vec<Bool>, String> {
for a in a {
- if let ExprBinary(binop, ref lhs, ref rhs) = a.node {
+ if let ExprKind::Binary(binop, ref lhs, ref rhs) = a.node {
if binop.node == op {
v = self.extract(op, &[lhs, rhs], v)?;
continue;
// prevent folding of `cfg!` macros and the like
if !in_macro(e.span) {
match e.node {
- ExprUnary(UnNot, ref inner) => return Ok(Bool::Not(box self.run(inner)?)),
- ExprBinary(binop, ref lhs, ref rhs) => match binop.node {
- BiOr => return Ok(Bool::Or(self.extract(BiOr, &[lhs, rhs], Vec::new())?)),
- BiAnd => return Ok(Bool::And(self.extract(BiAnd, &[lhs, rhs], Vec::new())?)),
+ ExprKind::Unary(UnNot, ref inner) => return Ok(Bool::Not(box self.run(inner)?)),
+ ExprKind::Binary(binop, ref lhs, ref rhs) => match binop.node {
+ 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())?)),
_ => (),
},
- ExprLit(ref lit) => match lit.node {
+ ExprKind::Lit(ref lit) => match lit.node {
LitKind::Bool(true) => return Ok(Bool::True),
LitKind::Bool(false) => return Ok(Bool::False),
_ => (),
}
for (n, expr) in self.terminals.iter().enumerate() {
if SpanlessEq::new(self.cx).ignore_fn().eq_expr(e, expr) {
- #[allow(cast_possible_truncation)]
+ #[allow(clippy::cast_possible_truncation)]
return Ok(Bool::Term(n as u8));
}
let negated = match e.node {
- ExprBinary(binop, ref lhs, ref rhs) => {
+ ExprKind::Binary(binop, ref lhs, ref rhs) => {
+
+ if !implements_ord(self.cx, lhs) {
+ continue;
+ }
+
let mk_expr = |op| {
Expr {
id: DUMMY_NODE_ID,
hir_id: DUMMY_HIR_ID,
span: DUMMY_SP,
attrs: ThinVec::new(),
- node: ExprBinary(dummy_spanned(op), lhs.clone(), rhs.clone()),
+ node: ExprKind::Binary(dummy_spanned(op), lhs.clone(), rhs.clone()),
}
};
match binop.node {
- BiEq => mk_expr(BiNe),
- BiNe => mk_expr(BiEq),
- BiGt => mk_expr(BiLe),
- BiGe => mk_expr(BiLt),
- BiLt => mk_expr(BiGe),
- BiLe => mk_expr(BiGt),
+ BinOpKind::Eq => mk_expr(BinOpKind::Ne),
+ BinOpKind::Ne => mk_expr(BinOpKind::Eq),
+ BinOpKind::Gt => mk_expr(BinOpKind::Le),
+ BinOpKind::Ge => mk_expr(BinOpKind::Lt),
+ BinOpKind::Lt => mk_expr(BinOpKind::Ge),
+ BinOpKind::Le => mk_expr(BinOpKind::Gt),
_ => continue,
}
},
_ => continue,
};
if SpanlessEq::new(self.cx).ignore_fn().eq_expr(&negated, expr) {
- #[allow(cast_possible_truncation)]
+ #[allow(clippy::cast_possible_truncation)]
return Ok(Bool::Not(Box::new(Bool::Term(n as u8))));
}
}
let n = self.terminals.len();
self.terminals.push(e);
if n < 32 {
- #[allow(cast_possible_truncation)]
+ #[allow(clippy::cast_possible_truncation)]
Ok(Bool::Term(n as u8))
} else {
Err("too many literals".to_owned())
fn simplify_not(&self, expr: &Expr) -> Option<String> {
match expr.node {
- ExprBinary(binop, ref lhs, ref rhs) => {
+ ExprKind::Binary(binop, ref lhs, ref rhs) => {
+
+ if !implements_ord(self.cx, lhs) {
+ return None;
+ }
+
match binop.node {
- BiEq => Some(" != "),
- BiNe => Some(" == "),
- BiLt => Some(" >= "),
- BiGt => Some(" <= "),
- BiLe => Some(" > "),
- BiGe => Some(" < "),
+ BinOpKind::Eq => Some(" != "),
+ BinOpKind::Ne => Some(" == "),
+ BinOpKind::Lt => Some(" >= "),
+ BinOpKind::Gt => Some(" <= "),
+ BinOpKind::Le => Some(" > "),
+ BinOpKind::Ge => Some(" < "),
_ => None,
}.and_then(|op| Some(format!("{}{}{}", self.snip(lhs)?, op, self.snip(rhs)?)))
},
- ExprMethodCall(ref path, _, ref args) if args.len() == 1 => {
+ ExprKind::MethodCall(ref path, _, ref args) if args.len() == 1 => {
let type_of_receiver = self.cx.tables.expr_ty(&args[0]);
if !match_type(self.cx, type_of_receiver, &paths::OPTION) &&
!match_type(self.cx, type_of_receiver, &paths::RESULT) {
METHODS_WITH_NEGATION
.iter().cloned()
.flat_map(|(a, b)| vec![(a, b), (b, a)])
- .find(|&(a, _)| a == path.name.as_str())
+ .find(|&(a, _)| a == path.ident.as_str())
.and_then(|(_, neg_method)| Some(format!("{}.{}()", self.snip(&args[0])?, neg_method)))
},
_ => None,
}
// The boolean part of the return indicates whether some simplifications have been applied.
-fn suggest(cx: &LateContext, suggestion: &Bool, terminals: &[&Expr]) -> (String, bool) {
+fn suggest(cx: &LateContext<'_, '_>, suggestion: &Bool, terminals: &[&Expr]) -> (String, bool) {
let mut suggest_context = SuggestContext {
terminals,
cx,
"this expression can be optimized out by applying boolean operations to the \
outer expression",
);
- db.span_suggestion(
+ db.span_suggestion_with_applicability(
e.span,
"it would look like the following",
suggest(self.cx, suggestion, &h2q.terminals).0,
+ Applicability::Unspecified,
);
},
);
return;
}
match e.node {
- ExprBinary(binop, _, _) if binop.node == BiOr || binop.node == BiAnd => self.bool_expr(e),
- ExprUnary(UnNot, ref inner) => if self.cx.tables.node_types()[inner.hir_id].is_bool() {
+ ExprKind::Binary(binop, _, _) if binop.node == BinOpKind::Or || binop.node == BinOpKind::And => self.bool_expr(e),
+ ExprKind::Unary(UnNot, ref inner) => if self.cx.tables.node_types()[inner.hir_id].is_bool() {
self.bool_expr(e);
} else {
walk_expr(self, e);
NestedVisitorMap::None
}
}
+
+
+fn implements_ord<'a, 'tcx>(cx: &'a LateContext<'a, 'tcx>, expr: &Expr) -> bool {
+ let ty = cx.tables.expr_ty(expr);
+ get_trait_def_id(cx, &paths::ORD)
+ .map_or(false, |id| implements_trait(cx, ty, id, &[]))
+}