Skip to content

Missing unused_must_use warning for macro calls #55240

@snoyberg

Description

@snoyberg

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions