Skip to content

Commit c10eeea

Browse files
committed
Use duck-typing to get pair type
1 parent 5a3799e commit c10eeea

File tree

5 files changed

+55
-59
lines changed

5 files changed

+55
-59
lines changed

clang-tools-extra/clang-tidy/modernize/UseStructuredBindingCheck.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88

99
#include "UseStructuredBindingCheck.h"
1010
#include "../utils/DeclRefExprUtils.h"
11-
#include "../utils/OptionsUtils.h"
1211
#include "clang/Lex/Lexer.h"
1312

1413
using namespace clang::ast_matchers;
1514

1615
namespace clang::tidy::modernize {
1716

18-
static constexpr const char *DefaultPairTypes = "std::pair";
1917
static constexpr llvm::StringLiteral PairDeclName = "PairVarD";
2018
static constexpr llvm::StringLiteral PairVarTypeName = "PairVarType";
2119
static constexpr llvm::StringLiteral FirstVarDeclName = "FirstVarDecl";
@@ -178,16 +176,15 @@ AST_MATCHER_P(Expr, ignoringCopyCtorAndImplicitCast,
178176
return InnerMatcher.matches(*Node.IgnoreImpCasts(), Finder, Builder);
179177
}
180178

181-
} // namespace
182-
183-
UseStructuredBindingCheck::UseStructuredBindingCheck(StringRef Name,
184-
ClangTidyContext *Context)
185-
: ClangTidyCheck(Name, Context),
186-
PairTypes(utils::options::parseStringList(
187-
Options.get("PairTypes", DefaultPairTypes))) {
188-
;
179+
AST_MATCHER(CXXRecordDecl, isPairType) {
180+
return llvm::all_of(Node.fields(), [](const FieldDecl *FD) {
181+
return FD->getAccess() == AS_public &&
182+
(FD->getName() == "first" || FD->getName() == "second");
183+
});
189184
}
190185

186+
} // namespace
187+
191188
static auto getVarInitWithMemberMatcher(
192189
StringRef PairName, StringRef MemberName, StringRef TypeName,
193190
StringRef BindingName,
@@ -204,10 +201,9 @@ static auto getVarInitWithMemberMatcher(
204201
}
205202

206203
void UseStructuredBindingCheck::registerMatchers(MatchFinder *Finder) {
207-
auto PairType =
208-
qualType(unless(isVolatileQualified()),
209-
hasUnqualifiedDesugaredType(recordType(
210-
hasDeclaration(cxxRecordDecl(hasAnyName(PairTypes))))));
204+
auto PairType = qualType(unless(isVolatileQualified()),
205+
hasUnqualifiedDesugaredType(recordType(
206+
hasDeclaration(cxxRecordDecl(isPairType())))));
211207

212208
auto UnlessShouldBeIgnored =
213209
unless(anyOf(hasAnySpecifiersShouldBeIgnored(), isInMarco()));

clang-tools-extra/clang-tidy/modernize/UseStructuredBindingCheck.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,13 @@ namespace clang::tidy::modernize {
2020
/// http://clang.llvm.org/extra/clang-tidy/checks/modernize/use-structured-binding.html
2121
class UseStructuredBindingCheck : public ClangTidyCheck {
2222
public:
23-
UseStructuredBindingCheck(StringRef Name, ClangTidyContext *Context);
23+
UseStructuredBindingCheck(StringRef Name, ClangTidyContext *Context)
24+
: ClangTidyCheck(Name, Context) {}
2425
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
2526
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
2627
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
2728
return LangOpts.CPlusPlus17;
2829
}
29-
30-
private:
31-
const std::vector<StringRef> PairTypes;
3230
};
3331

3432
} // namespace clang::tidy::modernize

clang-tools-extra/docs/clang-tidy/checks/modernize/use-structured-binding.rst

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,3 @@ and:
8484
if (results.second) {
8585
handle_inserted(results.first);
8686
}
87-
88-
Options
89-
-------
90-
91-
.. option:: PairTypes
92-
93-
A semicolon-separated list of type names to be treated as pair-like for
94-
structured binding suggestions. Example: `MyPairType;OtherPairType`.
95-
Default is `std::pair`.

clang-tools-extra/test/clang-tidy/checkers/modernize/use-structured-binding-custom.cpp

Lines changed: 0 additions & 32 deletions
This file was deleted.

clang-tools-extra/test/clang-tidy/checkers/modernize/use-structured-binding.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,3 +388,46 @@ void stdTieNotWarn() {
388388
x = 1;
389389
};
390390
}
391+
392+
struct otherPair {
393+
int first;
394+
int second;
395+
};
396+
397+
void OtherPairTest() {
398+
{
399+
auto P = otherPair();
400+
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: use a structured binding to decompose a pair [modernize-use-structured-binding]
401+
// CHECK-FIXES-ALL: auto [x, y] = otherPair();
402+
int x = P.first;
403+
int y = P.second;
404+
}
405+
}
406+
407+
struct otherNonPair1 {
408+
int first;
409+
int second;
410+
411+
private:
412+
int third;
413+
};
414+
415+
struct otherNonPair2 {
416+
int first;
417+
int second;
418+
int third;
419+
};
420+
421+
void OtherNonPairTest() {
422+
{
423+
auto P = otherNonPair1();
424+
int x = P.first;
425+
int y = P.second;
426+
}
427+
428+
{
429+
auto P = otherNonPair2();
430+
int x = P.first;
431+
int y = P.second;
432+
}
433+
}

0 commit comments

Comments
 (0)