span_lint_and_then, SpanlessEq,
};
use if_chain::if_chain;
-use rustc::hir::intravisit::*;
-use rustc::hir::*;
-use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
-use rustc::{declare_lint_pass, declare_tool_lint};
+use rustc::hir::map::Map;
+use rustc_ast::ast::LitKind;
use rustc_errors::Applicability;
-use syntax::ast::LitKind;
-use syntax::source_map::Span;
+use rustc_hir::intravisit::{walk_expr, FnKind, NestedVisitorMap, Visitor};
+use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, HirId, UnOp};
+use rustc_lint::{LateContext, LateLintPass};
+use rustc_session::{declare_lint_pass, declare_tool_lint};
+use rustc_span::source_map::Span;
declare_clippy_lint! {
/// **What it does:** Checks for boolean expressions that can be written more
fn check_fn(
&mut self,
cx: &LateContext<'a, 'tcx>,
- _: intravisit::FnKind<'tcx>,
- _: &'tcx FnDecl,
- body: &'tcx Body,
+ _: FnKind<'tcx>,
+ _: &'tcx FnDecl<'_>,
+ body: &'tcx Body<'_>,
_: Span,
_: HirId,
) {
use quine_mc_cluskey::Bool;
struct Hir2Qmm<'a, 'tcx, 'v> {
- terminals: Vec<&'v Expr>,
+ terminals: Vec<&'v Expr<'v>>,
cx: &'a LateContext<'a, 'tcx>,
}
impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> {
- fn extract(&mut self, op: BinOpKind, 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 ExprKind::Binary(binop, lhs, rhs) = &a.kind {
if binop.node == op {
Ok(v)
}
- fn run(&mut self, e: &'v Expr) -> Result<Bool, String> {
+ fn run(&mut self, e: &'v Expr<'_>) -> Result<Bool, String> {
fn negate(bin_op_kind: BinOpKind) -> Option<BinOpKind> {
match bin_op_kind {
BinOpKind::Eq => Some(BinOpKind::Ne),
// prevent folding of `cfg!` macros and the like
if !e.span.from_expansion() {
match &e.kind {
- ExprKind::Unary(UnNot, inner) => return Ok(Bool::Not(box self.run(inner)?)),
+ ExprKind::Unary(UnOp::UnNot, inner) => return Ok(Bool::Not(box self.run(inner)?)),
ExprKind::Binary(binop, lhs, 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())?)),
}
struct SuggestContext<'a, 'tcx, 'v> {
- terminals: &'v [&'v Expr],
+ terminals: &'v [&'v Expr<'v>],
cx: &'a LateContext<'a, 'tcx>,
output: String,
}
impl<'a, 'tcx, 'v> SuggestContext<'a, 'tcx, 'v> {
fn recurse(&mut self, suggestion: &Bool) -> Option<()> {
- use quine_mc_cluskey::Bool::*;
+ use quine_mc_cluskey::Bool::{And, False, Not, Or, Term, True};
match suggestion {
True => {
self.output.push_str("true");
}
},
Or(v) => {
- for (index, inner) in v.iter().enumerate() {
+ for (index, inner) in v.iter().rev().enumerate() {
if index > 0 {
self.output.push_str(" || ");
}
}
}
-fn simplify_not(cx: &LateContext<'_, '_>, expr: &Expr) -> Option<String> {
+fn simplify_not(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> Option<String> {
match &expr.kind {
ExprKind::Binary(binop, lhs, rhs) => {
if !implements_ord(cx, lhs) {
.iter()
.cloned()
.flat_map(|(a, b)| vec![(a, b), (b, a)])
- .find(|&(a, _)| a == path.ident.name.as_str())
+ .find(|&(a, _)| {
+ let path: &str = &path.ident.name.as_str();
+ a == path
+ })
.and_then(|(_, neg_method)| Some(format!("{}.{}()", snippet_opt(cx, args[0].span)?, neg_method)))
},
_ => None,
}
}
-fn suggest(cx: &LateContext<'_, '_>, suggestion: &Bool, terminals: &[&Expr]) -> String {
+fn suggest(cx: &LateContext<'_, '_>, suggestion: &Bool, terminals: &[&Expr<'_>]) -> String {
let mut suggest_context = SuggestContext {
terminals,
cx,
}
fn simple_negate(b: Bool) -> Bool {
- use quine_mc_cluskey::Bool::*;
+ use quine_mc_cluskey::Bool::{And, False, Not, Or, Term, True};
match b {
True => False,
False => True,
&Term(n) => stats.terminals[n as usize] += 1,
}
}
- use quine_mc_cluskey::Bool::*;
+ use quine_mc_cluskey::Bool::{And, False, Not, Or, Term, True};
let mut stats = Stats::default();
recurse(b, &mut stats);
stats
}
impl<'a, 'tcx> NonminimalBoolVisitor<'a, 'tcx> {
- fn bool_expr(&self, e: &'tcx Expr) {
+ fn bool_expr(&self, e: &'tcx Expr<'_>) {
let mut h2q = Hir2Qmm {
terminals: Vec::new(),
cx: self.cx,
}
simplified.push(simple_negated);
}
- let mut improvements = Vec::new();
+ let mut improvements = Vec::with_capacity(simplified.len());
'simplified: for suggestion in &simplified {
let simplified_stats = terminal_stats(suggestion);
let mut improvement = false;
}
impl<'a, 'tcx> Visitor<'tcx> for NonminimalBoolVisitor<'a, 'tcx> {
- fn visit_expr(&mut self, e: &'tcx Expr) {
+ type Map = Map<'tcx>;
+
+ fn visit_expr(&mut self, e: &'tcx Expr<'_>) {
if in_macro(e.span) {
return;
}
ExprKind::Binary(binop, _, _) if binop.node == BinOpKind::Or || binop.node == BinOpKind::And => {
self.bool_expr(e)
},
- ExprKind::Unary(UnNot, inner) => {
+ ExprKind::Unary(UnOp::UnNot, inner) => {
if self.cx.tables.node_types()[inner.hir_id].is_bool() {
self.bool_expr(e);
} else {
_ => walk_expr(self, e),
}
}
- fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
+ fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
NestedVisitorMap::None
}
}
-fn implements_ord<'a, 'tcx>(cx: &'a LateContext<'a, 'tcx>, expr: &Expr) -> bool {
+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, &[]))
}
}
impl<'a, 'tcx> Visitor<'tcx> for NotSimplificationVisitor<'a, 'tcx> {
- fn visit_expr(&mut self, expr: &'tcx Expr) {
- if let ExprKind::Unary(UnNot, inner) = &expr.kind {
+ type Map = Map<'tcx>;
+
+ fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
+ if let ExprKind::Unary(UnOp::UnNot, inner) = &expr.kind {
if let Some(suggestion) = simplify_not(self.cx, inner) {
span_lint_and_sugg(
self.cx,
walk_expr(self, expr);
}
- fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
+ fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
NestedVisitorMap::None
}
}