-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New lint: struct_fields_rest_default
#14412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 4 commits
b11506a
85c6663
554f903
0dab1e3
7f63851
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use clippy_utils::diagnostics::span_lint_and_help; | ||
use clippy_utils::is_trait_item; | ||
use clippy_utils::source::snippet; | ||
use rustc_hir::{ExprKind, StructTailExpr}; | ||
use rustc_lint::{LateLintPass, LintContext}; | ||
use rustc_session::declare_lint_pass; | ||
use rustc_span::sym; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Check struct initialization uses `..*::default()` pattern to skip rest of struct field initialization. | ||
/// | ||
/// ### Why restrict this? | ||
/// Using `..*::default()` can hide field initialization when new fields are added to structs, | ||
/// potentially leading to bugs where developers forget to explicitly set values for new fields. | ||
/// | ||
/// ### Example | ||
/// ```no_run | ||
/// #[derive(Default)] | ||
/// struct Foo { | ||
/// a: i32, | ||
/// b: i32, | ||
/// // when add new filed `c` | ||
/// c: i32, | ||
/// } | ||
/// | ||
/// let _ = Foo { | ||
/// a: Default::default(), | ||
/// ..Default::default() | ||
/// // developer may forget to explicitly set field `c` and cause bug | ||
/// }; | ||
/// ``` | ||
/// Use instead: | ||
/// ```no_run | ||
/// #[derive(Default)] | ||
/// struct Foo { | ||
/// a: i32, | ||
/// b: i32, | ||
/// // when add new filed `c` | ||
/// c: i32, | ||
/// } | ||
/// | ||
/// // make the compiler check for new fields to avoid bug. | ||
/// let _ = Foo { | ||
/// a: Default::default(), | ||
/// b: Default::default(), | ||
/// c: Default::default(), | ||
/// }; | ||
/// | ||
/// impl Foo { | ||
/// fn get_foo() -> Self { | ||
/// Foo{ a: 0, b: 0, c: 0} | ||
/// } | ||
/// } | ||
/// | ||
/// // or avoid using `..*::default()` | ||
/// let _ = Foo { | ||
/// a: Default::default(), | ||
/// ..Foo::get_foo() | ||
/// }; | ||
/// ``` | ||
#[clippy::version = "1.87.0"] | ||
pub STRUCT_FIELDS_REST_DEFAULT, | ||
restriction, | ||
"should not use `..*::default()` pattern to omit rest of struct field initialization" | ||
} | ||
|
||
declare_lint_pass!(StructFieldsDefault => [STRUCT_FIELDS_REST_DEFAULT]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for StructFieldsDefault { | ||
fn check_expr(&mut self, cx: &rustc_lint::LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx>) { | ||
if !expr.span.in_external_macro(cx.sess().source_map()) | ||
&& let ExprKind::Struct(_, _, StructTailExpr::Base(base)) = &expr.kind | ||
&& let ExprKind::Call(func, _) = base.kind | ||
&& is_trait_item(cx, func, sym::Default) | ||
{ | ||
span_lint_and_help( | ||
cx, | ||
STRUCT_FIELDS_REST_DEFAULT, | ||
base.span, | ||
format!( | ||
"should not use `..{}` to omit rest of struct field initialization", | ||
snippet(cx, base.span, "") | ||
), | ||
Some(expr.span), | ||
"explicitly specify all fields or use other base value instead of `..*::default()`", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this lint should only trigger, if the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. |
||
); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
//@aux-build:proc_macros.rs | ||
#![warn(clippy::struct_fields_rest_default)] | ||
extern crate proc_macros; | ||
|
||
#[derive(Default)] | ||
struct Foo { | ||
a: i32, | ||
b: i32, | ||
c: i32, | ||
} | ||
|
||
impl Foo { | ||
fn get_foo() -> Self { | ||
Foo { a: 0, b: 0, c: 0 } | ||
} | ||
} | ||
|
||
fn main() { | ||
#[rustfmt::skip] | ||
let _ = Foo { | ||
a: 10, | ||
..Default::default() | ||
//~^ struct_fields_rest_default | ||
}; | ||
|
||
#[rustfmt::skip] | ||
let _ = Foo { | ||
a: 10, | ||
..Foo::default() | ||
//~^ struct_fields_rest_default | ||
}; | ||
|
||
// should not lint | ||
#[rustfmt::skip] | ||
let _ = Foo { | ||
a: 10, | ||
..Foo::get_foo() | ||
}; | ||
|
||
// should not lint in external macro | ||
proc_macros::external! { | ||
#[derive(Default)] | ||
struct ExternalDefault { | ||
a: i32, | ||
b: i32, | ||
} | ||
|
||
let _ = ExternalDefault { | ||
a: 10, | ||
..Default::default() | ||
}; | ||
} | ||
|
||
// should not lint | ||
let _ = Foo { | ||
a: Default::default(), | ||
b: Default::default(), | ||
c: Default::default(), | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
error: should not use `..Default::default()` to omit rest of struct field initialization | ||
--> tests/ui/struct_fields_rest_default.rs:22:11 | ||
| | ||
LL | ..Default::default() | ||
| ^^^^^^^^^^^^^^^^^^ | ||
| | ||
help: explicitly specify all fields or use other base value instead of `..*::default()` | ||
--> tests/ui/struct_fields_rest_default.rs:20:13 | ||
| | ||
LL | let _ = Foo { | ||
| _____________^ | ||
LL | | a: 10, | ||
LL | | ..Default::default() | ||
LL | | | ||
LL | | }; | ||
| |_____^ | ||
= note: `-D clippy::struct-fields-rest-default` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::struct_fields_rest_default)]` | ||
|
||
error: should not use `..Foo::default()` to omit rest of struct field initialization | ||
--> tests/ui/struct_fields_rest_default.rs:29:11 | ||
| | ||
LL | ..Foo::default() | ||
| ^^^^^^^^^^^^^^ | ||
| | ||
help: explicitly specify all fields or use other base value instead of `..*::default()` | ||
--> tests/ui/struct_fields_rest_default.rs:27:13 | ||
| | ||
LL | let _ = Foo { | ||
| _____________^ | ||
LL | | a: 10, | ||
LL | | ..Foo::default() | ||
LL | | | ||
LL | | }; | ||
| |_____^ | ||
|
||
error: aborting due to 2 previous errors | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message should state what the code does, not what should (not) be done. This belongs into the help/note/sugg message, as you done below.