Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clang emits an "unused type alias" warning for a type alias that is used

I have some code that Clang is generating a warning for. This is simplified from the actual code, but the spirit is the same. this_t in the local class is used to instantiate some other template class.

template<class T>
struct value_holder
{
    T  value;
};

template<class T>
int get_value()
{
    struct value_t
    {
        using this_t = value_t;
        //    ^ here
        static value_holder<this_t> val()
        {
            return value_holder<this_t>();
        }

        operator int()
        { return 0; }
    };
    return value_t::val().value;
}

int main(int argc, char** argv) {
    return get_value<void>();
}

When compiled with -std=c++1z -Wall, Clang warns about unused type alias:

main.cpp:14:15: warning: unused type alias 'this_t' [-Wunused-local-typedef]
        using this_t = value_t;
              ^
1 warning generated.

You can see the error on godbolt (6.0, trunk) and locally I'm using Clang 7 which reports the same thing.

This warning only appears when the local class is nested in a template function or method of a template class. When the class is nested in a concrete class or function there is no warning.

Is Clang correct to emit this warning here? The this_t type is used in the return type of value_t::val().

like image 619
Chris Hunt Avatar asked May 06 '18 22:05

Chris Hunt


1 Answers

It looks like it is a bug in Clang (24883, 33298), first reported in 2015 against Clang 3.7. I tried in godbolt and it appears to occur as far back as 3.6.

like image 136
Chris Hunt Avatar answered Nov 13 '22 12:11

Chris Hunt