Skip to content

Correctly handle bracketed type in default_constructed_unit_struct #14367

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

Merged
merged 3 commits into from
Apr 10, 2025

Conversation

samueltardieu
Copy link
Contributor

There were two bugs here. Let's assume T is a singleton type implementing Default and that f() takes a T:

  • f(<T>::default()) cannot be replaced by f(<T) as it was (incorrect spans – this is tricky because the type relative path uses a base span covering only T, not <T>) (third commit)
  • The argument of f(<_>::default()) is inferred correctly, but cannot be replaced by <_> or _, as this cannot be used to infer an instance of a singleton type (first commit).

The second commit offers better error messages by pointing at the whole expression.

Fix #12654

changelog: [default_constructed_unit_struct]: do not suggest incorrect fix when using a type surrounded by brackets

An expression such as `<_>::default()` should not be replaced by an
ambiguous type name such as `_` even if the inferred type in the
original expression is a singleton.
This is more consistent with what other lints are doing: the use of
`default` to create a unit struct is the whole expression.
@rustbot
Copy link
Collaborator

rustbot commented Mar 6, 2025

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

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Mar 6, 2025
@samueltardieu
Copy link
Contributor Author

r? clippy

@rustbot rustbot assigned y21 and unassigned llogiq Mar 31, 2025
Copy link
Member

@y21 y21 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, r=me with the dbg removed

{
span_lint_and_sugg(
let mut removals = vec![(expr.span.with_lo(qpath.qself_span().hi()), String::new())];
if expr.span.with_source_text(cx, |s| dbg!(s).starts_with('<')) == Some(true) {
Copy link
Member

@y21 y21 Apr 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you forgot to remove a dbg here (maybe we should run clippy_lints dogfood with -Dclippy::dbg_macro)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this! I've submitted #14579 to avoid this in the future.

When `<T>::default()` is replaced by `T` when `T` is a singleton,
the brackets around the type name can be removed.
@samueltardieu samueltardieu enabled auto-merge April 10, 2025 05:48
@samueltardieu samueltardieu added this pull request to the merge queue Apr 10, 2025
Merged via the queue into rust-lang:master with commit 529bb5f Apr 10, 2025
11 checks passed
@samueltardieu samueltardieu deleted the push-nzlqupnnlkym branch April 11, 2025 08:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

default_constructed_unit_structs sugg causes error
4 participants