34

Say there is a single warning such as path_statements, unused_variables. Is there a way to ignore a single instant of this, without isolating them into a code block or function?

To be clear, when there is a single warning in the code. I would like the ability to quiet only that warning, without having to do special changes addressing the particular warning. And without this quieting warnings anywhere else, even later on in the same function.

With GCC this can be done as follows:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat"
    /* Isolated region which doesn't raise warnings! */
    this_would_raise_Wformat(args);
#pragma GCC diagnostic pop

Does Rust have the equivalent capability?


Note, am asking about the general case of how to quiet warnings. Am aware there are ways to resolve unused var warning for eg.

ideasman42
  • 42,413
  • 44
  • 197
  • 320
  • 1
    You mean to ignore all warnings of a kind in a module? The question is a bit unclear. – ljedrz Sep 01 '16 at 11:08
  • @LukasKalbertodt, not a duplicate, am asking about silencing _any_ warnings. – ideasman42 Sep 01 '16 at 11:14
  • When you run rustc, it may give many warnings, a single instant would be one of them. Updated question to be more clear, giving example of how this can already be done with GCC. – ideasman42 Sep 01 '16 at 11:23
  • `#[allow(...)]` on single statement seems like what I'm looking for, worth adding this as an answer? – ideasman42 Sep 01 '16 at 11:25
  • I get that sometimes a scope is not usable (e.g., because the statement in question introduces a new variable), but if adding a block around the statement *works*, why wouldn't you use it? It's some line noise, but much less so than the GCC equivalent you show. –  Sep 01 '16 at 12:05
  • @delnan, often using a block makes most sense... however warnings can be be intermittent *(depending on platform, compiler versions... other conditional compilation)*. It may not be desirable to indent a large block of code, just to quiet warnings under some spesific configuration. – ideasman42 Sep 01 '16 at 13:15
  • Rust isn't whitespace-sensitive, indentation is not necessary. – ljedrz Sep 01 '16 at 14:24
  • If your adding new scopes without indenting it makes the code confusing in other ways, and in this case I would certainly prefer to quiet a single warning then introduce scopes in a hard-to-read way. Adding braces around blocks without indentation will confuse things longer term. You may declare variables and try access them later without realizing the scope is different failing to build or worse accessing a variable which was meant to be shadowed. While it may be acceptable in a few isolated cases, doing this all over the code-base is going to make it confusing/annoying to work on. – ideasman42 Sep 05 '16 at 02:52

2 Answers2

46

To silence warnings you have to add the allow(warning_type) attribute to the affected expression or any of its parents. If you only want to silence the warning on one specific expression, you can add the attribute to that expression/statement:

fn main() {
    #[allow(unused_variables)]
    let not_used = 27;

    #[allow(path_statements)]
    std::io::stdin;

    println!("hi!");
}

However, the feature of adding attributes to statements/expressions (as opposed to items, like functions) is still a bit broken. In particular, in the above code, the std::io::stdin line still triggers a warning. You can read the ongoing discussion about this feature here.


Often it is not necessary to use an attribute though. Many warnings (like unused_variables and unused_must_use) can be silenced by using let _ = as the left side of your statement. In general, any variable that starts with an underscore won't trigger unused-warnings.

Lukas Kalbertodt
  • 79,749
  • 26
  • 255
  • 305
7

If you want to silence all warnings of a kind in a module, write e.g. #![allow(dead_code)] (note the exclamation mark) at the top of the module. This will disable all warnings of this kind in the whole module. You can also call rustc with e.g. -A dead_code.

You can disable all warnings by writing #![allow(warnings)] at the top of the module.

You can insert a module (as described in the Rust book) where the specific warnings are ignored.

As Lukas said, you can also write e.g. #[allow(dead_code)] on a statement or an expression.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
ljedrz
  • 20,316
  • 4
  • 69
  • 97