Skip to content

Fix FP of identity_op when encountering Default::default() #15028

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
Jun 15, 2025

Conversation

relaxcn
Copy link
Contributor

@relaxcn relaxcn commented Jun 10, 2025

Fixes: #14932

changelog: Fix false positive of [identity_op] when encountering Default::default().

@rustbot
Copy link
Collaborator

rustbot commented Jun 10, 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 Jun 10, 2025
@relaxcn
Copy link
Contributor Author

relaxcn commented Jun 11, 2025

The current fix has false negatives at this code:

fn test() -> usize {
    0usize + &Default::default()
    //~^ identity_op
}

fn main() {
    let _n: usize = 0usize + &Default::default();
    //~^ identity_op
}

The lint also need to check whether the compiler can infer the type of Default::default().

I will make it ready when I finish.

@rustbot author

@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 Jun 11, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jun 11, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@relaxcn relaxcn force-pushed the fix-fp-identity_ip branch from 37aacd0 to e26f9be Compare June 11, 2025 16:34
@relaxcn relaxcn force-pushed the fix-fp-identity_ip branch from e26f9be to 7cb7e28 Compare June 11, 2025 16:37
@relaxcn
Copy link
Contributor Author

relaxcn commented Jun 11, 2025

@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 11, 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 have the feeling that this only solves one edge case but doesn't address the real problem. For example, the following code will fail as well:

trait Def {
    fn def() -> Self;
}

impl Def for usize {
    fn def() -> Self {
        0
    }
}

_ = 0usize + &Def::def();

The problem is not really the Default::default() call, it is the fact that the associated function call returns a type which is neither given explicitly nor derived from the function arguments. Maybe checking this would solve the general case.

@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 Jun 12, 2025
@relaxcn
Copy link
Contributor Author

relaxcn commented Jun 13, 2025

Thank you!

I have modified the code. I have tried to find more cases about non-assoc function, but I can't.

Now, the lint will exclude checking AssocFn which return Self and doesn't specify fully-qualified.

@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 13, 2025
@relaxcn
Copy link
Contributor Author

relaxcn commented Jun 14, 2025

It's strange. I passed the cargo test localy but it still failed in github action.

Now fixed.

@samueltardieu
Copy link
Contributor

That's a reasonable approach, thanks!

@samueltardieu samueltardieu added this pull request to the merge queue Jun 15, 2025
Merged via the queue into rust-lang:master with commit 4d67a1c Jun 15, 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.

FP identity_op does not catch type hint is needed
3 participants