From 49774448d69b55f5c46aef2147b45537fd61276a Mon Sep 17 00:00:00 2001 From: mitchell Date: Wed, 3 Dec 2025 18:41:45 +0800 Subject: [PATCH] [clang-tidy] Fix false positive in `readability-redundant-typename` (#170034) Closes #169166 --------- Co-authored-by: Victor Chernyakin --- .../readability/RedundantTypenameCheck.cpp | 6 +++-- .../readability/redundant-typename.cpp | 24 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/clang-tidy/readability/RedundantTypenameCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantTypenameCheck.cpp index a4edd2b46b86..5f2519ce9d5c 100644 --- a/clang-tools-extra/clang-tidy/readability/RedundantTypenameCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/RedundantTypenameCheck.cpp @@ -47,6 +47,9 @@ void RedundantTypenameCheck::check(const MatchFinder::MatchResult &Result) { const SourceLocation ElaboratedKeywordLoc = [&] { if (const auto *NonDependentTypeLoc = Result.Nodes.getNodeAs("nonDependentTypeLoc")) { + if (NonDependentTypeLoc->getType()->isDependentType()) + return SourceLocation(); + if (const auto TL = NonDependentTypeLoc->getAs()) return TL.getElaboratedKeywordLoc(); @@ -59,8 +62,7 @@ void RedundantTypenameCheck::check(const MatchFinder::MatchResult &Result) { if (const auto TL = NonDependentTypeLoc->getAs()) - if (!TL.getType()->isDependentType()) - return TL.getElaboratedKeywordLoc(); + return TL.getElaboratedKeywordLoc(); } else { TypeLoc InnermostTypeLoc = *Result.Nodes.getNodeAs("dependentTypeLoc"); diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-typename.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-typename.cpp index 2efafd1a9a64..e8fcd9bcd573 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-typename.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-typename.cpp @@ -267,3 +267,27 @@ WHOLE_TYPE_IN_MACRO Macro2; #define WHOLE_DECLARATION_IN_MACRO typename NotDependent::R Macro3 WHOLE_DECLARATION_IN_MACRO; + +template struct Wrapper {}; +template +struct ClassWrapper { + using R = T; + Wrapper f(); +}; + +template +Wrapper::R> ClassWrapper::f() { + return {}; +} + +template struct StructWrapper {}; +template +class ClassWithNestedStruct { + struct Nested {}; + StructWrapper f(); +}; + +template +StructWrapper::Nested> ClassWithNestedStruct::f() { + return {}; +}