Skip to content

Commit 893b3ae

Browse files
committed
Fix false negative and false positive
1 parent 41b823c commit 893b3ae

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

clang-tools-extra/clang-tidy/readability/RedundantTypenameCheck.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "clang/ASTMatchers/ASTMatchFinder.h"
1212
#include "clang/ASTMatchers/ASTMatchers.h"
1313
#include "clang/Basic/Diagnostic.h"
14+
#include "clang/Lex/Lexer.h"
1415
#include "clang/Sema/DeclSpec.h"
1516

1617
using namespace clang::ast_matchers;
@@ -41,7 +42,7 @@ void RedundantTypenameCheck::registerMatchers(MatchFinder *Finder) {
4142
}
4243

4344
void RedundantTypenameCheck::check(const MatchFinder::MatchResult &Result) {
44-
const SourceLocation TypenameKeywordLoc = [&] {
45+
const SourceLocation ElaboratedKeywordLoc = [&] {
4546
if (const auto *TTL =
4647
Result.Nodes.getNodeAs<TypedefTypeLoc>("typedefTypeLoc"))
4748
return TTL->getElaboratedKeywordLoc();
@@ -53,14 +54,24 @@ void RedundantTypenameCheck::check(const MatchFinder::MatchResult &Result) {
5354
if (const auto DNTL = InnermostTypeLoc.getAs<DependentNameTypeLoc>())
5455
return DNTL.getElaboratedKeywordLoc();
5556

57+
if (const auto TSTL =
58+
InnermostTypeLoc.getAs<TemplateSpecializationTypeLoc>())
59+
return TSTL.getElaboratedKeywordLoc();
60+
5661
return SourceLocation();
5762
}();
5863

59-
if (TypenameKeywordLoc.isInvalid())
64+
if (ElaboratedKeywordLoc.isInvalid())
65+
return;
66+
67+
if (Token ElaboratedKeyword;
68+
Lexer::getRawToken(ElaboratedKeywordLoc, ElaboratedKeyword,
69+
*Result.SourceManager, getLangOpts()) ||
70+
ElaboratedKeyword.getRawIdentifier() != "typename")
6071
return;
6172

62-
diag(TypenameKeywordLoc, "redundant 'typename'")
63-
<< FixItHint::CreateRemoval(TypenameKeywordLoc);
73+
diag(ElaboratedKeywordLoc, "redundant 'typename'")
74+
<< FixItHint::CreateRemoval(ElaboratedKeywordLoc);
6475
}
6576

6677
} // namespace clang::tidy::readability

clang-tools-extra/test/clang-tidy/checkers/readability/redundant-typename.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ using trait = const typename T::R ****;
9898
// CHECK-MESSAGES-20: :[[@LINE-1]]:21: warning: redundant 'typename' [readability-redundant-typename]
9999
// CHECK-FIXES-20: using trait = const T::R ****;
100100

101+
template <typename T>
102+
using t = typename T::template R<T>;
103+
// CHECK-MESSAGES-20: :[[@LINE-1]]:11: warning: redundant 'typename' [readability-redundant-typename]
104+
// CHECK-FIXES-20: using t = T::template R<T>;
105+
101106
template <typename T>
102107
trait<typename T::R> m();
103108

@@ -197,6 +202,7 @@ class A {
197202
// CHECK-MESSAGES-20: :[[@LINE-1]]:17: warning: redundant 'typename' [readability-redundant-typename]
198203
// CHECK-FIXES-20: friend void k(T::R) {}
199204

205+
friend struct T::R;
200206
enum E1 : typename T::R {};
201207
enum class E2 : typename T::R {};
202208
operator typename T::R();

0 commit comments

Comments
 (0)