[clang-tools-extra] [clang-tidy] Fix false positive in `readability-redundant-typename` (PR #170034)

Victor Chernyakin via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 2 13:32:35 PST 2025


================
@@ -47,8 +47,10 @@ void RedundantTypenameCheck::check(const MatchFinder::MatchResult &Result) {
   const SourceLocation ElaboratedKeywordLoc = [&] {
     if (const auto *NonDependentTypeLoc =
             Result.Nodes.getNodeAs<TypeLoc>("nonDependentTypeLoc")) {
-      if (const auto TL = NonDependentTypeLoc->getAs<TypedefTypeLoc>())
-        return TL.getElaboratedKeywordLoc();
+      if (const auto TL = NonDependentTypeLoc->getAs<TypedefTypeLoc>()) {
+        if (!TL.getType()->isDependentType())
+          return TL.getElaboratedKeywordLoc();
+      }
 
       if (const auto TL = NonDependentTypeLoc->getAs<TagTypeLoc>())
         return TL.getElaboratedKeywordLoc();
----------------
localspook wrote:

I think we need the `!TL.getType()->isDependentType()` check here as well, because it seems not only `TypedefTypeLoc`s have this problem, but `TagTypeLoc`s too; here's a snippet that demonstrates that:
```cpp
template<typename T> struct ListWrapper {};
template<typename T>
class ClassWrapper {
public:
    struct Argument {}; // Previously was: using Argument = ListWrapper<T>;
    ListWrapper<Argument> arguments;
    ListWrapper<Argument> getArguments() const;
};
template<typename T>
ListWrapper</*false positive*/typename ClassWrapper<T>::Argument> ClassWrapper<T>::getArguments() const {
    return arguments;
}
```
At that point, however, we're repeating ourselves 3 times, so I think it would be better to wrap everything in the *outer* `if` with that check.

https://github.com/llvm/llvm-project/pull/170034


More information about the cfe-commits mailing list