Skip to content

Missing unused_must_use warning for macro calls #55240

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
snoyberg opened this issue Oct 21, 2018 · 2 comments
Closed

Missing unused_must_use warning for macro calls #55240

snoyberg opened this issue Oct 21, 2018 · 2 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.

Comments

@snoyberg
Copy link
Contributor

snoyberg commented Oct 21, 2018

This code is pretty close to my original code:

use std::fmt::{Formatter, Display, self};

struct Foo;

impl Display for Foo {
    fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
        write!(fmt, "ignored result\n");

        let mut closure = || {
            write!(fmt, "not ignoring result\n")
        };

        closure(); // also ignoring here

        Ok(())
    }
}

fn main() {
    println!("{}", Foo);
}

Expected: a warning on the first write! call and the call to closure about an ignored Result value.

Actual: I get the warning on the closure call, but not on the first write! call.

My best guess is that for some reason Results returned from a macro do not trigger this warning, so I came up with a smaller attempted repro. However, this shows a slightly different result:

macro_rules! err_macro {
    () => (Err(String::from("error from macro")) as Result<(), String>)
}

fn err_function() -> Result<(), String> {
    Err(String::from("error from function"))
}

fn main() {
    err_function();
    err_macro!();
}

As expected, I get a warning on the call to err_function(). I also get a warning for the macro call, in both the definition and use locations:

warning: unused `std::result::Result` which must be used
  --> where-da-warning-2.rs:2:12
   |
2  |     () => (Err(String::from("error from macro")) as Result<(), String>)
   |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
11 |     err_macro!();
   |     ------------- in this macro invocation
   |
   = note: this `Result` may be an `Err` variant, which should be handled

Perhaps the reason the write! macro doesn't generate a warning is because the its a macro being pulled in from a different library, not being defined locally.

Meta

$ rustc --version --verbose
rustc 1.31.0-nightly (96cafc53c 2018-10-09)
binary: rustc
commit-hash: 96cafc53cfc6667a03c8e77d8e0a2fc96555ff6b
commit-date: 2018-10-09
host: x86_64-apple-darwin
release: 1.31.0-nightly
LLVM version: 8.0
@zackmdavis zackmdavis added the A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. label Oct 21, 2018
@psibi
Copy link
Member

psibi commented Jan 25, 2020

This issue seems to have been fixed as I'm getting proper warning on your above code:

warning: unused `std::result::Result` that must be used
 --> /home/sibi/foo.rs:7:9
  |
7 |         write!(fmt, "ignored result\n");
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_must_use)]` on by default
  = note: this `Result` may be an `Err` variant, which should be handled
  = note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

warning: unused `std::result::Result` that must be used
  --> /home/sibi/foo.rs:11:9
   |
11 |         closure(); // also ignoring here
   |         ^^^^^^^^^^
   |
   = note: this `Result` may be an `Err` variant, which should be handled

My rust version:

~/g/bouncy-args (master) $ rustc --version --verbose
rustc 1.40.0 (73528e339 2019-12-16)
binary: rustc
commit-hash: 73528e339aae0f17a15ffa49a8ac608f50c6cf14
commit-date: 2019-12-16
host: x86_64-unknown-linux-gnu
release: 1.40.0
LLVM version: 9.0

@snoyberg
Copy link
Contributor Author

Thanks @psibi! I'll close this out.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.
Projects
None yet
Development

No branches or pull requests

3 participants