@@ -22,6 +22,7 @@ mod needless_bitwise_bool;
22
22
mod numeric_arithmetic;
23
23
mod op_ref;
24
24
mod ptr_eq;
25
+ mod self_assignment;
25
26
mod verbose_bit_mask;
26
27
27
28
declare_clippy_lint ! {
@@ -701,6 +702,45 @@ declare_clippy_lint! {
701
702
"use `std::ptr::eq` when comparing raw pointers"
702
703
}
703
704
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
+
704
744
pub struct Operators {
705
745
arithmetic_context : numeric_arithmetic:: Context ,
706
746
verbose_bit_mask_threshold : u64 ,
@@ -730,6 +770,7 @@ impl_lint_pass!(Operators => [
730
770
MODULO_ARITHMETIC ,
731
771
NEEDLESS_BITWISE_BOOL ,
732
772
PTR_EQ ,
773
+ SELF_ASSIGNMENT ,
733
774
] ) ;
734
775
impl Operators {
735
776
pub fn new ( verbose_bit_mask_threshold : u64 ) -> Self {
@@ -775,6 +816,7 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
775
816
} ,
776
817
ExprKind :: Assign ( lhs, rhs, _) => {
777
818
assign_op_pattern:: check ( cx, e, lhs, rhs) ;
819
+ self_assignment:: check ( cx, e, lhs, rhs) ;
778
820
} ,
779
821
ExprKind :: Unary ( op, arg) => {
780
822
if op == UnOp :: Neg {
0 commit comments