2017-09-17 16:18:12 +00:00
|
|
|
//! lint on blocks unnecessarily using >= with a + 1 or - 1
|
|
|
|
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
|
2019-04-08 20:43:55 +00:00
|
|
|
use rustc::{declare_lint_pass, declare_tool_lint};
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use syntax::ast::*;
|
2017-09-17 16:18:12 +00:00
|
|
|
|
2018-05-30 08:15:50 +00:00
|
|
|
use crate::utils::{snippet_opt, span_lint_and_then};
|
2017-09-17 16:18:12 +00:00
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **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
|
2019-08-02 06:13:54 +00:00
|
|
|
/// # let x = 1;
|
|
|
|
/// # let y = 1;
|
|
|
|
/// if x >= y + 1 {}
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```
|
|
|
|
///
|
2019-08-02 06:13:54 +00:00
|
|
|
/// Could be written as:
|
2019-03-05 16:50:33 +00:00
|
|
|
///
|
|
|
|
/// ```rust
|
2019-08-02 06:13:54 +00:00
|
|
|
/// # let x = 1;
|
|
|
|
/// # let y = 1;
|
|
|
|
/// if x > y {}
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```
|
2017-09-17 16:18:12 +00:00
|
|
|
pub INT_PLUS_ONE,
|
2018-03-28 13:24:26 +00:00
|
|
|
complexity,
|
2017-09-17 16:18:12 +00:00
|
|
|
"instead of using x >= y + 1, use x > y"
|
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
declare_lint_pass!(IntPlusOne => [INT_PLUS_ONE]);
|
2017-09-17 16:18:12 +00:00
|
|
|
|
|
|
|
// cases:
|
|
|
|
// BinOpKind::Ge
|
|
|
|
// x >= y + 1
|
|
|
|
// x - 1 >= y
|
|
|
|
//
|
|
|
|
// BinOpKind::Le
|
|
|
|
// x + 1 <= y
|
|
|
|
// x <= y - 1
|
|
|
|
|
2017-11-03 08:24:28 +00:00
|
|
|
#[derive(Copy, Clone)]
|
2017-09-23 18:32:11 +00:00
|
|
|
enum Side {
|
|
|
|
LHS,
|
|
|
|
RHS,
|
|
|
|
}
|
|
|
|
|
2017-09-17 16:18:12 +00:00
|
|
|
impl IntPlusOne {
|
2018-08-01 20:48:41 +00:00
|
|
|
#[allow(clippy::cast_sign_loss)]
|
2019-04-08 20:43:55 +00:00
|
|
|
fn check_lit(self, lit: &Lit, target_value: i128) -> bool {
|
2019-09-27 15:16:06 +00:00
|
|
|
if let LitKind::Int(value, ..) = lit.kind {
|
2017-11-04 19:55:56 +00:00
|
|
|
return value == (target_value as u128);
|
2017-09-17 16:18:12 +00:00
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
fn check_binop(self, cx: &EarlyContext<'_>, binop: BinOpKind, lhs: &Expr, rhs: &Expr) -> Option<String> {
|
2019-09-27 15:16:06 +00:00
|
|
|
match (binop, &lhs.kind, &rhs.kind) {
|
2017-09-17 16:18:12 +00:00
|
|
|
// case where `x - 1 >= ...` or `-1 + x >= ...`
|
|
|
|
(BinOpKind::Ge, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _) => {
|
2019-09-27 15:16:06 +00:00
|
|
|
match (lhskind.node, &lhslhs.kind, &lhsrhs.kind) {
|
2017-09-17 16:18:12 +00:00
|
|
|
// `-1 + x`
|
2017-11-04 19:55:56 +00:00
|
|
|
(BinOpKind::Add, &ExprKind::Lit(ref lit), _) if self.check_lit(lit, -1) => {
|
|
|
|
self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS)
|
|
|
|
},
|
2017-09-17 16:18:12 +00:00
|
|
|
// `x - 1`
|
2017-11-04 19:55:56 +00:00
|
|
|
(BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
|
|
|
|
self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS)
|
|
|
|
},
|
|
|
|
_ => None,
|
2017-09-17 16:18:12 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
// case where `... >= y + 1` or `... >= 1 + y`
|
2017-11-04 19:55:56 +00:00
|
|
|
(BinOpKind::Ge, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs))
|
|
|
|
if rhskind.node == BinOpKind::Add =>
|
|
|
|
{
|
2019-09-27 15:16:06 +00:00
|
|
|
match (&rhslhs.kind, &rhsrhs.kind) {
|
2017-09-17 16:18:12 +00:00
|
|
|
// `y + 1` and `1 + y`
|
2017-11-04 19:55:56 +00:00
|
|
|
(&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)
|
|
|
|
},
|
|
|
|
_ => None,
|
2017-09-17 16:18:12 +00:00
|
|
|
}
|
2017-11-04 19:55:56 +00:00
|
|
|
}
|
2017-09-17 16:18:12 +00:00
|
|
|
// case where `x + 1 <= ...` or `1 + x <= ...`
|
2017-11-04 19:55:56 +00:00
|
|
|
(BinOpKind::Le, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _)
|
|
|
|
if lhskind.node == BinOpKind::Add =>
|
|
|
|
{
|
2019-09-27 15:16:06 +00:00
|
|
|
match (&lhslhs.kind, &lhsrhs.kind) {
|
2017-09-17 16:18:12 +00:00
|
|
|
// `1 + x` and `x + 1`
|
2017-11-04 19:55:56 +00:00
|
|
|
(&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)
|
|
|
|
},
|
|
|
|
_ => None,
|
2017-09-17 16:18:12 +00:00
|
|
|
}
|
2017-11-04 19:55:56 +00:00
|
|
|
}
|
2017-09-17 16:18:12 +00:00
|
|
|
// case where `... >= y - 1` or `... >= -1 + y`
|
|
|
|
(BinOpKind::Le, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs)) => {
|
2019-09-27 15:16:06 +00:00
|
|
|
match (rhskind.node, &rhslhs.kind, &rhsrhs.kind) {
|
2017-09-17 16:18:12 +00:00
|
|
|
// `-1 + y`
|
2017-11-04 19:55:56 +00:00
|
|
|
(BinOpKind::Add, &ExprKind::Lit(ref lit), _) if self.check_lit(lit, -1) => {
|
|
|
|
self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS)
|
|
|
|
},
|
2017-09-17 16:18:12 +00:00
|
|
|
// `y - 1`
|
2017-11-04 19:55:56 +00:00
|
|
|
(BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
|
|
|
|
self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS)
|
|
|
|
},
|
|
|
|
_ => None,
|
2017-09-17 16:18:12 +00:00
|
|
|
}
|
|
|
|
},
|
2017-11-04 19:55:56 +00:00
|
|
|
_ => None,
|
2017-09-17 16:18:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-04 19:55:56 +00:00
|
|
|
fn generate_recommendation(
|
2019-04-08 20:43:55 +00:00
|
|
|
self,
|
2018-07-23 11:01:12 +00:00
|
|
|
cx: &EarlyContext<'_>,
|
2017-11-04 19:55:56 +00:00
|
|
|
binop: BinOpKind,
|
|
|
|
node: &Expr,
|
|
|
|
other_side: &Expr,
|
|
|
|
side: Side,
|
|
|
|
) -> Option<String> {
|
2017-09-23 18:32:11 +00:00
|
|
|
let binop_string = match binop {
|
|
|
|
BinOpKind::Ge => ">",
|
|
|
|
BinOpKind::Le => "<",
|
2017-11-04 19:55:56 +00:00
|
|
|
_ => return None,
|
2017-09-23 18:32:11 +00:00
|
|
|
};
|
|
|
|
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)),
|
|
|
|
};
|
|
|
|
return rec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
fn emit_warning(self, cx: &EarlyContext<'_>, block: &Expr, recommendation: String) {
|
2018-11-27 20:14:15 +00:00
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
INT_PLUS_ONE,
|
|
|
|
block.span,
|
|
|
|
"Unnecessary `>= y + 1` or `x - 1 >=`",
|
|
|
|
|db| {
|
2019-01-27 12:33:56 +00:00
|
|
|
db.span_suggestion(
|
2018-11-27 20:14:15 +00:00
|
|
|
block.span,
|
2019-08-28 05:29:30 +00:00
|
|
|
"change it to",
|
2018-11-27 20:14:15 +00:00
|
|
|
recommendation,
|
|
|
|
Applicability::MachineApplicable, // snippet
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2017-09-23 18:32:11 +00:00
|
|
|
}
|
2017-09-17 16:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EarlyLintPass for IntPlusOne {
|
2018-07-23 11:01:12 +00:00
|
|
|
fn check_expr(&mut self, cx: &EarlyContext<'_>, item: &Expr) {
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ExprKind::Binary(ref kind, ref lhs, ref rhs) = item.kind {
|
2017-09-24 11:31:12 +00:00
|
|
|
if let Some(ref rec) = self.check_binop(cx, kind.node, lhs, rhs) {
|
|
|
|
self.emit_warning(cx, item, rec.clone());
|
2017-09-24 09:30:29 +00:00
|
|
|
}
|
2017-09-17 16:18:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|