Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do tuples not get unused variable warnings?

Tags:

c++

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};
}
  • clang 12.0.1: bs and p1 don't get warnings
  • gcc 11.2: bs and ps don't get warnings

Why don't these get warnings? Is there a way to mark the destructor of Bar such that it won't mask the warnings?

like image 212
yairchu Avatar asked Aug 12 '21 13:08

yairchu


People also ask

How do you avoid unused variable warnings in Python?

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.

How do I ignore unused variable warning?

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.

How do you ignore variables in C++?

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.

Why does the compiler tell me not to use unused variables?

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.

What is the meaning of ‘unused 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’?

What are the dangers of using unused variables in programming?

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.

Is it a good practice to remove the unused local variables?

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.


Video Answer


1 Answers

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

  • Bug 55203 - No unused warning for variables of non-trivial types

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]
}
like image 67
dfrib Avatar answered Oct 18 '22 19:10

dfrib