Skip to content

Fix unit_arg suggests wrongly for Default::default #14881

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

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
Contributor

@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
Contributor

@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)
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