Skip to content

Add #[must_use] to types and methods#3395

Merged
mladedav merged 8 commits intotokio-rs:mainfrom
theodorebje:clippy-must-use
Jul 5, 2025
Merged

Add #[must_use] to types and methods#3395
mladedav merged 8 commits intotokio-rs:mainfrom
theodorebje:clippy-must-use

Conversation

@theodorebje
Copy link
Contributor

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.

Theodore Bjernhed 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.
@theodorebje theodorebje mentioned this pull request Jul 3, 2025
5 tasks
@theodorebje
Copy link
Contributor Author

I wonder if 8a561f3 would make the return_self_not_must_use lint more accurate? Maybe we should try and merge that first?

theodorebje and others added 4 commits July 4, 2025 23:38
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.
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.
Copy link
Collaborator

@mladedav mladedav left a comment

Choose a reason for hiding this comment

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

Looks good 👍

@jplatte
Copy link
Contributor

jplatte commented Jul 5, 2025

I agree. Any reason this is still marked as draft?

@theodorebje
Copy link
Contributor Author

Sorry, forgot to convert it to a PR.

@theodorebje
Copy link
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.

@theodorebje theodorebje marked this pull request as ready for review July 5, 2025 16:35
@theodorebje
Copy link
Contributor Author

Ready to merge now 👌

@mladedav mladedav merged commit fb64e72 into tokio-rs:main Jul 5, 2025
18 checks passed
@theodorebje theodorebje deleted the clippy-must-use branch July 5, 2025 18:10
jplatte pushed a commit that referenced this pull request Sep 28, 2025
Co-authored-by: Theodore Bjernhed <fosseder@danwin1210.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants