Skip to content

lint when calling the blanket Into impl from a From impl #12459

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 1 commit into from
Mar 13, 2024

Conversation

y21
Copy link
Member

@y21 y21 commented Mar 10, 2024

Closes #11150

warning: function cannot return without recursing
  --> x.rs:9:9
   |
9  | /         fn from(value: f32) -> Self {
10 | |             value.into()
11 | |         }
   | |_________^
   |
note: recursive call site
  --> x.rs:10:13
   |
10 |             value.into()
   |             ^^^^^^^^^^^^

I'm also thinking that we can probably generalize this lint to #11032 at some point (instead of hardcoding a bunch of impls), like how rustc's unconditional_recursion works, at least up to one indirect call, but this still seems useful for now :)

I've also noticed that we use fn_def_id in a bunch of lints and then try to get the node args of the call in a separate step, so I made a helper function that does both in one. I intend to refactor a bunch of uses of fn_def_id to use this later

I can add more test cases, but this is already using much of the same logic that exists for the other impls that this lint looks for (e.g. making sure that there are no conditional returns).

changelog: [unconditional_recursion]: emit a warning inside of From::from when unconditionally calling the blanket .into() impl

@rustbot
Copy link
Collaborator

rustbot commented Mar 10, 2024

r? @Jarcho

rustbot has assigned @Jarcho.
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 10, 2024
Copy link
Contributor

@Jarcho Jarcho left a comment

Choose a reason for hiding this comment

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

This should have some negative tests. Otherwise Looks good.

@y21
Copy link
Member Author

y21 commented Mar 12, 2024

added a few more tests I could think of in the last commit, if you have some more concrete ones I can add them. Also removed an unnecessary get_trait_def_id which could be using diagnostic items

Copy link
Contributor

@Jarcho Jarcho left a comment

Choose a reason for hiding this comment

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

@bors delegate

All looks good. r=me after squashing.

@y21 y21 force-pushed the unconditional_recursion_from_into branch from 1827791 to 65defdb Compare March 13, 2024 16:25
@y21
Copy link
Member Author

y21 commented Mar 13, 2024

squashed
@bors r=Jarcho

@bors
Copy link
Contributor

bors commented Mar 13, 2024

📌 Commit 65defdb has been approved by Jarcho

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Mar 13, 2024

⌛ Testing commit 65defdb with merge 73be486...

@bors
Copy link
Contributor

bors commented Mar 13, 2024

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: Jarcho
Pushing 73be486 to master...

@bors bors merged commit 73be486 into rust-lang:master Mar 13, 2024
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.

Using into() in the From trait without having implemented Into should generate a compilation error.
5 participants