@@ -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 ! {
@@ -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+
704744pub 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] ) ;
734775impl 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 {
0 commit comments