]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/int_plus_one.rs
Rollup merge of #83092 - petrochenkov:qspan, r=estebank
[rust.git] / clippy_lints / src / int_plus_one.rs
index f2781b734cf61691220fe7602b8f2a2fbf539a03..260b8988d371113adf83b4801033215edc05e5c3 100644 (file)
@@ -5,12 +5,11 @@
 use rustc_lint::{EarlyContext, EarlyLintPass};
 use rustc_session::{declare_lint_pass, declare_tool_lint};
 
-use crate::utils::{snippet_opt, span_lint_and_then};
+use crate::utils::{snippet_opt, span_lint_and_sugg};
 
 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.
@@ -47,8 +46,8 @@
 
 #[derive(Copy, Clone)]
 enum Side {
-    LHS,
-    RHS,
+    Lhs,
+    Rhs,
 }
 
 impl IntPlusOne {
@@ -67,11 +66,11 @@ fn check_binop(cx: &EarlyContext<'_>, binop: BinOpKind, lhs: &Expr, rhs: &Expr)
                 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)
+                        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)
+                        Self::generate_recommendation(cx, binop, lhslhs, rhs, Side::Lhs)
                     },
                     _ => None,
                 }
@@ -83,10 +82,10 @@ fn check_binop(cx: &EarlyContext<'_>, binop: BinOpKind, lhs: &Expr, rhs: &Expr)
                 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)
+                        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)
+                        Self::generate_recommendation(cx, binop, rhslhs, lhs, Side::Rhs)
                     },
                     _ => None,
                 }
@@ -98,10 +97,10 @@ fn check_binop(cx: &EarlyContext<'_>, binop: BinOpKind, lhs: &Expr, rhs: &Expr)
                 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)
+                        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)
+                        Self::generate_recommendation(cx, binop, lhslhs, rhs, Side::Lhs)
                     },
                     _ => None,
                 }
@@ -111,11 +110,11 @@ fn check_binop(cx: &EarlyContext<'_>, binop: BinOpKind, lhs: &Expr, rhs: &Expr)
                 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)
+                        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)
+                        Self::generate_recommendation(cx, binop, rhslhs, lhs, Side::Rhs)
                     },
                     _ => None,
                 }
@@ -139,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,19 +148,14 @@ fn generate_recommendation(
     }
 
     fn emit_warning(cx: &EarlyContext<'_>, block: &Expr, recommendation: String) {
-        span_lint_and_then(
+        span_lint_and_sugg(
             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
-                );
-            },
+            "unnecessary `>= y + 1` or `x - 1 >=`",
+            "change it to",
+            recommendation,
+            Applicability::MachineApplicable, // snippet
         );
     }
 }
@@ -169,8 +163,8 @@ fn emit_warning(cx: &EarlyContext<'_>, block: &Expr, recommendation: String) {
 impl EarlyLintPass for IntPlusOne {
     fn check_expr(&mut self, cx: &EarlyContext<'_>, item: &Expr) {
         if let ExprKind::Binary(ref kind, ref lhs, ref rhs) = item.kind {
-            if let Some(ref rec) = Self::check_binop(cx, kind.node, lhs, rhs) {
-                Self::emit_warning(cx, item, rec.clone());
+            if let Some(rec) = Self::check_binop(cx, kind.node, lhs, rhs) {
+                Self::emit_warning(cx, item, rec);
             }
         }
     }