How do I best silence a warning about unused variables?
You can put it in "(void)var;
" expression (does nothing) so that a compiler sees it is used. This is portable between compilers.
E.g.
void foo(int param1, int param2)
{
(void)param2;
bar(param1);
}
Or,
#define UNUSED(expr) do { (void)(expr); } while (0)
...
void foo(int param1, int param2)
{
UNUSED(param2);
bar(param1);
}
What is the best way to suppress A Unused variable x warning? [duplicate]
I found an article, http://sourcefrog.net/weblog/software/languages/C/unused.html, that explains UNUSED. It is interesting that the author also mangles the unused variable name, so you can't inadvertently use it in the future.
Excerpt:
#ifdef UNUSED
#elif defined(__GNUC__)
# define UNUSED(x) UNUSED_ ## x __attribute__((unused))
#elif defined(__LCLINT__)
# define UNUSED(x) /*@unused@*/ x
#else
# define UNUSED(x) x
#endif
void dcc_mon_siginfo_handler(int UNUSED(whatsig))
How to suppress warnings about unused variables in C++?
Compile with the -Wno-unused-variable
option.
See the GCC documentation on Warning Options for more information.
The -Wno-__
options turn off the options set by -W__
. Here we are turning off -Wunused-variable
.
Also, you can apply the __attribute__((unused))
to the variable (or function, etc.) to suppress this warning on a case-by-case basis. Thanks Jesse Good for mentioning this.
Silencing unused variable warning in JS
in the TSConfig file you can set "no-unused-variable": true
that will make it stop complain about all the unused variables in the entire project
You can also put an underscore before the variable name if you want TS to ignore only that specific unused variable like this:
function foo(_bar:number) {
return 0;
}
Which is the best way to suppress unused variable warning
This is the approach I use: cross platform macro for silencing unused variables warning
It allows you to use one macro for any platform (although the definitions may differ, depending on the compiler), so it's a very portable approach to express your intention to popular compilers for C based languages. On GCC and Clang, it is equivalent of wrapping your third example (#pragma unused(testString)
) into a macro.
Using the example from the linked answer:
- (void)testString:(NSString *)testString
{
MONUnusedParameter(testString);
}
I've found this approach best for portability and clarity, in use with some pretty large C, C++, ObjC, and ObjC++ codebases.
How can I suppress unused parameter warnings in C?
I usually write a macro like this:
#define UNUSED(x) (void)(x)
You can use this macro for all your unused parameters. (Note that this works on any compiler.)
For example:
void f(int x) {
UNUSED(x);
...
}
Suppress warning for unused parameters in function [duplicate]
Assuming this
int f(int x, int y)
{
return y;
}
to provoke the warning mentiond by the OP.
For gcc just do this
int f(int x __attribute__ ((unused)), int y)
{
return y;
}
to have the waninng disappear.
A more generic trick is to just do:
int f(int x, int y)
{
x = x;
return y;
}
For gcc specifing the option -Wno-unused-parameter
should disable this warning.
How to disable unused variable warning in Rust?
The correct is
fn main() {
#[allow(unused_variables)]
let x = 0;
}
Related Topics
How to Get Enum Item Name from Its Value
What Are Copy Elision and Return Value Optimization
Is Short-Circuiting Logical Operators Mandated? and Evaluation Order
Strange Output in Comparison of Float With Float Literal
How to Read an Entire File into a Std::String in C++
What Are the Evaluation Order Guarantees Introduced by C++17
What Is Ownership of Resources or Pointers
Using Base Pointer Register in C++ Inline Asm
Difference Between Float and Double
Is It Okay to Inherit Implementation from Stl Containers, Rather Than Delegate
Undefined Reference to Static Class Member
Resolve Build Errors Due to Circular Dependency Amongst Classes
What Is the Curiously Recurring Template Pattern (Crtp)
How to Find the Length of an Array
Derived Template-Class Access to Base-Class Member-Data
Opencv C++/Obj-C: Detecting a Sheet of Paper/Square Detection