Skip to content

Commit a9a1fe8

Browse files
committed
Move IdentityOp into Operators lint pass
1 parent 3bd75b5 commit a9a1fe8

File tree

6 files changed

+58
-59
lines changed

6 files changed

+58
-59
lines changed

clippy_lints/src/lib.register_all.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
8282
LintId::of(functions::RESULT_UNIT_ERR),
8383
LintId::of(functions::TOO_MANY_ARGUMENTS),
8484
LintId::of(get_first::GET_FIRST),
85-
LintId::of(identity_op::IDENTITY_OP),
8685
LintId::of(if_let_mutex::IF_LET_MUTEX),
8786
LintId::of(indexing_slicing::OUT_OF_BOUNDS_INDEXING),
8887
LintId::of(infinite_iter::INFINITE_ITER),
@@ -250,6 +249,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
250249
LintId::of(operators::EQ_OP),
251250
LintId::of(operators::ERASING_OP),
252251
LintId::of(operators::FLOAT_EQUALITY_WITHOUT_ABS),
252+
LintId::of(operators::IDENTITY_OP),
253253
LintId::of(operators::INEFFECTIVE_BIT_MASK),
254254
LintId::of(operators::MISREFACTORED_ASSIGN_OP),
255255
LintId::of(operators::OP_REF),

clippy_lints/src/lib.register_complexity.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ store.register_group(true, "clippy::complexity", Some("clippy_complexity"), vec!
1313
LintId::of(explicit_write::EXPLICIT_WRITE),
1414
LintId::of(format::USELESS_FORMAT),
1515
LintId::of(functions::TOO_MANY_ARGUMENTS),
16-
LintId::of(identity_op::IDENTITY_OP),
1716
LintId::of(int_plus_one::INT_PLUS_ONE),
1817
LintId::of(lifetimes::EXTRA_UNUSED_LIFETIMES),
1918
LintId::of(lifetimes::NEEDLESS_LIFETIMES),
@@ -68,6 +67,7 @@ store.register_group(true, "clippy::complexity", Some("clippy_complexity"), vec!
6867
LintId::of(no_effect::UNNECESSARY_OPERATION),
6968
LintId::of(operators::DOUBLE_COMPARISONS),
7069
LintId::of(operators::DURATION_SUBSEC),
70+
LintId::of(operators::IDENTITY_OP),
7171
LintId::of(overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL),
7272
LintId::of(partialeq_ne_impl::PARTIALEQ_NE_IMPL),
7373
LintId::of(precedence::PRECEDENCE),

clippy_lints/src/lib.register_lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ store.register_lints(&[
172172
functions::TOO_MANY_LINES,
173173
future_not_send::FUTURE_NOT_SEND,
174174
get_first::GET_FIRST,
175-
identity_op::IDENTITY_OP,
176175
if_let_mutex::IF_LET_MUTEX,
177176
if_not_else::IF_NOT_ELSE,
178177
if_then_some_else_none::IF_THEN_SOME_ELSE_NONE,
@@ -420,6 +419,7 @@ store.register_lints(&[
420419
operators::ERASING_OP,
421420
operators::FLOAT_ARITHMETIC,
422421
operators::FLOAT_EQUALITY_WITHOUT_ABS,
422+
operators::IDENTITY_OP,
423423
operators::INEFFECTIVE_BIT_MASK,
424424
operators::INTEGER_ARITHMETIC,
425425
operators::MISREFACTORED_ASSIGN_OP,

clippy_lints/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@ mod from_str_radix_10;
236236
mod functions;
237237
mod future_not_send;
238238
mod get_first;
239-
mod identity_op;
240239
mod if_let_mutex;
241240
mod if_not_else;
242241
mod if_then_some_else_none;
@@ -548,7 +547,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
548547
store.register_late_pass(|| Box::new(needless_for_each::NeedlessForEach));
549548
store.register_late_pass(|| Box::new(misc::MiscLints));
550549
store.register_late_pass(|| Box::new(eta_reduction::EtaReduction));
551-
store.register_late_pass(|| Box::new(identity_op::IdentityOp));
552550
store.register_late_pass(|| Box::new(mut_mut::MutMut));
553551
store.register_late_pass(|| Box::new(mut_reference::UnnecessaryMutPassed));
554552
store.register_late_pass(|| Box::new(len_zero::LenZero));

clippy_lints/src/identity_op.rs renamed to clippy_lints/src/operators/identity_op.rs

Lines changed: 33 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,40 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::source::snippet_with_applicability;
44
use clippy_utils::{clip, unsext};
55
use rustc_errors::Applicability;
6-
use rustc_hir::{BinOp, BinOpKind, Expr, ExprKind, Node};
7-
use rustc_lint::{LateContext, LateLintPass};
6+
use rustc_hir::{BinOpKind, Expr, ExprKind, Node};
7+
use rustc_lint::LateContext;
88
use rustc_middle::ty;
9-
use rustc_session::{declare_lint_pass, declare_tool_lint};
109
use rustc_span::source_map::Span;
1110

12-
declare_clippy_lint! {
13-
/// ### What it does
14-
/// Checks for identity operations, e.g., `x + 0`.
15-
///
16-
/// ### Why is this bad?
17-
/// This code can be removed without changing the
18-
/// meaning. So it just obscures what's going on. Delete it mercilessly.
19-
///
20-
/// ### Example
21-
/// ```rust
22-
/// # let x = 1;
23-
/// x / 1 + 0 * 1 - 0 | 0;
24-
/// ```
25-
#[clippy::version = "pre 1.29.0"]
26-
pub IDENTITY_OP,
27-
complexity,
28-
"using identity operations, e.g., `x + 0` or `y / 1`"
29-
}
30-
31-
declare_lint_pass!(IdentityOp => [IDENTITY_OP]);
11+
use super::IDENTITY_OP;
3212

33-
impl<'tcx> LateLintPass<'tcx> for IdentityOp {
34-
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
35-
if expr.span.from_expansion() {
36-
return;
37-
}
38-
if let ExprKind::Binary(cmp, left, right) = &expr.kind {
39-
if !is_allowed(cx, *cmp, left, right) {
40-
match cmp.node {
41-
BinOpKind::Add | BinOpKind::BitOr | BinOpKind::BitXor => {
42-
check(cx, left, 0, expr.span, right.span, needs_parenthesis(cx, expr, right));
43-
check(cx, right, 0, expr.span, left.span, Parens::Unneeded);
44-
},
45-
BinOpKind::Shl | BinOpKind::Shr | BinOpKind::Sub => {
46-
check(cx, right, 0, expr.span, left.span, Parens::Unneeded);
47-
},
48-
BinOpKind::Mul => {
49-
check(cx, left, 1, expr.span, right.span, needs_parenthesis(cx, expr, right));
50-
check(cx, right, 1, expr.span, left.span, Parens::Unneeded);
51-
},
52-
BinOpKind::Div => check(cx, right, 1, expr.span, left.span, Parens::Unneeded),
53-
BinOpKind::BitAnd => {
54-
check(cx, left, -1, expr.span, right.span, needs_parenthesis(cx, expr, right));
55-
check(cx, right, -1, expr.span, left.span, Parens::Unneeded);
56-
},
57-
BinOpKind::Rem => check_remainder(cx, left, right, expr.span, left.span),
58-
_ => (),
59-
}
60-
}
13+
pub(crate) fn check<'tcx>(
14+
cx: &LateContext<'tcx>,
15+
expr: &'tcx Expr<'_>,
16+
op: BinOpKind,
17+
left: &'tcx Expr<'_>,
18+
right: &'tcx Expr<'_>,
19+
) {
20+
if !is_allowed(cx, op, left, right) {
21+
match op {
22+
BinOpKind::Add | BinOpKind::BitOr | BinOpKind::BitXor => {
23+
check_op(cx, left, 0, expr.span, right.span, needs_parenthesis(cx, expr, right));
24+
check_op(cx, right, 0, expr.span, left.span, Parens::Unneeded);
25+
},
26+
BinOpKind::Shl | BinOpKind::Shr | BinOpKind::Sub => {
27+
check_op(cx, right, 0, expr.span, left.span, Parens::Unneeded);
28+
},
29+
BinOpKind::Mul => {
30+
check_op(cx, left, 1, expr.span, right.span, needs_parenthesis(cx, expr, right));
31+
check_op(cx, right, 1, expr.span, left.span, Parens::Unneeded);
32+
},
33+
BinOpKind::Div => check_op(cx, right, 1, expr.span, left.span, Parens::Unneeded),
34+
BinOpKind::BitAnd => {
35+
check_op(cx, left, -1, expr.span, right.span, needs_parenthesis(cx, expr, right));
36+
check_op(cx, right, -1, expr.span, left.span, Parens::Unneeded);
37+
},
38+
BinOpKind::Rem => check_remainder(cx, left, right, expr.span, left.span),
39+
_ => (),
6140
}
6241
}
6342
}
@@ -108,12 +87,12 @@ fn needs_parenthesis(cx: &LateContext<'_>, binary: &Expr<'_>, right: &Expr<'_>)
10887
Parens::Needed
10988
}
11089

111-
fn is_allowed(cx: &LateContext<'_>, cmp: BinOp, left: &Expr<'_>, right: &Expr<'_>) -> bool {
90+
fn is_allowed(cx: &LateContext<'_>, cmp: BinOpKind, left: &Expr<'_>, right: &Expr<'_>) -> bool {
11291
// This lint applies to integers
11392
!cx.typeck_results().expr_ty(left).peel_refs().is_integral()
11493
|| !cx.typeck_results().expr_ty(right).peel_refs().is_integral()
11594
// `1 << 0` is a common pattern in bit manipulation code
116-
|| (cmp.node == BinOpKind::Shl
95+
|| (cmp == BinOpKind::Shl
11796
&& constant_simple(cx, cx.typeck_results(), right) == Some(Constant::Int(0))
11897
&& constant_simple(cx, cx.typeck_results(), left) == Some(Constant::Int(1)))
11998
}
@@ -130,7 +109,7 @@ fn check_remainder(cx: &LateContext<'_>, left: &Expr<'_>, right: &Expr<'_>, span
130109
}
131110
}
132111

133-
fn check(cx: &LateContext<'_>, e: &Expr<'_>, m: i8, span: Span, arg: Span, parens: Parens) {
112+
fn check_op(cx: &LateContext<'_>, e: &Expr<'_>, m: i8, span: Span, arg: Span, parens: Parens) {
134113
if let Some(Constant::Int(v)) = constant_simple(cx, cx.typeck_results(), e).map(Constant::peel_refs) {
135114
let check = match *cx.typeck_results().expr_ty(e).peel_refs().kind() {
136115
ty::Int(ity) => unsext(cx.tcx, -1_i128, ity),

clippy_lints/src/operators/mod.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod duration_subsec;
1111
mod eq_op;
1212
mod erasing_op;
1313
mod float_equality_without_abs;
14+
mod identity_op;
1415
mod misrefactored_assign_op;
1516
mod op_ref;
1617
mod verbose_bit_mask;
@@ -411,6 +412,25 @@ declare_clippy_lint! {
411412
"float equality check without `.abs()`"
412413
}
413414

415+
declare_clippy_lint! {
416+
/// ### What it does
417+
/// Checks for identity operations, e.g., `x + 0`.
418+
///
419+
/// ### Why is this bad?
420+
/// This code can be removed without changing the
421+
/// meaning. So it just obscures what's going on. Delete it mercilessly.
422+
///
423+
/// ### Example
424+
/// ```rust
425+
/// # let x = 1;
426+
/// x / 1 + 0 * 1 - 0 | 0;
427+
/// ```
428+
#[clippy::version = "pre 1.29.0"]
429+
pub IDENTITY_OP,
430+
complexity,
431+
"using identity operations, e.g., `x + 0` or `y / 1`"
432+
}
433+
414434
pub struct Operators {
415435
arithmetic_context: arithmetic::Context,
416436
verbose_bit_mask_threshold: u64,
@@ -430,6 +450,7 @@ impl_lint_pass!(Operators => [
430450
OP_REF,
431451
ERASING_OP,
432452
FLOAT_EQUALITY_WITHOUT_ABS,
453+
IDENTITY_OP,
433454
]);
434455
impl Operators {
435456
pub fn new(verbose_bit_mask_threshold: u64) -> Self {
@@ -451,6 +472,7 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
451472
op_ref::check(cx, e, op.node, lhs, rhs);
452473
}
453474
erasing_op::check(cx, e, op.node, lhs, rhs);
475+
identity_op::check(cx, e, op.node, lhs, rhs);
454476
}
455477
self.arithmetic_context.check_binary(cx, e, op.node, lhs, rhs);
456478
bit_mask::check(cx, e, op.node, lhs, rhs);

0 commit comments

Comments
 (0)