@@ -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 } ;
@@ -4918,20 +4917,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
49184917 ) ;
49194918 }
49204919
4921- // if this impl block implements a trait, lint in trait definition instead
4922- if !implements_trait
4923- && impl_item. ident . name == sym:: new
4924- && let ret_ty = return_ty ( cx, impl_item. owner_id )
4925- && ret_ty != self_ty
4926- && !contains_ty_adt_constructor_opaque ( cx, ret_ty, self_ty)
4927- {
4928- span_lint (
4929- cx,
4930- NEW_RET_NO_SELF ,
4931- impl_item. span ,
4932- "methods called `new` usually return `Self`" ,
4933- ) ;
4934- }
4920+ new_ret_no_self:: check_impl_item ( cx, impl_item, self_ty, implements_trait) ;
49354921 }
49364922 }
49374923
@@ -4963,18 +4949,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
49634949 ) ;
49644950 }
49654951
4966- if item. ident . name == sym:: new
4967- && let ret_ty = return_ty ( cx, item. owner_id )
4968- && let self_ty = TraitRef :: identity ( cx. tcx , item. owner_id . to_def_id ( ) ) . self_ty ( )
4969- && !ret_ty. contains ( self_ty)
4970- {
4971- span_lint (
4972- cx,
4973- NEW_RET_NO_SELF ,
4974- item. span ,
4975- "methods called `new` usually return `Self`" ,
4976- ) ;
4977- }
4952+ new_ret_no_self:: check_trait_item ( cx, item) ;
49784953 }
49794954 }
49804955}
0 commit comments