In the following example, compiling with -Wall
, some of the unused variables are not warned about:
#include <tuple>
struct Foo
{
int a, b;
};
struct Bar
{
~Bar() {}
int a, b;
};
int three() { return 3; }
int main()
{
Foo f0 {1, 2};
Foo f1 {three(), 2};
Bar b0 {1, 2};
Bar b1 {three(), 2};
std::tuple<int, int> p0 {1, 2};
std::tuple<int, int> p1 {three(), 2};
}
b
s and p1
don't get warningsb
s and p
s don't get warningsWhy don't these get warnings? Is there a way to mark the destructor of Bar
such that it won't mask the warnings?
The warnings won't interrupt the program execution. To suppress the warning, one can simply name the variable with an underscore ('_') alone. Python treats it as an unused variable and ignores it without giving the warning message.
By default, the compiler does not warn about unused variables. Use -Wunused-variable to enable this warning specifically, or use an encompassing -W value such as -Weverything . The __attribute__((unused)) attribute can be used to warn about most unused variables, but suppress warnings for a specific set of variables.
You can use the _attribute_ keyword to assign the "unused" attribute to a variable. Doing so will cause the compiler to not generate any warnings if the variable is not referenced. You can assign the "unused" attribute to a method parameter in the method declaration.
So a possible reason for an unused variable is that there is a mistake in your code. That is why the compiler warns you. If you "don't do" unused variables, it is even a dead giveaway. When you see such a warning, you should verify your code and either fix it or remove the offending variable.
Naming the variable as ‘dummy’, ‘unused’ or any name that conveys that this variable is currently not used anywhere. How to suppress the warning ‘unused variable’?
However, in my view the greater danger is to yourself. An unused variable might be intentional, or it might be an oversight pointing to a defect. For instance, you might have mistyped a name and stored a value in one place when you thought you'd stored it in another. The resulting program could run fine but silently give the wrong result.
It is always good practice to remove the unused local variables. A code with many unused local variables, unused imports, or unused line of codes is considered to be dead code.
The compiler will do its best to flag unused variables, but this is difficult for non-trivial types, and both Bar
and std::tuple<int, int>
are non-trivial.
static_assert(std::is_trivial_v<Foo>);
static_assert(!std::is_trivial_v<Bar>);
static_assert(!std::is_trivial_v<std::tuple<int, int>>);
As is described in
As the compiler cannot tell for sure in some cases, such as the ctor calling an external function, the only reasonable way of handling this I see is explicit tagging of types for which there should be an unused warning if only ctor/dtor are called for a variable. Here's my attempt at attribute warn_unused.
you could use GCC's warn_unused
attribute if you want a -Wunused-variable
to be emitted in case on the constructor and/or destructor of the variable of the non-trivial type is called.
struct __attribute__((warn_unused)) Bar {
~Bar() {}
int a, b;
};
int main() {
Bar b{1, 3}; // warning: unused variable 'b' [-Wunused-variable]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With