diff --git a/clang-tools-extra/clang-tidy/misc/DefinitionsInHeadersCheck.cpp b/clang-tools-extra/clang-tidy/misc/DefinitionsInHeadersCheck.cpp --- a/clang-tools-extra/clang-tidy/misc/DefinitionsInHeadersCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/DefinitionsInHeadersCheck.cpp @@ -134,6 +134,9 @@ DiagnosticIDs::Note) << FixItHint::CreateInsertion(FD->getInnerLocStart(), "inline "); } else if (const auto *VD = dyn_cast(ND)) { + // C++14 variable templates are allowed. + if (VD->getDescribedVarTemplate()) + return; // Static data members of a class template are allowed. if (VD->getDeclContext()->isDependentContext() && VD->isStaticDataMember()) return; diff --git a/clang-tools-extra/docs/clang-tidy/checks/misc-definitions-in-headers.rst b/clang-tools-extra/docs/clang-tidy/checks/misc-definitions-in-headers.rst --- a/clang-tools-extra/docs/clang-tidy/checks/misc-definitions-in-headers.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/misc-definitions-in-headers.rst @@ -83,6 +83,10 @@ constexpr int f10() { return 0; } // OK: constexpr function implies inline. + // OK: C++14 variable templates are inline. + template + constexpr T pi = T(3.1415926L); + Options ------- diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc-definitions-in-headers-1z.hpp b/clang-tools-extra/test/clang-tidy/checkers/misc-definitions-in-headers-1z.hpp --- a/clang-tools-extra/test/clang-tidy/checkers/misc-definitions-in-headers-1z.hpp +++ b/clang-tools-extra/test/clang-tidy/checkers/misc-definitions-in-headers-1z.hpp @@ -1,4 +1,4 @@ -// RUN: %check_clang_tidy %s misc-definitions-in-headers %t -- -- -std=c++1z +// RUN: %check_clang_tidy -std=c++17-or-later %s misc-definitions-in-headers %t class CE { constexpr static int i = 5; // OK: inline variable definition. @@ -8,3 +8,7 @@ int b = 1; // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable 'b' defined in a header file; variable definitions in header files can lead to ODR violations [misc-definitions-in-headers] + +// OK: C++14 variable template. +template +constexpr T pi = T(3.1415926L);