Skip to content

[Clang] Issuing a deprecated warning when the chosen template specialization does not use the deprecated class #112366

@dyniols

Description

@dyniols

Following code produces warning for 'deprecated_class' but the class which uses deprecated class doesn't use that template specialization. It seems that specializations that are instantiated during overload resolution are diagnosing the attribute on them even if that overload is not selected.

https://godbolt.org/

#ifdef BE_THE_HEADER
#pragma clang system_header
#include <type_traits>
template<typename T>
class [[deprecated]] deprecated_class{};

template<int Dimensions, typename DataT>
class using_deprecated{
  public:
  template <int  Dims = Dimensions,
  typename = std::enable_if_t<(Dims > 0), int>>
  int foo() const {
    return Dims; 
  }

  template <int  Dims = Dimensions>
  typename std::enable_if_t<(Dims == 0), deprecated_class<DataT>>
  foo() const {
    return deprecated_class<DataT>();
  }
};
#else
#define BE_THE_HEADER
#include __FILE__
int main(){
  using_deprecated<1, int> test_obj;
  auto return_value = test_obj.foo();
}
#endif

Compiling output with -Wall flag

In file included from main.cpp:24:
main.cpp:17:42: warning: 'deprecated_class<int>' is deprecated [-Wdeprecated-declarations]
   17 |   typename std::enable_if_t<(Dims == 0), deprecated_class<DataT>>
      |                                          ^
main.cpp:26:28: note: in instantiation of template class 'using_deprecated<1, int>' requested here
   26 |   using_deprecated<1, int> test_obj;
      |                            ^
main.cpp:5:9: note: 'deprecated_class<int>' has been explicitly marked deprecated here
    5 | class [[deprecated]] deprecated_class{};
      |         ^
main.cpp:27:8: warning: unused variable 'return_value' [-Wunused-variable]
   27 |   auto return_value = test_obj.foo();

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions