Skip to content

Commit b483787

Browse files
committed
Move SelfAssignment into Operators lint pass
1 parent 0b069c1 commit b483787

File tree

7 files changed

+57
-61
lines changed

7 files changed

+57
-61
lines changed

clippy_lints/src/lib.register_all.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
254254
LintId::of(operators::MODULO_ONE),
255255
LintId::of(operators::OP_REF),
256256
LintId::of(operators::PTR_EQ),
257+
LintId::of(operators::SELF_ASSIGNMENT),
257258
LintId::of(option_env_unwrap::OPTION_ENV_UNWRAP),
258259
LintId::of(overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL),
259260
LintId::of(partialeq_ne_impl::PARTIALEQ_NE_IMPL),
@@ -278,7 +279,6 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
278279
LintId::of(repeat_once::REPEAT_ONCE),
279280
LintId::of(returns::LET_AND_RETURN),
280281
LintId::of(returns::NEEDLESS_RETURN),
281-
LintId::of(self_assignment::SELF_ASSIGNMENT),
282282
LintId::of(self_named_constructors::SELF_NAMED_CONSTRUCTORS),
283283
LintId::of(serde_api::SERDE_API_MISUSE),
284284
LintId::of(significant_drop_in_scrutinee::SIGNIFICANT_DROP_IN_SCRUTINEE),

clippy_lints/src/lib.register_correctness.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ store.register_group(true, "clippy::correctness", Some("clippy_correctness"), ve
5151
LintId::of(operators::ERASING_OP),
5252
LintId::of(operators::INEFFECTIVE_BIT_MASK),
5353
LintId::of(operators::MODULO_ONE),
54+
LintId::of(operators::SELF_ASSIGNMENT),
5455
LintId::of(option_env_unwrap::OPTION_ENV_UNWRAP),
5556
LintId::of(ptr::INVALID_NULL_PTR_USAGE),
5657
LintId::of(ptr::MUT_FROM_REF),
5758
LintId::of(ranges::REVERSED_EMPTY_RANGES),
5859
LintId::of(regex::INVALID_REGEX),
59-
LintId::of(self_assignment::SELF_ASSIGNMENT),
6060
LintId::of(serde_api::SERDE_API_MISUSE),
6161
LintId::of(size_of_in_element_count::SIZE_OF_IN_ELEMENT_COUNT),
6262
LintId::of(swap::ALMOST_SWAPPED),

clippy_lints/src/lib.register_lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ store.register_lints(&[
425425
operators::NEEDLESS_BITWISE_BOOL,
426426
operators::OP_REF,
427427
operators::PTR_EQ,
428+
operators::SELF_ASSIGNMENT,
428429
operators::VERBOSE_BIT_MASK,
429430
option_env_unwrap::OPTION_ENV_UNWRAP,
430431
option_if_let_else::OPTION_IF_LET_ELSE,
@@ -470,7 +471,6 @@ store.register_lints(&[
470471
returns::LET_AND_RETURN,
471472
returns::NEEDLESS_RETURN,
472473
same_name_method::SAME_NAME_METHOD,
473-
self_assignment::SELF_ASSIGNMENT,
474474
self_named_constructors::SELF_NAMED_CONSTRUCTORS,
475475
semicolon_if_nothing_returned::SEMICOLON_IF_NOTHING_RETURNED,
476476
serde_api::SERDE_API_MISUSE,

clippy_lints/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,6 @@ mod repeat_once;
351351
mod return_self_not_must_use;
352352
mod returns;
353353
mod same_name_method;
354-
mod self_assignment;
355354
mod self_named_constructors;
356355
mod semicolon_if_nothing_returned;
357356
mod serde_api;
@@ -805,7 +804,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
805804
store.register_late_pass(|| Box::new(stable_sort_primitive::StableSortPrimitive));
806805
store.register_late_pass(|| Box::new(repeat_once::RepeatOnce));
807806
store.register_late_pass(|| Box::new(unwrap_in_result::UnwrapInResult));
808-
store.register_late_pass(|| Box::new(self_assignment::SelfAssignment));
809807
store.register_late_pass(|| Box::new(manual_unwrap_or::ManualUnwrapOr));
810808
store.register_late_pass(|| Box::new(manual_ok_or::ManualOkOr));
811809
store.register_late_pass(|| Box::new(semicolon_if_nothing_returned::SemicolonIfNothingReturned));

clippy_lints/src/operators/mod.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ mod modulo_one;
2222
mod needless_bitwise_bool;
2323
mod op_ref;
2424
mod ptr_eq;
25+
mod self_assignment;
2526
mod verbose_bit_mask;
2627

2728
declare_clippy_lint! {
@@ -686,6 +687,37 @@ declare_clippy_lint! {
686687
"use `std::ptr::eq` when comparing raw pointers"
687688
}
688689

690+
declare_clippy_lint! {
691+
/// ### What it does
692+
/// Checks for explicit self-assignments.
693+
///
694+
/// ### Why is this bad?
695+
/// Self-assignments are redundant and unlikely to be
696+
/// intentional.
697+
///
698+
/// ### Known problems
699+
/// If expression contains any deref coercions or
700+
/// indexing operations they are assumed not to have any side effects.
701+
///
702+
/// ### Example
703+
/// ```rust
704+
/// struct Event {
705+
/// id: usize,
706+
/// x: i32,
707+
/// y: i32,
708+
/// }
709+
///
710+
/// fn copy_position(a: &mut Event, b: &Event) {
711+
/// a.x = b.x;
712+
/// a.y = a.y;
713+
/// }
714+
/// ```
715+
#[clippy::version = "1.48.0"]
716+
pub SELF_ASSIGNMENT,
717+
correctness,
718+
"explicit self-assignment"
719+
}
720+
689721
pub struct Operators {
690722
arithmetic_context: arithmetic::Context,
691723
verbose_bit_mask_threshold: u64,
@@ -715,6 +747,7 @@ impl_lint_pass!(Operators => [
715747
MODULO_ARITHMETIC,
716748
NEEDLESS_BITWISE_BOOL,
717749
PTR_EQ,
750+
SELF_ASSIGNMENT,
718751
]);
719752
impl Operators {
720753
pub fn new(verbose_bit_mask_threshold: u64) -> Self {
@@ -760,6 +793,7 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
760793
},
761794
ExprKind::Assign(lhs, rhs, _) => {
762795
assign_op_pattern::check(cx, e, lhs, rhs);
796+
self_assignment::check(cx, e, lhs, rhs);
763797
},
764798
ExprKind::Unary(op, arg) => {
765799
if op == UnOp::Neg {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use clippy_utils::diagnostics::span_lint;
2+
use clippy_utils::eq_expr_value;
3+
use clippy_utils::source::snippet;
4+
use rustc_hir::Expr;
5+
use rustc_lint::LateContext;
6+
7+
use super::SELF_ASSIGNMENT;
8+
9+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, lhs: &'tcx Expr<'_>, rhs: &'tcx Expr<'_>) {
10+
if eq_expr_value(cx, lhs, rhs) {
11+
let lhs = snippet(cx, lhs.span, "<lhs>");
12+
let rhs = snippet(cx, rhs.span, "<rhs>");
13+
span_lint(
14+
cx,
15+
SELF_ASSIGNMENT,
16+
e.span,
17+
&format!("self-assignment of `{}` to `{}`", rhs, lhs),
18+
);
19+
}
20+
}

clippy_lints/src/self_assignment.rs

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)