2021-05-27 08:49:37 -05:00
|
|
|
use clippy_utils::consts::{self, Constant};
|
2021-12-19 02:48:25 -06:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2021-12-21 15:00:14 -06:00
|
|
|
use clippy_utils::source::snippet_with_applicability;
|
2018-11-27 14:14:15 -06:00
|
|
|
use if_chain::if_chain;
|
2021-12-19 02:48:25 -06:00
|
|
|
use rustc_errors::Applicability;
|
2021-12-21 15:00:14 -06:00
|
|
|
use rustc_hir::{BinOpKind, Expr, ExprKind, UnOp};
|
2020-01-12 00:08:41 -06:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-01-11 05:37:08 -06:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2020-01-04 04:00:00 -06:00
|
|
|
use rustc_span::source_map::Span;
|
2016-04-17 16:33:21 -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 multiplication by -1 as a form of negation.
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-02 13:37:11 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// It's more readable to just negate.
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-02 13:37:11 -05:00
|
|
|
/// ### Known problems
|
|
|
|
/// This only catches integers (for now).
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-02 13:37:11 -05:00
|
|
|
/// ### Example
|
2019-03-05 16:23:50 -06:00
|
|
|
/// ```ignore
|
2021-12-19 02:48:25 -06:00
|
|
|
/// // Bad
|
|
|
|
/// let a = x * -1;
|
|
|
|
///
|
|
|
|
/// // Good
|
|
|
|
/// let b = -x;
|
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"]
|
2016-04-17 16:33:21 -05:00
|
|
|
pub NEG_MULTIPLY,
|
2018-03-28 08:24:26 -05:00
|
|
|
style,
|
2021-12-21 15:00:14 -06:00
|
|
|
"multiplying integers by `-1`"
|
2016-04-17 16:33:21 -05:00
|
|
|
}
|
|
|
|
|
2019-04-08 15:43:55 -05:00
|
|
|
declare_lint_pass!(NegMultiply => [NEG_MULTIPLY]);
|
2016-04-17 16:33:21 -05:00
|
|
|
|
2018-08-01 15:48:41 -05:00
|
|
|
#[allow(clippy::match_same_arms)]
|
2020-06-25 15:41:36 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for NegMultiply {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
|
2021-04-02 16:35:32 -05:00
|
|
|
if let ExprKind::Binary(ref op, left, right) = e.kind {
|
2019-09-29 11:40:38 -05:00
|
|
|
if BinOpKind::Mul == op.node {
|
|
|
|
match (&left.kind, &right.kind) {
|
|
|
|
(&ExprKind::Unary(..), &ExprKind::Unary(..)) => {},
|
2021-04-02 16:35:32 -05:00
|
|
|
(&ExprKind::Unary(UnOp::Neg, lit), _) => check_mul(cx, e.span, lit, right),
|
|
|
|
(_, &ExprKind::Unary(UnOp::Neg, lit)) => check_mul(cx, e.span, lit, left),
|
2019-09-29 11:40:38 -05:00
|
|
|
_ => {},
|
|
|
|
}
|
2016-04-17 16:33:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
fn check_mul(cx: &LateContext<'_>, span: Span, lit: &Expr<'_>, exp: &Expr<'_>) {
|
2017-10-23 14:18:02 -05:00
|
|
|
if_chain! {
|
2019-09-27 10:16:06 -05:00
|
|
|
if let ExprKind::Lit(ref l) = lit.kind;
|
2021-10-04 01:33:40 -05:00
|
|
|
if consts::lit_to_constant(&l.node, cx.typeck_results().expr_ty_opt(lit)) == Constant::Int(1);
|
2020-07-17 03:47:04 -05:00
|
|
|
if cx.typeck_results().expr_ty(exp).is_integral();
|
2021-12-19 02:48:25 -06:00
|
|
|
|
2017-10-23 14:18:02 -05:00
|
|
|
then {
|
2021-12-21 15:00:14 -06:00
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
|
|
|
let suggestion = format!("-{}", snippet_with_applicability(cx, exp.span, "..", &mut applicability));
|
2021-12-19 02:48:25 -06:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
NEG_MULTIPLY,
|
|
|
|
span,
|
2021-12-21 15:00:14 -06:00
|
|
|
"this multiplication by -1 can be written more succinctly",
|
2021-12-19 02:48:25 -06:00
|
|
|
"consider using",
|
|
|
|
suggestion,
|
|
|
|
applicability,
|
|
|
|
);
|
2017-10-23 14:18:02 -05:00
|
|
|
}
|
|
|
|
}
|
2016-04-17 16:33:21 -05:00
|
|
|
}
|