-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[clang-tidy] Add new check: readability-redundant-typename
#161574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
localspook
wants to merge
19
commits into
llvm:main
Choose a base branch
from
localspook:readability-redundant-typename
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
82c842a
[clang-tidy] Add new check: `readability-redundant-typename`
localspook 7353f87
Merge branch 'main' into readability-redundant-typename
localspook 4bae981
Move matcher out of lambda
localspook 8f0118a
Use different names for bound nodes
localspook 21d3d44
Use `isInvalid`
localspook 7e100c3
Update clang-tools-extra/clang-tidy/readability/RedundantTypenameCheck.h
localspook 3e362cd
Add C++98 tests, guard variable template behind C++14
localspook b7a23d9
fix false negative with variable templates
localspook fb68c3c
fix false negative with variable templates: take two
localspook 8f33b21
test fixes, move simple matchers into `ASTMatchers.h`
localspook 99a7c14
reduce duplication in matchers
localspook 2309f33
Remove stray newline
localspook 41b823c
Add tests with pack expansions
localspook 893b3ae
Fix false negative and false positive
localspook e6944fa
Add test case
localspook e897eb7
Test C++03
localspook efb594a
Adjust matcher usage examples
localspook 809c8a4
unnecessary -> redundant
localspook beb67d4
Fix typo
localspook File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
77 changes: 77 additions & 0 deletions
77
clang-tools-extra/clang-tidy/readability/RedundantTypenameCheck.cpp
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "RedundantTypenameCheck.h" | ||
#include "clang/AST/TypeLoc.h" | ||
#include "clang/ASTMatchers/ASTMatchFinder.h" | ||
#include "clang/ASTMatchers/ASTMatchers.h" | ||
#include "clang/Basic/Diagnostic.h" | ||
#include "clang/Lex/Lexer.h" | ||
#include "clang/Sema/DeclSpec.h" | ||
|
||
using namespace clang::ast_matchers; | ||
using namespace clang::ast_matchers::internal; | ||
|
||
namespace clang::tidy::readability { | ||
|
||
void RedundantTypenameCheck::registerMatchers(MatchFinder *Finder) { | ||
Finder->addMatcher(typedefTypeLoc().bind("typedefTypeLoc"), this); | ||
|
||
if (!getLangOpts().CPlusPlus20) | ||
return; | ||
|
||
const auto InImplicitTypenameContext = anyOf( | ||
hasParent(decl(anyOf( | ||
typedefNameDecl(), templateTypeParmDecl(), nonTypeTemplateParmDecl(), | ||
friendDecl(), fieldDecl(), | ||
varDecl(hasDeclContext(anyOf(namespaceDecl(), translationUnitDecl())), | ||
unless(parmVarDecl())), | ||
parmVarDecl(hasParent(expr(requiresExpr()))), | ||
parmVarDecl(hasParent(typeLoc(hasParent( | ||
decl(anyOf(cxxMethodDecl(), hasParent(friendDecl()), | ||
functionDecl(has(nestedNameSpecifier())))))))), | ||
// Match return types. | ||
functionDecl(unless(cxxConversionDecl()))))), | ||
hasParent(expr(anyOf(cxxNamedCastExpr(), cxxNewExpr())))); | ||
Finder->addMatcher(typeLoc(InImplicitTypenameContext).bind("typeloc"), this); | ||
} | ||
|
||
void RedundantTypenameCheck::check(const MatchFinder::MatchResult &Result) { | ||
const SourceLocation ElaboratedKeywordLoc = [&] { | ||
if (const auto *TTL = | ||
Result.Nodes.getNodeAs<TypedefTypeLoc>("typedefTypeLoc")) | ||
return TTL->getElaboratedKeywordLoc(); | ||
|
||
TypeLoc InnermostTypeLoc = *Result.Nodes.getNodeAs<TypeLoc>("typeloc"); | ||
while (const TypeLoc Next = InnermostTypeLoc.getNextTypeLoc()) | ||
InnermostTypeLoc = Next; | ||
|
||
if (const auto DNTL = InnermostTypeLoc.getAs<DependentNameTypeLoc>()) | ||
return DNTL.getElaboratedKeywordLoc(); | ||
|
||
if (const auto TSTL = | ||
InnermostTypeLoc.getAs<TemplateSpecializationTypeLoc>()) | ||
return TSTL.getElaboratedKeywordLoc(); | ||
|
||
return SourceLocation(); | ||
}(); | ||
|
||
if (ElaboratedKeywordLoc.isInvalid()) | ||
return; | ||
|
||
if (Token ElaboratedKeyword; | ||
Lexer::getRawToken(ElaboratedKeywordLoc, ElaboratedKeyword, | ||
*Result.SourceManager, getLangOpts()) || | ||
ElaboratedKeyword.getRawIdentifier() != "typename") | ||
return; | ||
|
||
diag(ElaboratedKeywordLoc, "redundant 'typename'") | ||
<< FixItHint::CreateRemoval(ElaboratedKeywordLoc); | ||
} | ||
|
||
} // namespace clang::tidy::readability |
36 changes: 36 additions & 0 deletions
36
clang-tools-extra/clang-tidy/readability/RedundantTypenameCheck.h
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_REDUNDANTTYPENAMECHECK_H | ||
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_REDUNDANTTYPENAMECHECK_H | ||
|
||
#include "../ClangTidyCheck.h" | ||
|
||
namespace clang::tidy::readability { | ||
|
||
/// Finds redundant uses of the `typename` keyword. | ||
/// | ||
/// For the user-facing documentation see: | ||
/// https://clang.llvm.org/extra/clang-tidy/checks/readability/redundant-typename.html | ||
class RedundantTypenameCheck : public ClangTidyCheck { | ||
public: | ||
RedundantTypenameCheck(StringRef Name, ClangTidyContext *Context) | ||
: ClangTidyCheck(Name, Context) {} | ||
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { | ||
return LangOpts.CPlusPlus; | ||
vbvictor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
void registerMatchers(ast_matchers::MatchFinder *Finder) override; | ||
void check(const ast_matchers::MatchFinder::MatchResult &Result) override; | ||
std::optional<TraversalKind> getCheckTraversalKind() const override { | ||
return TK_IgnoreUnlessSpelledInSource; | ||
} | ||
}; | ||
|
||
} // namespace clang::tidy::readability | ||
|
||
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_REDUNDANTTYPENAMECHECK_H |
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
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
26 changes: 26 additions & 0 deletions
26
clang-tools-extra/docs/clang-tidy/checks/readability/redundant-typename.rst
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
.. title:: clang-tidy - readability-redundant-typename | ||
|
||
readability-redundant-typename | ||
============================== | ||
|
||
Finds redundant uses of the ``typename`` keyword. | ||
|
||
``typename`` is redundant in two cases. First, before non-dependent names: | ||
|
||
.. code-block:: c++ | ||
|
||
/* typename */ std::vector<int>::size_type size; | ||
|
||
And second, since C++20, before dependent names that appear in a context | ||
where only a type is allowed (the following example shows just a few of them): | ||
|
||
.. code-block:: c++ | ||
|
||
template <typename T> | ||
using trait = /* typename */ T::type; | ||
|
||
template <typename T> | ||
struct S { | ||
/* typename */ T::type variable; | ||
/* typename */ T::type function(/* typename */ T::type); | ||
}; | ||
vbvictor marked this conversation as resolved.
Show resolved
Hide resolved
|
19 changes: 19 additions & 0 deletions
19
clang-tools-extra/test/clang-tidy/checkers/readability/redundant-typename-cxx98.cpp
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// RUN: %check_clang_tidy -std=c++98,c++03 %s readability-redundant-typename %t \ | ||
// RUN: -- -- -fno-delayed-template-parsing | ||
|
||
struct NotDependent { | ||
typedef int R; | ||
}; | ||
|
||
template <typename T> | ||
typename T::R f() { | ||
static_cast<typename T::R>(0); | ||
|
||
typename NotDependent::R NotDependentVar; | ||
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: redundant 'typename' [readability-redundant-typename] | ||
// CHECK-FIXES: NotDependent::R NotDependentVar; | ||
|
||
void notDependentFunctionDeclaration(typename NotDependent::R); | ||
// CHECK-MESSAGES: :[[@LINE-1]]:40: warning: redundant 'typename' [readability-redundant-typename] | ||
// CHECK-FIXES: void notDependentFunctionDeclaration(NotDependent::R); | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.