Skip to content

Conversation

profetia
Copy link
Contributor

@profetia profetia commented May 24, 2025

Closes #14857

changelog: [unit_arg] fix wrong suggestion for Default::default

@rustbot
Copy link
Collaborator

rustbot commented May 24, 2025

r? @samueltardieu

rustbot has assigned @samueltardieu.
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 May 24, 2025
@profetia profetia changed the title fix: unit_arg suggests wrongly for Default::default Fix unit_arg suggests wrongly for Default::default May 24, 2025
Copy link
Member

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

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

I don't think it behaves as expected in the following cases:

    macro_rules! mac {
        (def) => { Default::default() };
        (nondef $e:expr) => { $e };
        (func $f:expr) => { $f() };
    }
    fn_take_unit(mac!(def));
    fn_take_unit(mac!(nondef Default::default()));
    fn_take_unit(mac!(func Default::default));

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels May 24, 2025
@profetia
Copy link
Contributor Author

Turns out this lint cannot correctly handle macros at all:

    fn some_other_fn(_: &i32) {}

    macro_rules! another_mac {
        () => {
            some_other_fn(&Default::default());
        };
        ($e:expr) => {
            some_other_fn(&$e);
        };
    }

    fn_take_unit(another_mac!());
    fn_take_unit(another_mac!(1));

@profetia
Copy link
Contributor Author

Thank you! Now both the Default::default and the macro problems are fixed.

@profetia
Copy link
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels May 24, 2025
Copy link
Member

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

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

I'm not sure this is the right fix: we should not specialize it for Default::default(). Any expression whose type is not fully determined without being coerced to () is susceptible to trigger this.

For example, the following

fn def<T: Default>() -> T {
    Default::default()
}

fn take_unit(_: ()) {}

fn main() {
    take_unit(def());
}

exhibits the same problem as the original issue, without using Default::default().

clippy_utils::ty::expr_type_is_certain() might be useful here. If it returns false, let _: () = …; might be a good suggestion. And if the expression is default(), it can be totally removed from the suggestion, unless it comes from a macro (I don't think we should look inside macros).

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels May 31, 2025
@profetia
Copy link
Contributor Author

Updated. Thank you!

@samueltardieu
Copy link
Member

If this is ready for review, don't forget to replace S-waiting-on-author by S-waiting-on-review, which can be done by using the ready command to rustbot, or by requesting a new review from the PR assignee.

@profetia
Copy link
Contributor Author

profetia commented Jun 9, 2025

Big thanks for reminding me!

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jun 9, 2025
Copy link
Member

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

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

I don't like the lintcheck output, but I think it was bogus before and it still bogus afterwards.

@samueltardieu samueltardieu added this pull request to the merge queue Jun 12, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jun 12, 2025
@samueltardieu samueltardieu added this pull request to the merge queue Jun 12, 2025
Merged via the queue into rust-lang:master with commit 180adb3 Jun 12, 2025
11 checks passed
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.

Failed to fix 'passing a unit value to a function'
3 participants