]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/int_plus_one.rs
Update lint documentation to use markdown headlines
[rust.git] / clippy_lints / src / int_plus_one.rs
index 22ebf562d3ba16fd5eb98265d88b912aec30c5ec..49b69dd072a21352da51d21d4eb1a4c0bee2778f 100644 (file)
@@ -1,21 +1,20 @@
 //! lint on blocks unnecessarily using >= with a + 1 or - 1
 
-use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
-use rustc::{declare_lint_pass, declare_tool_lint};
+use clippy_utils::diagnostics::span_lint_and_sugg;
+use clippy_utils::source::snippet_opt;
+use rustc_ast::ast::{BinOpKind, Expr, ExprKind, Lit, LitKind};
 use rustc_errors::Applicability;
-use syntax::ast::*;
-
-use crate::utils::{snippet_opt, span_lint_and_then};
+use rustc_lint::{EarlyContext, EarlyLintPass};
+use rustc_session::{declare_lint_pass, declare_tool_lint};
 
 declare_clippy_lint! {
-    /// **What it does:** Checks for usage of `x >= y + 1` or `x - 1 >= y` (and `<=`) in a block
-    ///
+    /// ### What it does
+    /// Checks for usage of `x >= y + 1` or `x - 1 >= y` (and `<=`) in a block
     ///
-    /// **Why is this bad?** Readability -- better to use `> y` instead of `>= y + 1`.
+    /// ### Why is this bad?
+    /// Readability -- better to use `> y` instead of `>= y + 1`.
     ///
-    /// **Known problems:** None.
-    ///
-    /// **Example:**
+    /// ### Example
     /// ```rust
     /// # let x = 1;
     /// # let y = 1;
@@ -31,7 +30,7 @@
     /// ```
     pub INT_PLUS_ONE,
     complexity,
-    "instead of using x >= y + 1, use x > y"
+    "instead of using `x >= y + 1`, use `x > y`"
 }
 
 declare_lint_pass!(IntPlusOne => [INT_PLUS_ONE]);
 
 #[derive(Copy, Clone)]
 enum Side {
-    LHS,
-    RHS,
+    Lhs,
+    Rhs,
 }
 
 impl IntPlusOne {
     #[allow(clippy::cast_sign_loss)]
-    fn check_lit(self, lit: &Lit, target_value: i128) -> bool {
-        if let LitKind::Int(value, ..) = lit.node {
+    fn check_lit(lit: &Lit, target_value: i128) -> bool {
+        if let LitKind::Int(value, ..) = lit.kind {
             return value == (target_value as u128);
         }
         false
     }
 
-    fn check_binop(self, cx: &EarlyContext<'_>, binop: BinOpKind, lhs: &Expr, rhs: &Expr) -> Option<String> {
-        match (binop, &lhs.node, &rhs.node) {
+    fn check_binop(cx: &EarlyContext<'_>, binop: BinOpKind, lhs: &Expr, rhs: &Expr) -> Option<String> {
+        match (binop, &lhs.kind, &rhs.kind) {
             // case where `x - 1 >= ...` or `-1 + x >= ...`
             (BinOpKind::Ge, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _) => {
-                match (lhskind.node, &lhslhs.node, &lhsrhs.node) {
+                match (lhskind.node, &lhslhs.kind, &lhsrhs.kind) {
                     // `-1 + x`
-                    (BinOpKind::Add, &ExprKind::Lit(ref lit), _) if self.check_lit(lit, -1) => {
-                        self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS)
+                    (BinOpKind::Add, &ExprKind::Lit(ref lit), _) if Self::check_lit(lit, -1) => {
+                        Self::generate_recommendation(cx, binop, lhsrhs, rhs, Side::Lhs)
                     },
                     // `x - 1`
-                    (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
-                        self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS)
+                    (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if Self::check_lit(lit, 1) => {
+                        Self::generate_recommendation(cx, binop, lhslhs, rhs, Side::Lhs)
                     },
                     _ => None,
                 }
@@ -80,13 +79,13 @@ fn check_binop(self, cx: &EarlyContext<'_>, binop: BinOpKind, lhs: &Expr, rhs: &
             (BinOpKind::Ge, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs))
                 if rhskind.node == BinOpKind::Add =>
             {
-                match (&rhslhs.node, &rhsrhs.node) {
+                match (&rhslhs.kind, &rhsrhs.kind) {
                     // `y + 1` and `1 + y`
-                    (&ExprKind::Lit(ref lit), _) if self.check_lit(lit, 1) => {
-                        self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS)
+                    (&ExprKind::Lit(ref lit), _) if Self::check_lit(lit, 1) => {
+                        Self::generate_recommendation(cx, binop, rhsrhs, lhs, Side::Rhs)
                     },
-                    (_, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
-                        self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS)
+                    (_, &ExprKind::Lit(ref lit)) if Self::check_lit(lit, 1) => {
+                        Self::generate_recommendation(cx, binop, rhslhs, lhs, Side::Rhs)
                     },
                     _ => None,
                 }
@@ -95,27 +94,27 @@ fn check_binop(self, cx: &EarlyContext<'_>, binop: BinOpKind, lhs: &Expr, rhs: &
             (BinOpKind::Le, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _)
                 if lhskind.node == BinOpKind::Add =>
             {
-                match (&lhslhs.node, &lhsrhs.node) {
+                match (&lhslhs.kind, &lhsrhs.kind) {
                     // `1 + x` and `x + 1`
-                    (&ExprKind::Lit(ref lit), _) if self.check_lit(lit, 1) => {
-                        self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS)
+                    (&ExprKind::Lit(ref lit), _) if Self::check_lit(lit, 1) => {
+                        Self::generate_recommendation(cx, binop, lhsrhs, rhs, Side::Lhs)
                     },
-                    (_, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
-                        self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS)
+                    (_, &ExprKind::Lit(ref lit)) if Self::check_lit(lit, 1) => {
+                        Self::generate_recommendation(cx, binop, lhslhs, rhs, Side::Lhs)
                     },
                     _ => None,
                 }
             }
             // case where `... >= y - 1` or `... >= -1 + y`
             (BinOpKind::Le, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs)) => {
-                match (rhskind.node, &rhslhs.node, &rhsrhs.node) {
+                match (rhskind.node, &rhslhs.kind, &rhsrhs.kind) {
                     // `-1 + y`
-                    (BinOpKind::Add, &ExprKind::Lit(ref lit), _) if self.check_lit(lit, -1) => {
-                        self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS)
+                    (BinOpKind::Add, &ExprKind::Lit(ref lit), _) if Self::check_lit(lit, -1) => {
+                        Self::generate_recommendation(cx, binop, rhsrhs, lhs, Side::Rhs)
                     },
                     // `y - 1`
-                    (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
-                        self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS)
+                    (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if Self::check_lit(lit, 1) => {
+                        Self::generate_recommendation(cx, binop, rhslhs, lhs, Side::Rhs)
                     },
                     _ => None,
                 }
@@ -125,7 +124,6 @@ fn check_binop(self, cx: &EarlyContext<'_>, binop: BinOpKind, lhs: &Expr, rhs: &
     }
 
     fn generate_recommendation(
-        self,
         cx: &EarlyContext<'_>,
         binop: BinOpKind,
         node: &Expr,
@@ -140,8 +138,8 @@ fn generate_recommendation(
         if let Some(snippet) = snippet_opt(cx, node.span) {
             if let Some(other_side_snippet) = snippet_opt(cx, other_side.span) {
                 let rec = match side {
-                    Side::LHS => Some(format!("{} {} {}", snippet, binop_string, other_side_snippet)),
-                    Side::RHS => Some(format!("{} {} {}", other_side_snippet, binop_string, snippet)),
+                    Side::Lhs => Some(format!("{} {} {}", snippet, binop_string, other_side_snippet)),
+                    Side::Rhs => Some(format!("{} {} {}", other_side_snippet, binop_string, snippet)),
                 };
                 return rec;
             }
@@ -149,29 +147,24 @@ fn generate_recommendation(
         None
     }
 
-    fn emit_warning(self, cx: &EarlyContext<'_>, block: &Expr, recommendation: String) {
-        span_lint_and_then(
+    fn emit_warning(cx: &EarlyContext<'_>, block: &Expr, recommendation: String) {
+        span_lint_and_sugg(
             cx,
             INT_PLUS_ONE,
             block.span,
-            "Unnecessary `>= y + 1` or `x - 1 >=`",
-            |db| {
-                db.span_suggestion(
-                    block.span,
-                    "change `>= y + 1` to `> y` as shown",
-                    recommendation,
-                    Applicability::MachineApplicable, // snippet
-                );
-            },
+            "unnecessary `>= y + 1` or `x - 1 >=`",
+            "change it to",
+            recommendation,
+            Applicability::MachineApplicable, // snippet
         );
     }
 }
 
 impl EarlyLintPass for IntPlusOne {
     fn check_expr(&mut self, cx: &EarlyContext<'_>, item: &Expr) {
-        if let ExprKind::Binary(ref kind, ref lhs, ref rhs) = item.node {
-            if let Some(ref rec) = self.check_binop(cx, kind.node, lhs, rhs) {
-                self.emit_warning(cx, item, rec.clone());
+        if let ExprKind::Binary(ref kind, ref lhs, ref rhs) = item.kind {
+            if let Some(rec) = Self::check_binop(cx, kind.node, lhs, rhs) {
+                Self::emit_warning(cx, item, rec);
             }
         }
     }