]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/booleans.rs
Update Clippy for MethodCall changes
[rust.git] / clippy_lints / src / booleans.rs
index 802ed6ec8e4bee57d3fc5fe0ac568c1ecdf75e7e..f92c564543b89bd46aba72bc7646a09bc190a7e9 100644 (file)
@@ -1,16 +1,16 @@
 use crate::utils::{
-    get_trait_def_id, implements_trait, in_macro, match_type, paths, snippet_opt, span_lint_and_sugg,
+    get_trait_def_id, implements_trait, in_macro, is_type_diagnostic_item, paths, snippet_opt, span_lint_and_sugg,
     span_lint_and_then, SpanlessEq,
 };
 use if_chain::if_chain;
-use rustc::declare_lint_pass;
-use rustc::hir::intravisit::*;
-use rustc::hir::*;
-use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
+use rustc_ast::ast::LitKind;
 use rustc_errors::Applicability;
-use rustc_session::declare_tool_lint;
-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_middle::hir::map::Map;
+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
@@ -59,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,
@@ -109,7 +109,7 @@ fn negate(bin_op_kind: BinOpKind) -> Option<BinOpKind> {
         // 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())?)),
@@ -161,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");
@@ -206,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(" || ");
                     }
@@ -247,9 +247,11 @@ fn simplify_not(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> Option<String> {
                 ))
             })
         },
-        ExprKind::MethodCall(path, _, args) if args.len() == 1 => {
+        ExprKind::MethodCall(path, _, args, _) if args.len() == 1 => {
             let type_of_receiver = cx.tables.expr_ty(&args[0]);
-            if !match_type(cx, type_of_receiver, &paths::OPTION) && !match_type(cx, type_of_receiver, &paths::RESULT) {
+            if !is_type_diagnostic_item(cx, type_of_receiver, sym!(option_type))
+                && !is_type_diagnostic_item(cx, type_of_receiver, sym!(result_type))
+            {
                 return None;
             }
             METHODS_WITH_NEGATION
@@ -277,7 +279,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,
@@ -325,7 +327,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
@@ -358,7 +360,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;
@@ -374,13 +376,13 @@ fn bool_expr(&self, e: &'tcx Expr<'_>) {
                             LOGIC_BUG,
                             e.span,
                             "this boolean expression contains a logic bug",
-                            |db| {
-                                db.span_help(
+                            |diag| {
+                                diag.span_help(
                                     h2q.terminals[i].span,
                                     "this expression can be optimized out by applying boolean operations to the \
                                      outer expression",
                                 );
-                                db.span_suggestion(
+                                diag.span_suggestion(
                                     e.span,
                                     "it would look like the following",
                                     suggest(self.cx, suggestion, &h2q.terminals),
@@ -409,8 +411,8 @@ fn bool_expr(&self, e: &'tcx Expr<'_>) {
                     NONMINIMAL_BOOL,
                     e.span,
                     "this boolean expression can be simplified",
-                    |db| {
-                        db.span_suggestions(
+                    |diag| {
+                        diag.span_suggestions(
                             e.span,
                             "try",
                             suggestions.into_iter(),
@@ -437,6 +439,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;
@@ -445,7 +449,7 @@ fn visit_expr(&mut self, e: &'tcx Expr<'_>) {
             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 {
@@ -455,7 +459,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
     }
 }
@@ -470,8 +474,10 @@ 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(UnNot, inner) = &expr.kind {
+        if let ExprKind::Unary(UnOp::UnNot, inner) = &expr.kind {
             if let Some(suggestion) = simplify_not(self.cx, inner) {
                 span_lint_and_sugg(
                     self.cx,
@@ -487,7 +493,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
     }
 }