diff --git a/clang-tools-extra/clang-tidy/bugprone/IncorrectEnableIfCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/IncorrectEnableIfCheck.cpp index 6181ac84f36e3..5a592e10a599e 100644 --- a/clang-tools-extra/clang-tidy/bugprone/IncorrectEnableIfCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/IncorrectEnableIfCheck.cpp @@ -47,8 +47,8 @@ void IncorrectEnableIfCheck::check(const MatchFinder::MatchResult &Result) { Result.Nodes.getNodeAs( "enable_if_specialization"); - if (!EnableIf || !EnableIfSpecializationLoc) - return; + assert(EnableIf); + assert(EnableIfSpecializationLoc); const SourceManager &SM = *Result.SourceManager; const SourceLocation RAngleLoc = @@ -57,9 +57,8 @@ void IncorrectEnableIfCheck::check(const MatchFinder::MatchResult &Result) { auto Diag = diag(EnableIf->getBeginLoc(), "incorrect std::enable_if usage detected; use " "'typename std::enable_if<...>::type'"); - // FIXME: This should handle the enable_if specialization already having an - // elaborated keyword. - if (!getLangOpts().CPlusPlus20) { + if (!getLangOpts().CPlusPlus20 && + EnableIfSpecializationLoc->getElaboratedKeywordLoc().isInvalid()) { Diag << FixItHint::CreateInsertion(EnableIfSpecializationLoc->getBeginLoc(), "typename "); } diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index eb735e6e62ee4..40723b98a2b78 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -224,6 +224,12 @@ Changes in existing checks positive when increment/decrement operators appear inside lambda bodies that are part of a condition expression. +- Improved :doc:`bugprone-incorrect-enable-if + ` check to not + insert an extraneous ``typename`` on code like + ``typename std::enable_if<...>``, where there's already a ``typename`` and + only the ``::type`` at the end is missing. + - Improved :doc:`bugprone-macro-parentheses ` check by printing the macro definition in the warning message if the macro is defined on command line. diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/incorrect-enable-if.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/incorrect-enable-if.cpp index 0c418efe1420b..81612a50e5bbd 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/incorrect-enable-if.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/incorrect-enable-if.cpp @@ -42,6 +42,12 @@ void invalid_multiple() {} // CHECK-FIXES: template ::type, typename = typename std::enable_if::type> // CHECK-FIXES-CXX20: template ::type, typename = std::enable_if::type> +template > +void invalid_typename_keyword() {} +// CHECK-MESSAGES: [[@LINE-2]]:23: warning: incorrect std::enable_if usage detected; use 'typename std::enable_if<...>::type' [bugprone-incorrect-enable-if] +// CHECK-FIXES: template ::type> +// CHECK-FIXES-CXX20: template ::type> + template > struct InvalidClass {}; // CHECK-MESSAGES: [[@LINE-2]]:23: warning: incorrect std::enable_if usage detected; use 'typename std::enable_if<...>::type' [bugprone-incorrect-enable-if]