Add #[must_use] to types and methods#3395
Merged
mladedav merged 8 commits intotokio-rs:mainfrom Jul 5, 2025
Merged
Conversation
added 3 commits
July 3, 2025 13:58
Methods returning `Self` often create new instances of a type without causing any side-effects (such as interacting with other types). Unlike for `must_use_candidate`, it is almost guaranteed that the user intended to use the newly created value, and simply just forgot. This fixes the `return_self_not_must_use` clippy lint.
Add `#[must_use]` attribute to public functions that return something not already marked must-use, have no mutable arg and mutate no statics. This fixes the `must_use_candidate` clippy lint.
Adding `#[must_use]` to each of a type's method is overly verbose and cluttery. This commit moves some of these `#[must_use]` calls to the type definitions instead.
Contributor
Author
|
I wonder if 8a561f3 would make the |
jplatte
reviewed
Jul 4, 2025
Adding `#[must_use]` to each of a type's method is overly verbose and cluttery. This commit moves some of these `#[must_use]` calls to the type definitions instead. This is the second time doing this. See fdc0147 for the first commit doing this.
mladedav
reviewed
Jul 5, 2025
Improve the compilation error messages when never using a returned `CookieJar` (or similar structs) Also add `#[must_use]` to the `CookieJarIter` versions of those structs, with custom error messages.
Contributor
|
I agree. Any reason this is still marked as draft? |
Contributor
Author
|
Sorry, forgot to convert it to a PR. |
Contributor
Author
|
I'm on GitHub mobile right now, I can't find a way to convert it... Are any of you able to convert it for me? Otherwise I'll convert it in a few hours when I get home. |
Contributor
Author
|
Ready to merge now 👌 |
jplatte
pushed a commit
that referenced
this pull request
Sep 28, 2025
Co-authored-by: Theodore Bjernhed <fosseder@danwin1210.de>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
This PR is part of the broader PR #3394 (Activate more lints)
Sometimes, users forget to use their newly created values after calling certain methods. To make Axum more user-friendly, we should help them remember to use these values.
Solution
The
#[must_use]attribute does exactly that. It causes a warning if the resulting value is never used.This PR also adds 3 lints to
Cargo.toml.Blockers
What types should receive the
#[must_use]attribute? More importantly, which ones should not? I imagine a GitHub search might help.