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