Skip to content

Commit a4617e8

Browse files
committed
Move SelfAssignment into Operators lint pass
1 parent 89a44aa commit a4617e8

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
@@ -255,6 +255,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
255255
LintId::of(operators::MODULO_ONE),
256256
LintId::of(operators::OP_REF),
257257
LintId::of(operators::PTR_EQ),
258+
LintId::of(operators::SELF_ASSIGNMENT),
258259
LintId::of(option_env_unwrap::OPTION_ENV_UNWRAP),
259260
LintId::of(overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL),
260261
LintId::of(partialeq_ne_impl::PARTIALEQ_NE_IMPL),
@@ -279,7 +280,6 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
279280
LintId::of(repeat_once::REPEAT_ONCE),
280281
LintId::of(returns::LET_AND_RETURN),
281282
LintId::of(returns::NEEDLESS_RETURN),
282-
LintId::of(self_assignment::SELF_ASSIGNMENT),
283283
LintId::of(self_named_constructors::SELF_NAMED_CONSTRUCTORS),
284284
LintId::of(serde_api::SERDE_API_MISUSE),
285285
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
@@ -427,6 +427,7 @@ store.register_lints(&[
427427
operators::NEEDLESS_BITWISE_BOOL,
428428
operators::OP_REF,
429429
operators::PTR_EQ,
430+
operators::SELF_ASSIGNMENT,
430431
operators::VERBOSE_BIT_MASK,
431432
option_env_unwrap::OPTION_ENV_UNWRAP,
432433
option_if_let_else::OPTION_IF_LET_ELSE,
@@ -472,7 +473,6 @@ store.register_lints(&[
472473
returns::LET_AND_RETURN,
473474
returns::NEEDLESS_RETURN,
474475
same_name_method::SAME_NAME_METHOD,
475-
self_assignment::SELF_ASSIGNMENT,
476476
self_named_constructors::SELF_NAMED_CONSTRUCTORS,
477477
semicolon_if_nothing_returned::SEMICOLON_IF_NOTHING_RETURNED,
478478
serde_api::SERDE_API_MISUSE,

clippy_lints/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,6 @@ mod repeat_once;
353353
mod return_self_not_must_use;
354354
mod returns;
355355
mod same_name_method;
356-
mod self_assignment;
357356
mod self_named_constructors;
358357
mod semicolon_if_nothing_returned;
359358
mod serde_api;
@@ -808,7 +807,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
808807
store.register_late_pass(|| Box::new(stable_sort_primitive::StableSortPrimitive));
809808
store.register_late_pass(|| Box::new(repeat_once::RepeatOnce));
810809
store.register_late_pass(|| Box::new(unwrap_in_result::UnwrapInResult));
811-
store.register_late_pass(|| Box::new(self_assignment::SelfAssignment));
812810
store.register_late_pass(|| Box::new(manual_unwrap_or::ManualUnwrapOr));
813811
store.register_late_pass(|| Box::new(manual_ok_or::ManualOkOr));
814812
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 needless_bitwise_bool;
2222
mod numeric_arithmetic;
2323
mod op_ref;
2424
mod ptr_eq;
25+
mod self_assignment;
2526
mod verbose_bit_mask;
2627

2728
declare_clippy_lint! {
@@ -700,6 +701,37 @@ declare_clippy_lint! {
700701
"use `std::ptr::eq` when comparing raw pointers"
701702
}
702703

704+
declare_clippy_lint! {
705+
/// ### What it does
706+
/// Checks for explicit self-assignments.
707+
///
708+
/// ### Why is this bad?
709+
/// Self-assignments are redundant and unlikely to be
710+
/// intentional.
711+
///
712+
/// ### Known problems
713+
/// If expression contains any deref coercions or
714+
/// indexing operations they are assumed not to have any side effects.
715+
///
716+
/// ### Example
717+
/// ```rust
718+
/// struct Event {
719+
/// id: usize,
720+
/// x: i32,
721+
/// y: i32,
722+
/// }
723+
///
724+
/// fn copy_position(a: &mut Event, b: &Event) {
725+
/// a.x = b.x;
726+
/// a.y = a.y;
727+
/// }
728+
/// ```
729+
#[clippy::version = "1.48.0"]
730+
pub SELF_ASSIGNMENT,
731+
correctness,
732+
"explicit self-assignment"
733+
}
734+
703735
pub struct Operators {
704736
arithmetic_context: numeric_arithmetic::Context,
705737
verbose_bit_mask_threshold: u64,
@@ -729,6 +761,7 @@ impl_lint_pass!(Operators => [
729761
MODULO_ARITHMETIC,
730762
NEEDLESS_BITWISE_BOOL,
731763
PTR_EQ,
764+
SELF_ASSIGNMENT,
732765
]);
733766
impl Operators {
734767
pub fn new(verbose_bit_mask_threshold: u64) -> Self {
@@ -774,6 +807,7 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
774807
},
775808
ExprKind::Assign(lhs, rhs, _) => {
776809
assign_op_pattern::check(cx, e, lhs, rhs);
810+
self_assignment::check(cx, e, lhs, rhs);
777811
},
778812
ExprKind::Unary(op, arg) => {
779813
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)