Skip to content

Commit 0dab1e3

Browse files
committed
Fix struct_fields_rest_default helping message
1 parent 554f903 commit 0dab1e3

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

clippy_lints/src/struct_fields_rest_default.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2-
use clippy_utils::path_def_id;
2+
use clippy_utils::is_trait_item;
33
use clippy_utils::source::snippet;
44
use rustc_hir::{ExprKind, StructTailExpr};
55
use rustc_lint::{LateLintPass, LintContext};
@@ -46,11 +46,23 @@ declare_clippy_lint! {
4646
/// b: Default::default(),
4747
/// c: Default::default(),
4848
/// };
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+
/// };
4961
/// ```
5062
#[clippy::version = "1.87.0"]
5163
pub STRUCT_FIELDS_REST_DEFAULT,
5264
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"
5466
}
5567

5668
declare_lint_pass!(StructFieldsDefault => [STRUCT_FIELDS_REST_DEFAULT]);
@@ -60,19 +72,18 @@ impl<'tcx> LateLintPass<'tcx> for StructFieldsDefault {
6072
if !expr.span.in_external_macro(cx.sess().source_map())
6173
&& let ExprKind::Struct(_, _, StructTailExpr::Base(base)) = &expr.kind
6274
&& 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)
6576
{
6677
span_lint_and_help(
6778
cx,
6879
STRUCT_FIELDS_REST_DEFAULT,
6980
base.span,
7081
format!(
7182
"should not use `..{}` to omit rest of struct field initialization",
72-
snippet(cx, base.span, "..")
83+
snippet(cx, base.span, "")
7384
),
7485
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()`",
7687
);
7788
}
7889
}

tests/ui/struct_fields_rest_default.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: should not use `..Default::default()` to omit rest of struct field initia
44
LL | ..Default::default()
55
| ^^^^^^^^^^^^^^^^^^
66
|
7-
help: each field's initial value should be explicitly specified
7+
help: explicitly specify all fields or use other base value instead of `..*::default()`
88
--> tests/ui/struct_fields_rest_default.rs:20:13
99
|
1010
LL | let _ = Foo {
@@ -23,7 +23,7 @@ error: should not use `..Foo::default()` to omit rest of struct field initializa
2323
LL | ..Foo::default()
2424
| ^^^^^^^^^^^^^^
2525
|
26-
help: each field's initial value should be explicitly specified
26+
help: explicitly specify all fields or use other base value instead of `..*::default()`
2727
--> tests/ui/struct_fields_rest_default.rs:27:13
2828
|
2929
LL | let _ = Foo {

0 commit comments

Comments
 (0)