Skip to content

Commit ffbba85

Browse files
committed
Move SelfAssignment into Operators lint pass
1 parent 92891a0 commit ffbba85

File tree

7 files changed

+65
-69
lines changed

7 files changed

+65
-69
lines changed

clippy_lints/src/lib.register_all.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
261261
LintId::of(operators::MODULO_ONE),
262262
LintId::of(operators::OP_REF),
263263
LintId::of(operators::PTR_EQ),
264+
LintId::of(operators::SELF_ASSIGNMENT),
264265
LintId::of(option_env_unwrap::OPTION_ENV_UNWRAP),
265266
LintId::of(overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL),
266267
LintId::of(partialeq_ne_impl::PARTIALEQ_NE_IMPL),
@@ -286,7 +287,6 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
286287
LintId::of(repeat_once::REPEAT_ONCE),
287288
LintId::of(returns::LET_AND_RETURN),
288289
LintId::of(returns::NEEDLESS_RETURN),
289-
LintId::of(self_assignment::SELF_ASSIGNMENT),
290290
LintId::of(self_named_constructors::SELF_NAMED_CONSTRUCTORS),
291291
LintId::of(serde_api::SERDE_API_MISUSE),
292292
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,13 +51,13 @@ 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(read_zero_byte_vec::READ_ZERO_BYTE_VEC),
5960
LintId::of(regex::INVALID_REGEX),
60-
LintId::of(self_assignment::SELF_ASSIGNMENT),
6161
LintId::of(serde_api::SERDE_API_MISUSE),
6262
LintId::of(size_of_in_element_count::SIZE_OF_IN_ELEMENT_COUNT),
6363
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
@@ -437,6 +437,7 @@ store.register_lints(&[
437437
operators::NEEDLESS_BITWISE_BOOL,
438438
operators::OP_REF,
439439
operators::PTR_EQ,
440+
operators::SELF_ASSIGNMENT,
440441
operators::VERBOSE_BIT_MASK,
441442
option_env_unwrap::OPTION_ENV_UNWRAP,
442443
option_if_let_else::OPTION_IF_LET_ELSE,
@@ -483,7 +484,6 @@ store.register_lints(&[
483484
returns::LET_AND_RETURN,
484485
returns::NEEDLESS_RETURN,
485486
same_name_method::SAME_NAME_METHOD,
486-
self_assignment::SELF_ASSIGNMENT,
487487
self_named_constructors::SELF_NAMED_CONSTRUCTORS,
488488
semicolon_if_nothing_returned::SEMICOLON_IF_NOTHING_RETURNED,
489489
serde_api::SERDE_API_MISUSE,

clippy_lints/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,6 @@ mod repeat_once;
355355
mod return_self_not_must_use;
356356
mod returns;
357357
mod same_name_method;
358-
mod self_assignment;
359358
mod self_named_constructors;
360359
mod semicolon_if_nothing_returned;
361360
mod serde_api;
@@ -829,7 +828,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
829828
store.register_late_pass(|| Box::new(stable_sort_primitive::StableSortPrimitive));
830829
store.register_late_pass(|| Box::new(repeat_once::RepeatOnce));
831830
store.register_late_pass(|| Box::new(unwrap_in_result::UnwrapInResult));
832-
store.register_late_pass(|| Box::new(self_assignment::SelfAssignment));
833831
store.register_late_pass(|| Box::new(manual_ok_or::ManualOkOr));
834832
store.register_late_pass(|| Box::new(semicolon_if_nothing_returned::SemicolonIfNothingReturned));
835833
store.register_late_pass(|| Box::new(async_yields_async::AsyncYieldsAsync));

clippy_lints/src/operators/mod.rs

Lines changed: 42 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,45 @@ 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+
/// x: i32,
721+
/// }
722+
///
723+
/// fn copy_position(a: &mut Event, b: &Event) {
724+
/// a.x = a.x;
725+
/// }
726+
/// ```
727+
///
728+
/// Should be:
729+
/// ```rust
730+
/// struct Event {
731+
/// x: i32,
732+
/// }
733+
///
734+
/// fn copy_position(a: &mut Event, b: &Event) {
735+
/// a.x = b.x;
736+
/// }
737+
/// ```
738+
#[clippy::version = "1.48.0"]
739+
pub SELF_ASSIGNMENT,
740+
correctness,
741+
"explicit self-assignment"
742+
}
743+
704744
pub struct Operators {
705745
arithmetic_context: numeric_arithmetic::Context,
706746
verbose_bit_mask_threshold: u64,
@@ -730,6 +770,7 @@ impl_lint_pass!(Operators => [
730770
MODULO_ARITHMETIC,
731771
NEEDLESS_BITWISE_BOOL,
732772
PTR_EQ,
773+
SELF_ASSIGNMENT,
733774
]);
734775
impl Operators {
735776
pub fn new(verbose_bit_mask_threshold: u64) -> Self {
@@ -775,6 +816,7 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
775816
},
776817
ExprKind::Assign(lhs, rhs, _) => {
777818
assign_op_pattern::check(cx, e, lhs, rhs);
819+
self_assignment::check(cx, e, lhs, rhs);
778820
},
779821
ExprKind::Unary(op, arg) => {
780822
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 & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)