[clang-tools-extra] [clang-tidy] Fix `bugprone-incorrect-enable-if` inserting duplicate `typename` (PR #190899)
Victor Chernyakin via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 9 18:13:22 PDT 2026
https://github.com/localspook updated https://github.com/llvm/llvm-project/pull/190899
>From d6006506c73a20314b1ed3ffe0fe50e2104ea6e9 Mon Sep 17 00:00:00 2001
From: Victor Chernyakin <chernyakin.victor.j at outlook.com>
Date: Tue, 7 Apr 2026 19:54:07 -0700
Subject: [PATCH 1/2] [clang-tidy] Fix `bugprone-incorrect-enable-if` inserting
duplicate `typename`
---
.../clang-tidy/bugprone/IncorrectEnableIfCheck.cpp | 9 ++++-----
clang-tools-extra/docs/ReleaseNotes.rst | 6 ++++++
.../clang-tidy/checkers/bugprone/incorrect-enable-if.cpp | 6 ++++++
3 files changed, 16 insertions(+), 5 deletions(-)
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<TemplateSpecializationTypeLoc>(
"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..60fd785246f54 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
+ <clang-tidy/checks/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
<clang-tidy/checks/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 <typename T, typename = typename std::enable_if<T::some_value>::type, typename = typename std::enable_if<T::other_value>::type>
// CHECK-FIXES-CXX20: template <typename T, typename = std::enable_if<T::some_value>::type, typename = std::enable_if<T::other_value>::type>
+template <typename T, typename = typename std::enable_if<T::some_value>>
+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 <typename T, typename = typename std::enable_if<T::some_value>::type>
+// CHECK-FIXES-CXX20: template <typename T, typename = typename std::enable_if<T::some_value>::type>
+
template <typename T, typename = std::enable_if<T::some_value>>
struct InvalidClass {};
// CHECK-MESSAGES: [[@LINE-2]]:23: warning: incorrect std::enable_if usage detected; use 'typename std::enable_if<...>::type' [bugprone-incorrect-enable-if]
>From e67ac9e993c308d91ad5f03885aaf6546567f6c1 Mon Sep 17 00:00:00 2001
From: Victor Chernyakin <chernyakin.victor.j at outlook.com>
Date: Thu, 9 Apr 2026 18:13:12 -0700
Subject: [PATCH 2/2] Adjust line wrapping
Co-authored-by: EugeneZelenko <eugene.zelenko at gmail.com>
---
clang-tools-extra/docs/ReleaseNotes.rst | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 60fd785246f54..40723b98a2b78 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -227,8 +227,8 @@ Changes in existing checks
- Improved :doc:`bugprone-incorrect-enable-if
<clang-tidy/checks/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.
+ ``typename std::enable_if<...>``, where there's already a ``typename`` and
+ only the ``::type`` at the end is missing.
- Improved :doc:`bugprone-macro-parentheses
<clang-tidy/checks/bugprone/macro-parentheses>` check by printing the macro
More information about the cfe-commits
mailing list