2017-09-17 11:18:12 -05:00
|
|
|
//! lint on blocks unnecessarily using >= with a + 1 or - 1
|
|
|
|
|
2021-03-15 19:55:45 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2021-03-14 18:17:44 -05:00
|
|
|
use clippy_utils::source::snippet_opt;
|
2020-02-29 21:23:33 -06:00
|
|
|
use rustc_ast::ast::{BinOpKind, Expr, ExprKind, Lit, LitKind};
|
2018-12-29 09:04:45 -06:00
|
|
|
use rustc_errors::Applicability;
|
2020-01-12 00:08:41 -06:00
|
|
|
use rustc_lint::{EarlyContext, EarlyLintPass};
|
2020-01-11 05:37:08 -06:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2017-09-17 11:18:12 -05:00
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-02 13:37:11 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for usage of `x >= y + 1` or `x - 1 >= y` (and `<=`) in a block
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-02 13:37:11 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Readability -- better to use `> y` instead of `>= y + 1`.
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-02 13:37:11 -05:00
|
|
|
/// ### Example
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```rust
|
2019-08-02 01:13:54 -05:00
|
|
|
/// # let x = 1;
|
|
|
|
/// # let y = 1;
|
|
|
|
/// if x >= y + 1 {}
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```
|
|
|
|
///
|
2019-08-02 01:13:54 -05:00
|
|
|
/// Could be written as:
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
|
|
|
/// ```rust
|
2019-08-02 01:13:54 -05:00
|
|
|
/// # let x = 1;
|
|
|
|
/// # let y = 1;
|
|
|
|
/// if x > y {}
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```
|
Added `clippy::version` attribute to all normal lints
So, some context for this, well, more a story. I'm not used to scripting, I've never really scripted anything, even if it's a valuable skill. I just never really needed it. Now, `@flip1995` correctly suggested using a script for this in `rust-clippy#7813`...
And I decided to write a script using nushell because why not? This was a mistake... I spend way more time on this than I would like to admit. It has definitely been more than 4 hours. It shouldn't take that long, but me being new to scripting and nushell just wasn't a good mixture... Anyway, here is the script that creates another script which adds the versions. Fun...
Just execute this on the `gh-pages` branch and the resulting `replacer.sh` in `clippy_lints` and it should all work.
```nu
mv v0.0.212 rust-1.00.0;
mv beta rust-1.57.0;
mv master rust-1.58.0;
let paths = (open ./rust-1.58.0/lints.json | select id id_span | flatten | select id path);
let versions = (
ls | where name =~ "rust-" | select name | format {name}/lints.json |
each { open $it | select id | insert version $it | str substring "5,11" version} |
group-by id | rotate counter-clockwise id version |
update version {get version | first 1} | flatten | select id version);
$paths | each { |row|
let version = ($versions | where id == ($row.id) | format {version})
let idu = ($row.id | str upcase)
$"sed -i '0,/($idu),/{s/pub ($idu),/#[clippy::version = "($version)"]\n pub ($idu),/}' ($row.path)"
} | str collect ";" | str find-replace --all '1.00.0' 'pre 1.29.0' | save "replacer.sh";
```
And this still has some problems, but at this point I just want to be done -.-
2021-10-21 14:06:26 -05:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2017-09-17 11:18:12 -05:00
|
|
|
pub INT_PLUS_ONE,
|
2018-03-28 08:24:26 -05:00
|
|
|
complexity,
|
2020-01-06 00:30:43 -06:00
|
|
|
"instead of using `x >= y + 1`, use `x > y`"
|
2017-09-17 11:18:12 -05:00
|
|
|
}
|
|
|
|
|
2019-04-08 15:43:55 -05:00
|
|
|
declare_lint_pass!(IntPlusOne => [INT_PLUS_ONE]);
|
2017-09-17 11:18:12 -05:00
|
|
|
|
|
|
|
// cases:
|
|
|
|
// BinOpKind::Ge
|
|
|
|
// x >= y + 1
|
|
|
|
// x - 1 >= y
|
|
|
|
//
|
|
|
|
// BinOpKind::Le
|
|
|
|
// x + 1 <= y
|
|
|
|
// x <= y - 1
|
|
|
|
|
2017-11-03 03:24:28 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2017-09-23 13:32:11 -05:00
|
|
|
enum Side {
|
2020-12-19 07:50:45 -06:00
|
|
|
Lhs,
|
|
|
|
Rhs,
|
2017-09-23 13:32:11 -05:00
|
|
|
}
|
|
|
|
|
2017-09-17 11:18:12 -05:00
|
|
|
impl IntPlusOne {
|
2018-08-01 15:48:41 -05:00
|
|
|
#[allow(clippy::cast_sign_loss)]
|
2019-10-03 14:09:32 -05:00
|
|
|
fn check_lit(lit: &Lit, target_value: i128) -> bool {
|
2019-09-27 10:16:06 -05:00
|
|
|
if let LitKind::Int(value, ..) = lit.kind {
|
2017-11-04 14:55:56 -05:00
|
|
|
return value == (target_value as u128);
|
2017-09-17 11:18:12 -05:00
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2019-10-03 14:09:32 -05:00
|
|
|
fn check_binop(cx: &EarlyContext<'_>, binop: BinOpKind, lhs: &Expr, rhs: &Expr) -> Option<String> {
|
2019-09-27 10:16:06 -05:00
|
|
|
match (binop, &lhs.kind, &rhs.kind) {
|
2017-09-17 11:18:12 -05:00
|
|
|
// case where `x - 1 >= ...` or `-1 + x >= ...`
|
|
|
|
(BinOpKind::Ge, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _) => {
|
2019-09-27 10:16:06 -05:00
|
|
|
match (lhskind.node, &lhslhs.kind, &lhsrhs.kind) {
|
2017-09-17 11:18:12 -05:00
|
|
|
// `-1 + x`
|
2019-10-03 14:09:32 -05:00
|
|
|
(BinOpKind::Add, &ExprKind::Lit(ref lit), _) if Self::check_lit(lit, -1) => {
|
2020-12-19 07:50:45 -06:00
|
|
|
Self::generate_recommendation(cx, binop, lhsrhs, rhs, Side::Lhs)
|
2017-11-04 14:55:56 -05:00
|
|
|
},
|
2017-09-17 11:18:12 -05:00
|
|
|
// `x - 1`
|
2019-10-03 14:09:32 -05:00
|
|
|
(BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if Self::check_lit(lit, 1) => {
|
2020-12-19 07:50:45 -06:00
|
|
|
Self::generate_recommendation(cx, binop, lhslhs, rhs, Side::Lhs)
|
2017-11-04 14:55:56 -05:00
|
|
|
},
|
|
|
|
_ => None,
|
2017-09-17 11:18:12 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
// case where `... >= y + 1` or `... >= 1 + y`
|
2017-11-04 14:55:56 -05:00
|
|
|
(BinOpKind::Ge, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs))
|
|
|
|
if rhskind.node == BinOpKind::Add =>
|
|
|
|
{
|
2019-09-27 10:16:06 -05:00
|
|
|
match (&rhslhs.kind, &rhsrhs.kind) {
|
2017-09-17 11:18:12 -05:00
|
|
|
// `y + 1` and `1 + y`
|
2019-10-03 14:09:32 -05:00
|
|
|
(&ExprKind::Lit(ref lit), _) if Self::check_lit(lit, 1) => {
|
2020-12-19 07:50:45 -06:00
|
|
|
Self::generate_recommendation(cx, binop, rhsrhs, lhs, Side::Rhs)
|
2017-11-04 14:55:56 -05:00
|
|
|
},
|
2019-10-03 14:09:32 -05:00
|
|
|
(_, &ExprKind::Lit(ref lit)) if Self::check_lit(lit, 1) => {
|
2020-12-19 07:50:45 -06:00
|
|
|
Self::generate_recommendation(cx, binop, rhslhs, lhs, Side::Rhs)
|
2017-11-04 14:55:56 -05:00
|
|
|
},
|
|
|
|
_ => None,
|
2017-09-17 11:18:12 -05:00
|
|
|
}
|
2021-10-24 12:52:38 -05:00
|
|
|
},
|
2017-09-17 11:18:12 -05:00
|
|
|
// case where `x + 1 <= ...` or `1 + x <= ...`
|
2017-11-04 14:55:56 -05:00
|
|
|
(BinOpKind::Le, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _)
|
|
|
|
if lhskind.node == BinOpKind::Add =>
|
|
|
|
{
|
2019-09-27 10:16:06 -05:00
|
|
|
match (&lhslhs.kind, &lhsrhs.kind) {
|
2017-09-17 11:18:12 -05:00
|
|
|
// `1 + x` and `x + 1`
|
2019-10-03 14:09:32 -05:00
|
|
|
(&ExprKind::Lit(ref lit), _) if Self::check_lit(lit, 1) => {
|
2020-12-19 07:50:45 -06:00
|
|
|
Self::generate_recommendation(cx, binop, lhsrhs, rhs, Side::Lhs)
|
2017-11-04 14:55:56 -05:00
|
|
|
},
|
2019-10-03 14:09:32 -05:00
|
|
|
(_, &ExprKind::Lit(ref lit)) if Self::check_lit(lit, 1) => {
|
2020-12-19 07:50:45 -06:00
|
|
|
Self::generate_recommendation(cx, binop, lhslhs, rhs, Side::Lhs)
|
2017-11-04 14:55:56 -05:00
|
|
|
},
|
|
|
|
_ => None,
|
2017-09-17 11:18:12 -05:00
|
|
|
}
|
2021-10-24 12:52:38 -05:00
|
|
|
},
|
2017-09-17 11:18:12 -05:00
|
|
|
// case where `... >= y - 1` or `... >= -1 + y`
|
|
|
|
(BinOpKind::Le, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs)) => {
|
2019-09-27 10:16:06 -05:00
|
|
|
match (rhskind.node, &rhslhs.kind, &rhsrhs.kind) {
|
2017-09-17 11:18:12 -05:00
|
|
|
// `-1 + y`
|
2019-10-03 14:09:32 -05:00
|
|
|
(BinOpKind::Add, &ExprKind::Lit(ref lit), _) if Self::check_lit(lit, -1) => {
|
2020-12-19 07:50:45 -06:00
|
|
|
Self::generate_recommendation(cx, binop, rhsrhs, lhs, Side::Rhs)
|
2017-11-04 14:55:56 -05:00
|
|
|
},
|
2017-09-17 11:18:12 -05:00
|
|
|
// `y - 1`
|
2019-10-03 14:09:32 -05:00
|
|
|
(BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if Self::check_lit(lit, 1) => {
|
2020-12-19 07:50:45 -06:00
|
|
|
Self::generate_recommendation(cx, binop, rhslhs, lhs, Side::Rhs)
|
2017-11-04 14:55:56 -05:00
|
|
|
},
|
|
|
|
_ => None,
|
2017-09-17 11:18:12 -05:00
|
|
|
}
|
|
|
|
},
|
2017-11-04 14:55:56 -05:00
|
|
|
_ => None,
|
2017-09-17 11:18:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-04 14:55:56 -05:00
|
|
|
fn generate_recommendation(
|
2018-07-23 06:01:12 -05:00
|
|
|
cx: &EarlyContext<'_>,
|
2017-11-04 14:55:56 -05:00
|
|
|
binop: BinOpKind,
|
|
|
|
node: &Expr,
|
|
|
|
other_side: &Expr,
|
|
|
|
side: Side,
|
|
|
|
) -> Option<String> {
|
2017-09-23 13:32:11 -05:00
|
|
|
let binop_string = match binop {
|
|
|
|
BinOpKind::Ge => ">",
|
|
|
|
BinOpKind::Le => "<",
|
2017-11-04 14:55:56 -05:00
|
|
|
_ => return None,
|
2017-09-23 13:32:11 -05: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 {
|
2020-12-19 07:50:45 -06:00
|
|
|
Side::Lhs => Some(format!("{} {} {}", snippet, binop_string, other_side_snippet)),
|
|
|
|
Side::Rhs => Some(format!("{} {} {}", other_side_snippet, binop_string, snippet)),
|
2017-09-23 13:32:11 -05:00
|
|
|
};
|
|
|
|
return rec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2019-10-03 14:09:32 -05:00
|
|
|
fn emit_warning(cx: &EarlyContext<'_>, block: &Expr, recommendation: String) {
|
2020-04-17 09:01:25 -05:00
|
|
|
span_lint_and_sugg(
|
2018-11-27 14:14:15 -06:00
|
|
|
cx,
|
|
|
|
INT_PLUS_ONE,
|
|
|
|
block.span,
|
2020-08-11 08:14:32 -05:00
|
|
|
"unnecessary `>= y + 1` or `x - 1 >=`",
|
2020-04-17 09:01:25 -05:00
|
|
|
"change it to",
|
|
|
|
recommendation,
|
|
|
|
Applicability::MachineApplicable, // snippet
|
2018-11-27 14:14:15 -06:00
|
|
|
);
|
2017-09-23 13:32:11 -05:00
|
|
|
}
|
2017-09-17 11:18:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EarlyLintPass for IntPlusOne {
|
2018-07-23 06:01:12 -05:00
|
|
|
fn check_expr(&mut self, cx: &EarlyContext<'_>, item: &Expr) {
|
2019-09-27 10:16:06 -05:00
|
|
|
if let ExprKind::Binary(ref kind, ref lhs, ref rhs) = item.kind {
|
2020-08-11 08:14:32 -05:00
|
|
|
if let Some(rec) = Self::check_binop(cx, kind.node, lhs, rhs) {
|
|
|
|
Self::emit_warning(cx, item, rec);
|
2017-09-24 04:30:29 -05:00
|
|
|
}
|
2017-09-17 11:18:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|