PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, July 17, 2022

[FIXED] How to quiet a warning for a single statement in Rust?

 July 17, 2022     rust, warnings     No comments   

Issue

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.


Solution

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.



Answered By - Lukas Kalbertodt
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing