Skip to content
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

Added the [unnecessary_box_returns] lint #9102

Merged
merged 4 commits into from
Mar 30, 2023
Merged

Conversation

botahamec
Copy link
Contributor

@botahamec botahamec commented Jul 2, 2022

fixes #5

I'm not confident in the name of this lint. Let me know if you can think of something better


changelog: New lint: [`unnecessary_box_returns`]
#9102

@rust-highfive
Copy link

r? @dswij

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jul 2, 2022
@bors
Copy link
Collaborator

bors commented Jul 3, 2022

☔ The latest upstream changes (presumably #9105) made this pull request unmergeable. Please resolve the merge conflicts.

@dswij
Copy link
Member

dswij commented Jul 15, 2022

Thanks for the PR! Sorry I haven't been able to take a look at this. I will get to it asap this week when I have the time

@bors
Copy link
Collaborator

bors commented Jul 15, 2022

☔ The latest upstream changes (presumably #9103) made this pull request unmergeable. Please resolve the merge conflicts.

Copy link
Member

@dswij dswij left a comment

Choose a reason for hiding this comment

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

Thanks for the PR and the patience! The lint logic itself looks great. For the naming, I think unnecessary_box_returns is slightly more descriptive. What do you think?

I'm actually curious whether this will fire a lot in other rust codebases. I will try to run this lint for some of the most popular crates.

clippy_lints/src/unused_box.rs Outdated Show resolved Hide resolved
clippy_lints/src/unused_box.rs Outdated Show resolved Hide resolved
@botahamec
Copy link
Contributor Author

@dswij I like unnecessary_box_return. I think an 's' at the end is unnecessary.

@xFrednet
Copy link
Member

Hey, happy that you asked about the name and the suggestion. I would keep the 's' at the end of the name. Rustc's lint naming conventions suggest that lint names should be plural if possible. This also works better with lint attributes affecting multiple cases, like #![deny(unnecessary_box_returns)]

@botahamec
Copy link
Contributor Author

@xFrednet Ah, ok. I'll make that change then

@Jarcho
Copy link
Contributor

Jarcho commented Jul 20, 2022

The lint itself is fine, but it requires placement syntax before it's generally applicable. As far as I know that's completely stalled as of now. Box::new(foo()) can't optimize out the temporary from calling foo() unless it's inlined (and not necessarily then).

As a restriction lint the downside is fine. A size limit would also be fine. Just leaving it in the nursery until placement syntax is stabilized would also work.

@xFrednet xFrednet changed the title Added the [unused_box] lint Added the [unnecessary_box_returns] lint Jul 20, 2022
@bors
Copy link
Collaborator

bors commented Jul 25, 2022

☔ The latest upstream changes (presumably #9243) made this pull request unmergeable. Please resolve the merge conflicts.

@dswij
Copy link
Member

dswij commented Jul 26, 2022

The lint itself is fine, but it requires placement syntax before it's generally applicable. As far as I know that's completely stalled as of now. Box::new(foo()) can't optimize out the temporary from calling foo() unless it's inlined (and not necessarily then).

@botahamec I think we can add this to the test case, and I think it's good to be merged once the merge conflict is addressed.

@dswij
Copy link
Member

dswij commented Jul 26, 2022

So, I ran this lint on 250 crates:

Summary

Build failures

Total: 1

  • vsdb-0.45.2

Warnings

Total: 17

Crate Count
bincode-1.3.3 4
clap-3.2.15 2
crossbeam-channel-0.5.6 1
crossbeam-epoch-0.9.10 1
miniz_oxide-0.5.3 2
parking_lot_core-0.9.3 1
pest-2.1.3 4
regex-1.6.0 1
structopt-0.3.26 1

There are some improvements that needs to be made. e.g. these suggestion aren't the most accurate.

---> pest-2.1.3/src/parser_state.rs:1107:57
warning: function returns `Box<parser_state::ParserState<'i, R>>` when `parser_state::ParserState<'i, R>` implements `Sized`
    --> src/parser_state.rs:1107:57
     |
1107 |     pub(crate) fn checkpoint_ok(mut self: Box<Self>) -> Box<Self> {
     |                                                         ^^^^^^^^^ help: change the return type to: `parser_state::ParserState<'i, R>`
     |
     = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_box_returns
---> crossbeam-channel-0.5.6/src/flavors/zero.rs:57:27
warning: function returns `Box<flavors::zero::Packet<T>>` when `flavors::zero::Packet<T>` implements `Sized`
  --> src/flavors/zero.rs:57:27
   |
57 |     fn empty_on_heap() -> Box<Packet<T>> {
   |                           ^^^^^^^^^^^^^^ help: change the return type to: `flavors::zero::Packet<T>`
   |
   = note: requested on the command line with `-W clippy::unnecessary-box-returns`
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_box_returns
---> structopt-0.3.26/src/lib.rs:1218:53
warning: function returns `Box<T>` when `T` implements `Sized`
    --> src/lib.rs:1218:53
     |
1218 |     fn from_clap(matches: &clap::ArgMatches<'_>) -> Self {
     |                                                     ^^^^ help: change the return type to: `T`
     |
     = note: requested on the command line with `-W clippy::unnecessary-box-returns`
     = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_box_returns

We can address this in this PR or later as an improvement since this lint is going into nursery. What do you think? @botahamec

@botahamec
Copy link
Contributor Author

@dswij I think it at least makes sense to take a look at the third one. I'd honestly be ok with merging on the first two examples. Let me know if there's anything else I should look at though. I don't have my desktop with me at the moment, so it might take a while.

@dswij
Copy link
Member

dswij commented Jul 26, 2022

No rush. Feel free to ping me when it's ready

@botahamec botahamec force-pushed the unused-box branch 2 times, most recently from 4a2a27d to 59a3ccd Compare July 30, 2022 19:55
@bors
Copy link
Collaborator

bors commented Aug 2, 2022

☔ The latest upstream changes (presumably #9264) made this pull request unmergeable. Please resolve the merge conflicts.

@dswij dswij 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 Aug 14, 2022
@kraktus
Copy link
Contributor

kraktus commented Sep 14, 2022

...

---> pest-2.1.3/src/parser_state.rs:1107:57
warning: function returns `Box<parser_state::ParserState<'i, R>>` when `parser_state::ParserState<'i, R>` implements `Sized`
    --> src/parser_state.rs:1107:57
     |
1107 |     pub(crate) fn checkpoint_ok(mut self: Box<Self>) -> Box<Self> {
     |                                                         ^^^^^^^^^ help: change the return type to: `parser_state::ParserState<'i, R>`
     |
     = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_box_returns
---> crossbeam-channel-0.5.6/src/flavors/zero.rs:57:27
warning: function returns `Box<flavors::zero::Packet<T>>` when `flavors::zero::Packet<T>` implements `Sized`
 --> src/flavors/zero.rs:57:27
  |
57 |     fn empty_on_heap() -> Box<Packet<T>> {
  |                           ^^^^^^^^^^^^^^ help: change the return type to: `flavors::zero::Packet<T>`
  |
  = note: requested on the command line with `-W clippy::unnecessary-box-returns`
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_box_returns
---> structopt-0.3.26/src/lib.rs:1218:53
warning: function returns `Box<T>` when `T` implements `Sized`
   --> src/lib.rs:1218:53
    |
1218 |     fn from_clap(matches: &clap::ArgMatches<'_>) -> Self {
    |                                                     ^^^^ help: change the return type to: `T`
    |
    = note: requested on the command line with `-W clippy::unnecessary-box-returns`
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_box_returns

Note that the first two examples would be caught back by use_self anyway

@dswij
Copy link
Member

dswij commented Nov 19, 2022

hey @botahamec, sorry to nag, but it seems like there's not a lot of activity on this PR lately. Let me know if you don't have much time, and I might be able to continue the work on this PR.

@botahamec
Copy link
Contributor Author

@dswij Oh yeah, feel free.

@dswij
Copy link
Member

dswij commented Jan 7, 2023

r? @xFrednet

I've rebased this and update some outdated stuff. This should be ready to go, any improvements can be in future PRs since this is going to nursery.

Would you mind to help take a look if you have the time?

@bors
Copy link
Collaborator

bors commented Mar 8, 2023

☔ The latest upstream changes (presumably #10362) made this pull request unmergeable. Please resolve the merge conflicts.

@xFrednet
Copy link
Member

Hey @botahamec, since you pushed since my last review, have you seen the comments I left? Besides those two I think this is ready for merge :). Your welcome to reach out if you need help with the rebases or have any other questions :)

@botahamec
Copy link
Contributor Author

@xFrednet Yeah. I was hoping to get it done last week, but I never got around to it. I don't have my computer this week, so hopefully I'll finish it next week.

@xFrednet
Copy link
Member

Alright, no worries, just wanted to make sure you're not stuck on anything :). Enjoy your week!

@botahamec botahamec force-pushed the unused-box branch 8 times, most recently from 2e65e4a to 4d50aaa Compare March 26, 2023 21:39
@botahamec
Copy link
Contributor Author

Alright. I'm done squashing. That was surprisingly convoluted, but it's down to just four commits now.

@xFrednet
Copy link
Member

This version looks good to me. Thank you for sticking with this PR and completing it, I highly appreciate it. The squash also looks perfect. I hope you had fun :)

@bors r+

@bors
Copy link
Collaborator

bors commented Mar 30, 2023

📌 Commit 76d13bb has been approved by xFrednet

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Mar 30, 2023

⌛ Testing commit 76d13bb with merge ef3867f...

@bors
Copy link
Collaborator

bors commented Mar 30, 2023

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: xFrednet
Pushing ef3867f to master...

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.

Returning Box<T>
7 participants