Skip to content

Commit 24d4e55

Browse files
committed
Move IdentityOp into Operators lint pass
1 parent 2fb7ff4 commit 24d4e55

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),
@@ -252,6 +251,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
252251
LintId::of(operators::EQ_OP),
253252
LintId::of(operators::ERASING_OP),
254253
LintId::of(operators::FLOAT_EQUALITY_WITHOUT_ABS),
254+
LintId::of(operators::IDENTITY_OP),
255255
LintId::of(operators::INEFFECTIVE_BIT_MASK),
256256
LintId::of(operators::MISREFACTORED_ASSIGN_OP),
257257
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
@@ -14,7 +14,6 @@ store.register_group(true, "clippy::complexity", Some("clippy_complexity"), vec!
1414
LintId::of(explicit_write::EXPLICIT_WRITE),
1515
LintId::of(format::USELESS_FORMAT),
1616
LintId::of(functions::TOO_MANY_ARGUMENTS),
17-
LintId::of(identity_op::IDENTITY_OP),
1817
LintId::of(int_plus_one::INT_PLUS_ONE),
1918
LintId::of(lifetimes::EXTRA_UNUSED_LIFETIMES),
2019
LintId::of(lifetimes::NEEDLESS_LIFETIMES),
@@ -69,6 +68,7 @@ store.register_group(true, "clippy::complexity", Some("clippy_complexity"), vec!
6968
LintId::of(no_effect::UNNECESSARY_OPERATION),
7069
LintId::of(operators::DOUBLE_COMPARISONS),
7170
LintId::of(operators::DURATION_SUBSEC),
71+
LintId::of(operators::IDENTITY_OP),
7272
LintId::of(overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL),
7373
LintId::of(partialeq_ne_impl::PARTIALEQ_NE_IMPL),
7474
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
@@ -173,7 +173,6 @@ store.register_lints(&[
173173
functions::TOO_MANY_LINES,
174174
future_not_send::FUTURE_NOT_SEND,
175175
get_first::GET_FIRST,
176-
identity_op::IDENTITY_OP,
177176
if_let_mutex::IF_LET_MUTEX,
178177
if_not_else::IF_NOT_ELSE,
179178
if_then_some_else_none::IF_THEN_SOME_ELSE_NONE,
@@ -425,6 +424,7 @@ store.register_lints(&[
425424
operators::ERASING_OP,
426425
operators::FLOAT_ARITHMETIC,
427426
operators::FLOAT_EQUALITY_WITHOUT_ABS,
427+
operators::IDENTITY_OP,
428428
operators::INEFFECTIVE_BIT_MASK,
429429
operators::INTEGER_ARITHMETIC,
430430
operators::MISREFACTORED_ASSIGN_OP,

clippy_lints/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ mod from_str_radix_10;
237237
mod functions;
238238
mod future_not_send;
239239
mod get_first;
240-
mod identity_op;
241240
mod if_let_mutex;
242241
mod if_not_else;
243242
mod if_then_some_else_none;
@@ -544,7 +543,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
544543
store.register_late_pass(|| Box::new(needless_for_each::NeedlessForEach));
545544
store.register_late_pass(|| Box::new(misc::MiscLints));
546545
store.register_late_pass(|| Box::new(eta_reduction::EtaReduction));
547-
store.register_late_pass(|| Box::new(identity_op::IdentityOp));
548546
store.register_late_pass(|| Box::new(mut_mut::MutMut));
549547
store.register_late_pass(|| Box::new(mut_reference::UnnecessaryMutPassed));
550548
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
@@ -10,6 +10,7 @@ mod duration_subsec;
1010
mod eq_op;
1111
mod erasing_op;
1212
mod float_equality_without_abs;
13+
mod identity_op;
1314
mod misrefactored_assign_op;
1415
mod numeric_arithmetic;
1516
mod op_ref;
@@ -419,6 +420,25 @@ declare_clippy_lint! {
419420
"float equality check without `.abs()`"
420421
}
421422

423+
declare_clippy_lint! {
424+
/// ### What it does
425+
/// Checks for identity operations, e.g., `x + 0`.
426+
///
427+
/// ### Why is this bad?
428+
/// This code can be removed without changing the
429+
/// meaning. So it just obscures what's going on. Delete it mercilessly.
430+
///
431+
/// ### Example
432+
/// ```rust
433+
/// # let x = 1;
434+
/// x / 1 + 0 * 1 - 0 | 0;
435+
/// ```
436+
#[clippy::version = "pre 1.29.0"]
437+
pub IDENTITY_OP,
438+
complexity,
439+
"using identity operations, e.g., `x + 0` or `y / 1`"
440+
}
441+
422442
pub struct Operators {
423443
arithmetic_context: numeric_arithmetic::Context,
424444
verbose_bit_mask_threshold: u64,
@@ -438,6 +458,7 @@ impl_lint_pass!(Operators => [
438458
OP_REF,
439459
ERASING_OP,
440460
FLOAT_EQUALITY_WITHOUT_ABS,
461+
IDENTITY_OP,
441462
]);
442463
impl Operators {
443464
pub fn new(verbose_bit_mask_threshold: u64) -> Self {
@@ -459,6 +480,7 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
459480
op_ref::check(cx, e, op.node, lhs, rhs);
460481
}
461482
erasing_op::check(cx, e, op.node, lhs, rhs);
483+
identity_op::check(cx, e, op.node, lhs, rhs);
462484
}
463485
self.arithmetic_context.check_binary(cx, e, op.node, lhs, rhs);
464486
bit_mask::check(cx, e, op.node, lhs, rhs);

0 commit comments

Comments
 (0)