Skip to content

Add #[must_use] to cargo new --lib example code#16806

Closed
tyilo wants to merge 3 commits into
rust-lang:masterfrom
tyilo:cargo-new-must-use
Closed

Add #[must_use] to cargo new --lib example code#16806
tyilo wants to merge 3 commits into
rust-lang:masterfrom
tyilo:cargo-new-must-use

Conversation

@tyilo
Copy link
Copy Markdown

@tyilo tyilo commented Mar 30, 2026

What does this PR try to resolve?

This adds the #[must_use] attribute to the example lib code so that cargo clippy -- -Wclippy::pedantic doesn't emit any lints.

Before this change, clippy::must_use_candidate is emitted.

How to test and review this PR?

Run cargo new --lib and cargo clippy -- -Wclippy::pedantic and see that clippy::must_use_candidate is no longer emitted.

@rustbot rustbot added Command-new S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 30, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 30, 2026

r? @epage

rustbot has assigned @epage.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @ehuss, @epage, @weihanglo
  • @ehuss, @epage, @weihanglo expanded to ehuss, epage, weihanglo
  • Random selection from ehuss, epage, weihanglo

"
} else {
b"\
#[must_use]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think this is the only warning you got when running clippy with -Wclippy::pedantic. We have deliberately scoped clippy lints to a subset. We normally don't accept unless there is some compelling reason, for example:

Would you mind share the reason motivating you to open this PR (and only with this lint fixed)? Otherwise I tend to close this. Thank you.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

It is the only lint.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

While this is might be the only lint in the generated code, I think the approach we take to lints in Cargo is relevant.

It is also not clear why this matters. These are pedantic (non-default) lints for code that will be deleted.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

While this is might be the only lint in the generated code, I think the approach we take to lints in Cargo is relevant.

Ah, I understand what @weihanglo meant now. I was only talking about the generated code.

It is also not clear why this matters. These are pedantic (non-default) lints for code that will be deleted.

I think the generated example code should be as idiomatic as possible, even if it will be deleted.

But I also understand why this isn't important enough to change. Feel free to close the PR.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think the generated example code should be as idiomatic as possible, even if it will be deleted.

If this is something considered idiomatic, I'd want to better understand why it is relegated to a pedantic lint.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

If this is something considered idiomatic, I'd want to better understand why it is relegated to a pedantic lint.

Well its on Add::add in the standard library and this function implements the same functionality:

#[must_use = "this returns the result of the operation, without modifying the original"]

It is a pedantic lint because it has a lot of false positives, but in this specific case the function should have a #[must_use] attribute. See "Known problems" here: https://rust-lang.github.io/rust-clippy/master/#must_use_candidate

@arlosi
Copy link
Copy Markdown
Contributor

arlosi commented Apr 7, 2026

Thanks for the suggestion, but we're very conservative with changes to examples generated by cargo new. In the lib case, users will immediately delete the add function anyway, and it's a slippery slope of what idiomatic/best practices to include in the cargo new template.

#5151 tracks implementation of a templating system for users to be able to customize it to their needs.

@arlosi arlosi closed this Apr 7, 2026
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 7, 2026
@tyilo tyilo deleted the cargo-new-must-use branch April 7, 2026 17:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants