]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/booleans.rs
Use span_suggestion_with_applicability instead of span_suggestion
[rust.git] / clippy_lints / src / booleans.rs
index 8816d50c5c262e6b6408942fb7aeba6551802851..85f6eeb19ef9f22294710dd27dfd218ed3aa6f83 100644 (file)
@@ -1,10 +1,12 @@
-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 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.
@@ -84,9 +86,9 @@ struct Hir2Qmm<'a, 'tcx: 'a, 'v> {
 }
 
 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;
@@ -101,13 +103,13 @@ fn run(&mut self, e: &'v Expr) -> Result<Bool, String> {
         // 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),
                     _ => (),
@@ -117,16 +119,15 @@ fn run(&mut self, e: &'v Expr) -> Result<Bool, String> {
         }
         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) => {
-                    match implements_ord(self.cx, lhs) {
-                        Some(true) => (),
-                        _ => continue,
-                    };
+                ExprKind::Binary(binop, ref lhs, ref rhs) => {
+
+                    if !implements_ord(self.cx, lhs) {
+                        continue;
+                    }
 
                     let mk_expr = |op| {
                         Expr {
@@ -134,30 +135,30 @@ fn run(&mut self, e: &'v Expr) -> Result<Bool, String> {
                             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())
@@ -179,24 +180,23 @@ fn snip(&self, e: &Expr) -> Option<String> {
 
     fn simplify_not(&self, expr: &Expr) -> Option<String> {
         match expr.node {
-            ExprBinary(binop, ref lhs, ref rhs) => {
+            ExprKind::Binary(binop, ref lhs, ref rhs) => {
 
-                match implements_ord(self.cx, lhs) {
-                    Some(true) => (),
-                    _ => return None,
-                };
+                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) {
@@ -205,7 +205,7 @@ fn simplify_not(&self, expr: &Expr) -> Option<String> {
                 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,
@@ -276,7 +276,7 @@ fn recurse(&mut self, suggestion: &Bool) -> Option<()> {
 }
 
 // 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,
@@ -391,10 +391,11 @@ fn bool_expr(&self, e: &Expr) {
                                     "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,
                                 );
                             },
                         );
@@ -443,8 +444,8 @@ fn visit_expr(&mut self, e: &'tcx Expr) {
             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);
@@ -458,12 +459,8 @@ fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
 }
 
 
-fn implements_ord<'a, 'tcx>(cx: &'a LateContext<'a, 'tcx>, expr: &Expr) -> Option<bool> {
+fn implements_ord<'a, 'tcx>(cx: &'a LateContext<'a, 'tcx>, expr: &Expr) -> bool {
     let ty = cx.tables.expr_ty(expr);
-
-    return if let Some(id) = get_trait_def_id(cx, &paths::ORD) {
-        Some(implements_trait(cx, ty, id, &[]))
-    } else {
-        None
-    };
+    get_trait_def_id(cx, &paths::ORD)
+        .map_or(false, |id| implements_trait(cx, ty, id, &[]))
 }