Skip to content

new_without_default: small clean-up#17172

Open
ada4a wants to merge 5 commits into
rust-lang:masterfrom
ada4a:push-mnmmlwloxtxy
Open

new_without_default: small clean-up#17172
ada4a wants to merge 5 commits into
rust-lang:masterfrom
ada4a:push-mnmmlwloxtxy

Conversation

@ada4a

@ada4a ada4a commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

changelog: none

Should be merged after #16531 to avoid the rebase churn, so for now:

@rustbot blocked

nyurik and others added 5 commits June 1, 2026 12:02
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
@rustbot rustbot added needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jun 6, 2026
@rustbot

rustbot commented Jun 6, 2026

Copy link
Copy Markdown
Collaborator

r? @llogiq

rustbot has assigned @llogiq.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: 8 candidates
  • 8 candidates expanded to 8 candidates
  • Random selection from Jarcho, dswij, llogiq, samueltardieu

@rustbot rustbot added the S-blocked Status: marked as blocked ❌ on something else such as an RFC or other implementation work label Jun 6, 2026
@github-actions

github-actions Bot commented Jun 6, 2026

Copy link
Copy Markdown

Lintcheck changes for deb6159

Lint Added Removed Changed
clippy::default_mismatches_new 12 0 0

This comment will be updated if you push new changes

@ada4a ada4a force-pushed the push-mnmmlwloxtxy branch from 5b9d265 to deb6159 Compare June 8, 2026 10:59
@rustbot

rustbot commented Jun 13, 2026

Copy link
Copy Markdown
Collaborator

☔ The latest upstream changes (possibly #17096) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-fcp PRs that add, remove, or rename lints and need an FCP S-blocked Status: marked as blocked ❌ on something else such as an RFC or other implementation work S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants