Skip to content

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

Closed
@jruderman

Description

@jruderman

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

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions