@@ -3,7 +3,8 @@ use rustc_front::hir;
33use rustc_front:: intravisit:: FnKind ;
44use syntax:: ast;
55use syntax:: codemap:: Span ;
6- use utils:: { get_trait_def_id, implements_trait, in_external_macro, return_ty, span_lint, DEFAULT_TRAIT_PATH } ;
6+ use utils:: { get_trait_def_id, implements_trait, in_external_macro, return_ty, same_tys, span_lint,
7+ DEFAULT_TRAIT_PATH } ;
78
89/// **What it does:** This lints about type with a `fn new() -> Self` method and no `Default`
910/// implementation.
@@ -49,16 +50,15 @@ impl LateLintPass for NewWithoutDefault {
4950 if decl. inputs . is_empty ( ) && name. as_str ( ) == "new" {
5051 let self_ty = cx. tcx . lookup_item_type ( cx. tcx . map . local_def_id ( cx. tcx . map . get_parent ( id) ) ) . ty ;
5152
52- let ret_ty = return_ty ( cx. tcx . node_id_to_type ( id) ) ;
53-
54- if Some ( self_ty) == ret_ty {
55- if let Some ( default_trait_id) = get_trait_def_id ( cx, & DEFAULT_TRAIT_PATH ) {
56- if !implements_trait ( cx, self_ty, default_trait_id, Vec :: new ( ) ) {
57- span_lint ( cx, NEW_WITHOUT_DEFAULT , span,
58- & format ! ( "you should consider adding a `Default` implementation for `{}`" , self_ty) ) ;
59- }
60- }
61- }
53+ if_let_chain ! { [
54+ let Some ( ret_ty) = return_ty( cx. tcx. node_id_to_type( id) ) ,
55+ same_tys( cx, self_ty, ret_ty) ,
56+ let Some ( default_trait_id) = get_trait_def_id( cx, & DEFAULT_TRAIT_PATH ) ,
57+ !implements_trait( cx, self_ty, default_trait_id, Vec :: new( ) )
58+ ] , {
59+ span_lint( cx, NEW_WITHOUT_DEFAULT , span,
60+ & format!( "you should consider adding a `Default` implementation for `{}`" , self_ty) ) ;
61+ } }
6262 }
6363 }
6464 }
0 commit comments