]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/int_plus_one.rs
Auto merge of #4551 - mikerite:fix-ice-reporting, r=llogiq
[rust.git] / clippy_lints / src / int_plus_one.rs
index f94461b75a587324869158f86b3864e4f7d2d723..c886743533f8949514b1ad00388e1210acfb6c5a 100644 (file)
@@ -1,41 +1,40 @@
 //! lint on blocks unnecessarily using >= with a + 1 or - 1
 
 use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
-use rustc::{declare_tool_lint, lint_array};
+use rustc::{declare_lint_pass, declare_tool_lint};
+use rustc_errors::Applicability;
 use syntax::ast::*;
 
 use crate::utils::{snippet_opt, span_lint_and_then};
 
-/// **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`.
-///
-/// **Known problems:** None.
-///
-/// **Example:**
-/// ```rust
-/// x >= y + 1
-/// ```
-///
-/// Could be written:
-///
-/// ```rust
-/// x > y
-/// ```
 declare_clippy_lint! {
+    /// **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`.
+    ///
+    /// **Known problems:** None.
+    ///
+    /// **Example:**
+    /// ```rust
+    /// # let x = 1;
+    /// # let y = 1;
+    /// if x >= y + 1 {}
+    /// ```
+    ///
+    /// Could be written as:
+    ///
+    /// ```rust
+    /// # let x = 1;
+    /// # let y = 1;
+    /// if x > y {}
+    /// ```
     pub INT_PLUS_ONE,
     complexity,
     "instead of using x >= y + 1, use x > y"
 }
 
-pub struct IntPlusOne;
-
-impl LintPass for IntPlusOne {
-    fn get_lints(&self) -> LintArray {
-        lint_array!(INT_PLUS_ONE)
-    }
-}
+declare_lint_pass!(IntPlusOne => [INT_PLUS_ONE]);
 
 // cases:
 // BinOpKind::Ge
@@ -54,14 +53,14 @@ enum Side {
 
 impl IntPlusOne {
     #[allow(clippy::cast_sign_loss)]
-    fn check_lit(&self, lit: &Lit, target_value: i128) -> bool {
+    fn check_lit(self, lit: &Lit, target_value: i128) -> bool {
         if let LitKind::Int(value, ..) = lit.node {
             return value == (target_value as u128);
         }
         false
     }
 
-    fn check_binop(&self, cx: &EarlyContext<'_>, binop: BinOpKind, lhs: &Expr, rhs: &Expr) -> Option<String> {
+    fn check_binop(self, cx: &EarlyContext<'_>, binop: BinOpKind, lhs: &Expr, rhs: &Expr) -> Option<String> {
         match (binop, &lhs.node, &rhs.node) {
             // case where `x - 1 >= ...` or `-1 + x >= ...`
             (BinOpKind::Ge, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _) => {
@@ -126,7 +125,7 @@ fn check_binop(&self, cx: &EarlyContext<'_>, binop: BinOpKind, lhs: &Expr, rhs:
     }
 
     fn generate_recommendation(
-        &self,
+        self,
         cx: &EarlyContext<'_>,
         binop: BinOpKind,
         node: &Expr,
@@ -150,10 +149,21 @@ fn generate_recommendation(
         None
     }
 
-    fn emit_warning(&self, cx: &EarlyContext<'_>, block: &Expr, recommendation: String) {
-        span_lint_and_then(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);
-        });
+    fn emit_warning(self, cx: &EarlyContext<'_>, block: &Expr, recommendation: String) {
+        span_lint_and_then(
+            cx,
+            INT_PLUS_ONE,
+            block.span,
+            "Unnecessary `>= y + 1` or `x - 1 >=`",
+            |db| {
+                db.span_suggestion(
+                    block.span,
+                    "change it to",
+                    recommendation,
+                    Applicability::MachineApplicable, // snippet
+                );
+            },
+        );
     }
 }