new_without_default: small clean-up#17172
Open
ada4a wants to merge 5 commits into
Open
Conversation
Move is_unit_struct into clippy_utils to use it in the new lint default_mismatches_new
Mostly to simplify next commit's diff
If a type has an auto-derived `Default` trait and a `fn new() -> Self`,
this lint checks if the `new()` method performs custom logic rather
than simply calling the `default()` method.
Users expect the `new()` method to be equivalent to `default()`,
so if the `Default` trait is auto-derived, the `new()` method should
not perform custom logic. Otherwise, there is a risk of different
behavior between the two instantiation methods.
```rust
struct MyStruct(i32);
impl MyStruct {
fn new() -> Self {
Self(42)
}
}
```
Users are unlikely to notice that `MyStruct::new()` and `MyStruct::default()` would produce
different results. The `new()` method should use auto-derived `default()` instead to be consistent:
```rust
struct MyStruct(i32);
impl MyStruct {
fn new() -> Self {
Self::default()
}
}
```
Alternatively, if the `new()` method requires a non-default initialization, implement a custom `Default`.
This also allows you to mark the `new()` implementation as `const`:
```rust
struct MyStruct(i32);
impl MyStruct {
const fn new() -> Self {
Self(42)
}
}
impl Default for MyStruct {
fn default() -> Self {
Self::new()
}
}
```
made a separate commit just to simplify the diff
Collaborator
|
r? @llogiq rustbot has assigned @llogiq. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
Lintcheck changes for deb6159
This comment will be updated if you push new changes |
Collaborator
|
☔ The latest upstream changes (possibly #17096) made this pull request unmergeable. Please resolve the merge conflicts. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
changelog: none
Should be merged after #16531 to avoid the rebase churn, so for now:
@rustbot blocked