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 explanatory message for [#must_use] on std::ops::Add et al #103320

Closed
jruderman opened this issue Oct 20, 2022 · 2 comments · Fixed by #103761
Closed

Add explanatory message for [#must_use] on std::ops::Add et al #103320

jruderman opened this issue Oct 20, 2022 · 2 comments · Fixed by #103761
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jruderman
Copy link
Contributor

jruderman commented Oct 20, 2022

Given the following code (playground):

use std::ops::Add;

fn main() {
    let mut x = 2_u32;
    x += 3;
    x.add(4);
    println!("{}", x);
}

The current output is:

warning: unused return value of `add` that must be used
 --> src/main.rs:6:5
  |
6 |     x.add(4);
  |     ^^^^^^^^^
  |
  = note: `#[warn(unused_must_use)]` on by default

Ideally the output should include:

  = note: this returns the result of the operation, without modifying the original

Precedent

Many other std methods have this exact must_use message, e.g. (playground):

use std::path::Path;

fn f(p: &Path) { 
    p.to_str();    
}

which gives the warning:

warning: unused return value of `Path::to_str` that must be used
 --> src/lib.rs:4:5
  |
4 |     p.to_str();    
  |     ^^^^^^^^^^^
  |
  = note: this returns the result of the operation, without modifying the original
  = note: `#[warn(unused_must_use)]` on by default
@jruderman jruderman added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 20, 2022
@chenyukang
Copy link
Member

@rustbot claim

@chenyukang
Copy link
Member

chenyukang commented Oct 22, 2022

Change this line:

to

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

will fix it, we may add attribute value to other trait such as Sub, Mul.
I'm not sure why whether it's necessary to keep it.
@m-ou-se How do you think about it?

chenyukang added a commit to chenyukang/rust that referenced this issue Feb 1, 2023
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Feb 2, 2023
…use, r=compiler-errors

Add explanatory message for [#must_use] in ops

Fixes rust-lang#103320
bors added a commit to rust-lang-ci/rust that referenced this issue Feb 6, 2023
…e, r=compiler-errors

Add explanatory message for [#must_use] in ops

Fixes rust-lang#103320
@bors bors closed this as completed in cb55d10 Feb 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants