@@ -58,13 +58,15 @@ declare_clippy_lint! {
5858pub struct UseSelf {
5959 msrv : Msrv ,
6060 stack : Vec < StackItem > ,
61+ recursive_self_in_type_definitions : bool ,
6162}
6263
6364impl UseSelf {
6465 pub fn new ( conf : & ' static Conf ) -> Self {
6566 Self {
6667 msrv : conf. msrv ,
6768 stack : Vec :: new ( ) ,
69+ recursive_self_in_type_definitions : conf. recursive_self_in_type_definitions ,
6870 }
6971 }
7072}
@@ -84,10 +86,10 @@ const SEGMENTS_MSG: &str = "segments should be composed of at least 1 element";
8486
8587impl < ' tcx > LateLintPass < ' tcx > for UseSelf {
8688 fn check_item ( & mut self , cx : & LateContext < ' tcx > , item : & Item < ' tcx > ) {
87- // We push the self types of `impl`s on a stack here. Only the top type on the stack is
88- // relevant for linting, since this is the self type of the `impl` we're currently in. To
89- // avoid linting on nested items, we push `StackItem::NoCheck` on the stack to signal, that
90- // we're in an `impl` or nested item, that we don't want to lint
89+ // We push the self types of items on a stack here. Only the top type on the stack is
90+ // relevant for linting, since this is the self type of the item we're currently in. To
91+ // avoid linting on nested items, we push `StackItem::NoCheck` on the stack to signal that
92+ // we're in an item or nested item that we don't want to lint
9193 let stack_item = if let ItemKind :: Impl ( Impl { self_ty, generics, .. } ) = item. kind
9294 && let TyKind :: Path ( QPath :: Resolved ( _, item_path) ) = self_ty. kind
9395 && let parameters = & item_path. segments . last ( ) . expect ( SEGMENTS_MSG ) . args
@@ -112,6 +114,15 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
112114 impl_id : item. owner_id . def_id ,
113115 types_to_skip,
114116 }
117+ } else if let ItemKind :: Struct ( ..) | ItemKind :: Enum ( ..) = item. kind
118+ && self . recursive_self_in_type_definitions
119+ && !item. span . from_expansion ( )
120+ && !is_from_proc_macro ( cx, item)
121+ {
122+ StackItem :: Check {
123+ impl_id : item. owner_id . def_id ,
124+ types_to_skip : FxHashSet :: default ( ) ,
125+ }
115126 } else {
116127 StackItem :: NoCheck
117128 } ;
0 commit comments