[clang-tidy] Fix bugprone-incorrect-enable-if inserting duplicate typename#190899
Merged
Conversation
Member
|
@llvm/pr-subscribers-clang-tidy @llvm/pr-subscribers-clang-tools-extra Author: Victor Chernyakin (localspook) ChangesThis PR resolves one of our FIXME's. Pre-C++20, this check turns typename std::enable_if<...>into typename typename std::enable_if<...>::typeinstead of typename std::enable_if<...>::typeFull diff: https://github.com/llvm/llvm-project/pull/190899.diff 3 Files Affected:
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..4f5a219747bae 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -219,6 +219,12 @@ Changes in existing checks
loss in overloads with transparent standard functors (e.g. ``std::plus<>``)
for ``std::accumulate``, ``std::reduce``, and ``std::inner_product``.
+- 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-inc-dec-in-conditions
<clang-tidy/checks/bugprone/inc-dec-in-conditions>` check by fixing a false
positive when increment/decrement operators appear inside lambda bodies that
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]
|
22cc58e to
d600650
Compare
zeyi2
approved these changes
Apr 8, 2026
Co-authored-by: EugeneZelenko <eugene.zelenko@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR resolves one of our FIXME's. Pre-C++20, this check turns
typename std::enable_if<...>into
instead of
typename std::enable_if<...>::typehttps://godbolt.org/z/4s4ozPsM3