@@ -80,6 +80,7 @@ mod needless_character_iteration;
8080mod needless_collect;
8181mod needless_option_as_deref;
8282mod needless_option_take;
83+ mod new_ret_no_self;
8384mod no_effect_replace;
8485mod obfuscated_if_else;
8586mod ok_expect;
@@ -148,11 +149,9 @@ mod zst_offset;
148149
149150use clippy_config:: Conf ;
150151use clippy_utils:: consts:: { ConstEvalCtxt , Constant } ;
151- use clippy_utils:: diagnostics:: span_lint;
152152use clippy_utils:: macros:: FormatArgsStorage ;
153153use clippy_utils:: msrvs:: { self , Msrv } ;
154- use clippy_utils:: ty:: contains_ty_adt_constructor_opaque;
155- use clippy_utils:: { contains_return, is_trait_method, iter_input_pats, peel_blocks, return_ty, sym} ;
154+ use clippy_utils:: { contains_return, is_trait_method, iter_input_pats, peel_blocks, sym} ;
156155pub use path_ends_with_ext:: DEFAULT_ALLOWED_DOTFILES ;
157156use rustc_data_structures:: fx:: FxHashSet ;
158157use rustc_hir:: { self as hir, Expr , ExprKind , Node , Stmt , StmtKind , TraitItem , TraitItemKind } ;
@@ -4921,20 +4920,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
49214920 ) ;
49224921 }
49234922
4924- // if this impl block implements a trait, lint in trait definition instead
4925- if !implements_trait
4926- && impl_item. ident . name == sym:: new
4927- && let ret_ty = return_ty ( cx, impl_item. owner_id )
4928- && ret_ty != self_ty
4929- && !contains_ty_adt_constructor_opaque ( cx, ret_ty, self_ty)
4930- {
4931- span_lint (
4932- cx,
4933- NEW_RET_NO_SELF ,
4934- impl_item. span ,
4935- "methods called `new` usually return `Self`" ,
4936- ) ;
4937- }
4923+ new_ret_no_self:: check_impl_item ( cx, impl_item, self_ty, implements_trait) ;
49384924 }
49394925 }
49404926
@@ -4966,18 +4952,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
49664952 ) ;
49674953 }
49684954
4969- if item. ident . name == sym:: new
4970- && let ret_ty = return_ty ( cx, item. owner_id )
4971- && let self_ty = TraitRef :: identity ( cx. tcx , item. owner_id . to_def_id ( ) ) . self_ty ( )
4972- && !ret_ty. contains ( self_ty)
4973- {
4974- span_lint (
4975- cx,
4976- NEW_RET_NO_SELF ,
4977- item. span ,
4978- "methods called `new` usually return `Self`" ,
4979- ) ;
4980- }
4955+ new_ret_no_self:: check_trait_item ( cx, item) ;
49814956 }
49824957 }
49834958}
0 commit comments