]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/booleans.rs
rustup https://github.com/rust-lang/rust/pull/68944
[rust.git] / clippy_lints / src / booleans.rs
index 6d918b3cc4b4bb73aff24939cfde121639bbac8a..b580240be175774013f7a8e28e85b230659f192b 100644 (file)
@@ -3,15 +3,14 @@
     span_lint_and_then, SpanlessEq,
 };
 use if_chain::if_chain;
-use rustc::declare_lint_pass;
-use rustc::hir::intravisit;
-use rustc::hir::intravisit::*;
-use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
+use rustc::hir::map::Map;
+use rustc_ast::ast::LitKind;
 use rustc_errors::Applicability;
-use rustc_hir::*;
-use rustc_session::declare_tool_lint;
+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;
-use syntax::ast::LitKind;
 
 declare_clippy_lint! {
     /// **What it does:** Checks for boolean expressions that can be written more
@@ -60,7 +59,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonminimalBool {
     fn check_fn(
         &mut self,
         cx: &LateContext<'a, 'tcx>,
-        _: intravisit::FnKind<'tcx>,
+        _: FnKind<'tcx>,
         _: &'tcx FnDecl<'_>,
         body: &'tcx Body<'_>,
         _: Span,
@@ -162,7 +161,7 @@ struct SuggestContext<'a, 'tcx, 'v> {
 
 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");
@@ -207,7 +206,7 @@ fn recurse(&mut self, suggestion: &Bool) -> Option<()> {
                 }
             },
             Or(v) => {
-                for (index, inner) in v.iter().enumerate() {
+                for (index, inner) in v.iter().rev().enumerate() {
                     if index > 0 {
                         self.output.push_str(" || ");
                     }
@@ -278,7 +277,7 @@ fn suggest(cx: &LateContext<'_, '_>, suggestion: &Bool, terminals: &[&Expr<'_>])
 }
 
 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,
@@ -326,7 +325,7 @@ fn recurse(b: &Bool, stats: &mut Stats) {
             &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
@@ -359,7 +358,7 @@ fn bool_expr(&self, e: &'tcx Expr<'_>) {
                 }
                 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;
@@ -438,6 +437,8 @@ fn bool_expr(&self, e: &'tcx Expr<'_>) {
 }
 
 impl<'a, 'tcx> Visitor<'tcx> for NonminimalBoolVisitor<'a, 'tcx> {
+    type Map = Map<'tcx>;
+
     fn visit_expr(&mut self, e: &'tcx Expr<'_>) {
         if in_macro(e.span) {
             return;
@@ -456,7 +457,7 @@ fn visit_expr(&mut self, e: &'tcx Expr<'_>) {
             _ => 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
     }
 }
@@ -471,6 +472,8 @@ struct NotSimplificationVisitor<'a, 'tcx> {
 }
 
 impl<'a, 'tcx> Visitor<'tcx> for NotSimplificationVisitor<'a, 'tcx> {
+    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) {
@@ -488,7 +491,7 @@ fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
 
         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
     }
 }