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

Add new lint to warn when #[must_use] attribute should be used on a method #8071

Merged
merged 3 commits into from
Dec 8, 2021

Conversation

GuillaumeGomez
Copy link
Member

@GuillaumeGomez GuillaumeGomez commented Dec 3, 2021

This lint is somewhat similar to https://rust-lang.github.io/rust-clippy/master/index.html#must_use_candidate but also different: it emits a warning by default and only targets methods (so not functions nor associated functions).

Someone suggested it to me after this tweet: https://twitter.com/m_ou_se/status/1466439813230477312

I think it would reduce the number of cases of API misuses quite a lot.

What do you think?


changelog: Added new [return_self_not_must_use] lint

@rust-highfive
Copy link

r? @xFrednet

(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 Dec 3, 2021
hir_id: HirId,
) {
if_chain! {
// We are only interested in methods, not in functions or associated functions.
Copy link

Choose a reason for hiding this comment

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

Why not? What about fn new() -> Self, fn new_foo(bar: Baz) -> Self or fn from_blurb(blurb: Blurb) -> Self?

Copy link
Member Author

Choose a reason for hiding this comment

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

They look like different use cases to me. What I'm trying to prevent here is cases like 0f32.rad(2.). It's much less likely (in my opinion) if it's not a method.

Copy link

Choose a reason for hiding this comment

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

Ah sure, if you don't want to cover that case here that makes sense. I also think it's quite unlikely that someone calls such a constructor function and forgets to actually do something with the result. Adding #[must_use] on all these might add quite a bit of noise to the code.

tests/ui/must_use_self_return.rs Outdated Show resolved Hide resolved
@GuillaumeGomez
Copy link
Member Author

Updated with @sdroege's suggestions.

@camsteffen
Copy link
Contributor

I'd call it return_self_not_must_use so that it makes sense with allow.

For the category I'd do suspicious or maybe pedantic, depending on the supposed false positive rate. I'm not really sure if there will be false positives, but it's possible there are methods that you can call for a side effect and drop the returned value.

@GuillaumeGomez GuillaumeGomez force-pushed the method-must-use branch 2 times, most recently from ca04751 to aecc38d Compare December 4, 2021 14:57
@GuillaumeGomez
Copy link
Member Author

@camsteffen This is indeed much better, I renamed the lint.

@GuillaumeGomez
Copy link
Member Author

And updated the category to pedantic.

Copy link
Member

@xFrednet xFrednet left a comment

Choose a reason for hiding this comment

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

The implementation logic looks good to me. There are some minor improvements to be made, and then it should be ready to be merged. 👍

Also, a thank you to the ones who have given feedback already. I appreciate it! 🙃

clippy_lints/src/return_self_not_must_use.rs Outdated Show resolved Hide resolved
tests/ui/auxiliary/option_helpers.rs Show resolved Hide resolved
clippy_lints/src/return_self_not_must_use.rs Show resolved Hide resolved
clippy_utils/src/lib.rs Outdated Show resolved Hide resolved
@GuillaumeGomez
Copy link
Member Author

Updated with comments from @xFrednet. :)

@xFrednet
Copy link
Member

xFrednet commented Dec 8, 2021

Everything looks good to me, thank you for the work!

I ran the lint on 25 crates with lintcheck and got about 60 reports, the once I've looked at seem to be correct. 🙃

@bors r+

@bors
Copy link
Contributor

bors commented Dec 8, 2021

📌 Commit e93767b has been approved by xFrednet

@bors
Copy link
Contributor

bors commented Dec 8, 2021

⌛ Testing commit e93767b with merge 19d90bf...

bors added a commit that referenced this pull request Dec 8, 2021
Add new lint to warn when #[must_use] attribute should be used on a method

This lint is somewhat similar to https://rust-lang.github.io/rust-clippy/master/index.html#must_use_candidate but also different: it emits a warning by default and only targets methods (so not functions nor associated functions).

Someone suggested it to me after this tweet: https://twitter.com/m_ou_se/status/1466439813230477312

I think it would reduce the number of cases of API misuses quite a lot.

What do you think?
@bors
Copy link
Contributor

bors commented Dec 8, 2021

💔 Test failed - checks-action_test

@xFrednet
Copy link
Member

xFrednet commented Dec 8, 2021

Clippy created the changelog based on the PR description. I added the corresponding message 🙃

@bors r+

@bors
Copy link
Contributor

bors commented Dec 8, 2021

⌛ Testing commit e93767b with merge 55afaa9...

bors added a commit that referenced this pull request Dec 8, 2021
Add new lint to warn when #[must_use] attribute should be used on a method

This lint is somewhat similar to https://rust-lang.github.io/rust-clippy/master/index.html#must_use_candidate but also different: it emits a warning by default and only targets methods (so not functions nor associated functions).

Someone suggested it to me after this tweet: https://twitter.com/m_ou_se/status/1466439813230477312

I think it would reduce the number of cases of API misuses quite a lot.

What do you think?

---

changelog: Added new [`return_self_not_must_use`] lint
@bors
Copy link
Contributor

bors commented Dec 8, 2021

⌛ Testing commit e93767b with merge 5305979...

@bors
Copy link
Contributor

bors commented Dec 8, 2021

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

@bors bors merged commit 5305979 into rust-lang:master Dec 8, 2021
@GuillaumeGomez
Copy link
Member Author

Thanks!

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.

6 participants