@@ -22,6 +22,7 @@ mod modulo_one;
2222mod needless_bitwise_bool;
2323mod op_ref;
2424mod ptr_eq;
25+ mod self_assignment;
2526mod verbose_bit_mask;
2627
2728declare_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+
689721pub 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] ) ;
719752impl 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 {
0 commit comments