1
1
use clippy_utils:: diagnostics:: span_lint_and_help;
2
- use clippy_utils:: path_def_id ;
2
+ use clippy_utils:: is_trait_item ;
3
3
use clippy_utils:: source:: snippet;
4
4
use rustc_hir:: { ExprKind , StructTailExpr } ;
5
5
use rustc_lint:: { LateLintPass , LintContext } ;
@@ -46,11 +46,23 @@ declare_clippy_lint! {
46
46
/// b: Default::default(),
47
47
/// c: Default::default(),
48
48
/// };
49
+ ///
50
+ /// impl Foo {
51
+ /// fn get_foo() -> Self {
52
+ /// Foo{ a: 0, b: 0, c: 0}
53
+ /// }
54
+ /// }
55
+ ///
56
+ /// // or avoid using `..*::default()`
57
+ /// let _ = Foo {
58
+ /// a: Default::default(),
59
+ /// ..Foo::get_foo()
60
+ /// };
49
61
/// ```
50
62
#[ clippy:: version = "1.87.0" ]
51
63
pub STRUCT_FIELDS_REST_DEFAULT ,
52
64
restriction,
53
- "should not use `..Default ::default()` to omit rest of struct field initialization"
65
+ "should not use `..* ::default()` pattern to omit rest of struct field initialization"
54
66
}
55
67
56
68
declare_lint_pass ! ( StructFieldsDefault => [ STRUCT_FIELDS_REST_DEFAULT ] ) ;
@@ -60,19 +72,18 @@ impl<'tcx> LateLintPass<'tcx> for StructFieldsDefault {
60
72
if !expr. span . in_external_macro ( cx. sess ( ) . source_map ( ) )
61
73
&& let ExprKind :: Struct ( _, _, StructTailExpr :: Base ( base) ) = & expr. kind
62
74
&& let ExprKind :: Call ( func, _) = base. kind
63
- && let Some ( did) = path_def_id ( cx, func)
64
- && cx. tcx . is_diagnostic_item ( sym:: default_fn, did)
75
+ && is_trait_item ( cx, func, sym:: Default )
65
76
{
66
77
span_lint_and_help (
67
78
cx,
68
79
STRUCT_FIELDS_REST_DEFAULT ,
69
80
base. span ,
70
81
format ! (
71
82
"should not use `..{}` to omit rest of struct field initialization" ,
72
- snippet( cx, base. span, ".. " )
83
+ snippet( cx, base. span, "" )
73
84
) ,
74
85
Some ( expr. span ) ,
75
- "each field's initial value should be explicitly specified " ,
86
+ "explicitly specify all fields or use other base value instead of `..*::default()` " ,
76
87
) ;
77
88
}
78
89
}
0 commit comments