Skip to content

Commit e973fc1

Browse files
committed
Move SelfAssignment into Operators lint pass
1 parent 3e943c9 commit e973fc1

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
@@ -257,6 +257,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
257257
LintId::of(operators::MODULO_ONE),
258258
LintId::of(operators::OP_REF),
259259
LintId::of(operators::PTR_EQ),
260+
LintId::of(operators::SELF_ASSIGNMENT),
260261
LintId::of(option_env_unwrap::OPTION_ENV_UNWRAP),
261262
LintId::of(overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL),
262263
LintId::of(partialeq_ne_impl::PARTIALEQ_NE_IMPL),
@@ -281,7 +282,6 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
281282
LintId::of(repeat_once::REPEAT_ONCE),
282283
LintId::of(returns::LET_AND_RETURN),
283284
LintId::of(returns::NEEDLESS_RETURN),
284-
LintId::of(self_assignment::SELF_ASSIGNMENT),
285285
LintId::of(self_named_constructors::SELF_NAMED_CONSTRUCTORS),
286286
LintId::of(serde_api::SERDE_API_MISUSE),
287287
LintId::of(single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS),

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
@@ -431,6 +431,7 @@ store.register_lints(&[
431431
operators::NEEDLESS_BITWISE_BOOL,
432432
operators::OP_REF,
433433
operators::PTR_EQ,
434+
operators::SELF_ASSIGNMENT,
434435
operators::VERBOSE_BIT_MASK,
435436
option_env_unwrap::OPTION_ENV_UNWRAP,
436437
option_if_let_else::OPTION_IF_LET_ELSE,
@@ -476,7 +477,6 @@ store.register_lints(&[
476477
returns::LET_AND_RETURN,
477478
returns::NEEDLESS_RETURN,
478479
same_name_method::SAME_NAME_METHOD,
479-
self_assignment::SELF_ASSIGNMENT,
480480
self_named_constructors::SELF_NAMED_CONSTRUCTORS,
481481
semicolon_if_nothing_returned::SEMICOLON_IF_NOTHING_RETURNED,
482482
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;
@@ -802,7 +801,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
802801
store.register_late_pass(|| Box::new(stable_sort_primitive::StableSortPrimitive));
803802
store.register_late_pass(|| Box::new(repeat_once::RepeatOnce));
804803
store.register_late_pass(|| Box::new(unwrap_in_result::UnwrapInResult));
805-
store.register_late_pass(|| Box::new(self_assignment::SelfAssignment));
806804
store.register_late_pass(|| Box::new(manual_ok_or::ManualOkOr));
807805
store.register_late_pass(|| Box::new(semicolon_if_nothing_returned::SemicolonIfNothingReturned));
808806
store.register_late_pass(|| Box::new(async_yields_async::AsyncYieldsAsync));

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! {
@@ -701,6 +702,37 @@ declare_clippy_lint! {
701702
"use `std::ptr::eq` when comparing raw pointers"
702703
}
703704

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