Skip to content

Commit fb99a85

Browse files
authored
[clang-tidy][NFC] Refactor modernize-use-trailing-return-type-check check code and tests (#140759)
- Deleted unused includes - Deleted useless braces - Converted private methods to static function to improve compilations speed and readability - Modernized tests to use `cxx-or-later`
1 parent e373f7a commit fb99a85

File tree

3 files changed

+32
-49
lines changed

3 files changed

+32
-49
lines changed

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

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ struct UnqualNameVisitor : public RecursiveASTVisitor<UnqualNameVisitor> {
113113
};
114114
} // namespace
115115

116-
constexpr llvm::StringLiteral Message =
116+
constexpr llvm::StringLiteral ErrorMessageOnFunction =
117117
"use a trailing return type for this function";
118118

119119
static SourceLocation expandIfMacroId(SourceLocation Loc,
@@ -125,7 +125,7 @@ static SourceLocation expandIfMacroId(SourceLocation Loc,
125125
return Loc;
126126
}
127127

128-
SourceLocation UseTrailingReturnTypeCheck::findTrailingReturnTypeSourceLocation(
128+
static SourceLocation findTrailingReturnTypeSourceLocation(
129129
const FunctionDecl &F, const FunctionTypeLoc &FTL, const ASTContext &Ctx,
130130
const SourceManager &SM, const LangOptions &LangOpts) {
131131
// We start with the location of the closing parenthesis.
@@ -217,10 +217,11 @@ classifyToken(const FunctionDecl &F, Preprocessor &PP, Token Tok) {
217217
return CT;
218218
}
219219

220-
std::optional<SmallVector<ClassifiedToken, 8>>
221-
UseTrailingReturnTypeCheck::classifyTokensBeforeFunctionName(
222-
const FunctionDecl &F, const ASTContext &Ctx, const SourceManager &SM,
223-
const LangOptions &LangOpts) {
220+
static std::optional<SmallVector<ClassifiedToken, 8>>
221+
classifyTokensBeforeFunctionName(const FunctionDecl &F, const ASTContext &Ctx,
222+
const SourceManager &SM,
223+
const LangOptions &LangOpts,
224+
Preprocessor *PP) {
224225
SourceLocation BeginF = expandIfMacroId(F.getBeginLoc(), SM);
225226
SourceLocation BeginNameF = expandIfMacroId(F.getLocation(), SM);
226227

@@ -242,7 +243,6 @@ UseTrailingReturnTypeCheck::classifyTokensBeforeFunctionName(
242243
const MacroInfo *MI = PP->getMacroInfo(&Info);
243244
if (!MI || MI->isFunctionLike()) {
244245
// Cannot handle function style macros.
245-
diag(F.getLocation(), Message);
246246
return std::nullopt;
247247
}
248248
}
@@ -253,10 +253,8 @@ UseTrailingReturnTypeCheck::classifyTokensBeforeFunctionName(
253253

254254
if (std::optional<ClassifiedToken> CT = classifyToken(F, *PP, T))
255255
ClassifiedTokens.push_back(*CT);
256-
else {
257-
diag(F.getLocation(), Message);
256+
else
258257
return std::nullopt;
259-
}
260258
}
261259

262260
return ClassifiedTokens;
@@ -273,17 +271,17 @@ static bool hasAnyNestedLocalQualifiers(QualType Type) {
273271
return Result;
274272
}
275273

276-
SourceRange UseTrailingReturnTypeCheck::findReturnTypeAndCVSourceRange(
277-
const FunctionDecl &F, const TypeLoc &ReturnLoc, const ASTContext &Ctx,
278-
const SourceManager &SM, const LangOptions &LangOpts) {
274+
static SourceRange
275+
findReturnTypeAndCVSourceRange(const FunctionDecl &F, const TypeLoc &ReturnLoc,
276+
const ASTContext &Ctx, const SourceManager &SM,
277+
const LangOptions &LangOpts, Preprocessor *PP) {
279278

280279
// We start with the range of the return type and expand to neighboring
281280
// qualifiers (const, volatile and restrict).
282281
SourceRange ReturnTypeRange = F.getReturnTypeSourceRange();
283282
if (ReturnTypeRange.isInvalid()) {
284283
// Happens if e.g. clang cannot resolve all includes and the return type is
285284
// unknown.
286-
diag(F.getLocation(), Message);
287285
return {};
288286
}
289287

@@ -294,7 +292,7 @@ SourceRange UseTrailingReturnTypeCheck::findReturnTypeAndCVSourceRange(
294292

295293
// Include qualifiers to the left and right of the return type.
296294
std::optional<SmallVector<ClassifiedToken, 8>> MaybeTokens =
297-
classifyTokensBeforeFunctionName(F, Ctx, SM, LangOpts);
295+
classifyTokensBeforeFunctionName(F, Ctx, SM, LangOpts, PP);
298296
if (!MaybeTokens)
299297
return {};
300298
const SmallVector<ClassifiedToken, 8> &Tokens = *MaybeTokens;
@@ -331,10 +329,11 @@ SourceRange UseTrailingReturnTypeCheck::findReturnTypeAndCVSourceRange(
331329
return ReturnTypeRange;
332330
}
333331

334-
void UseTrailingReturnTypeCheck::keepSpecifiers(
335-
std::string &ReturnType, std::string &Auto, SourceRange ReturnTypeCVRange,
336-
const FunctionDecl &F, const FriendDecl *Fr, const ASTContext &Ctx,
337-
const SourceManager &SM, const LangOptions &LangOpts) {
332+
static void keepSpecifiers(std::string &ReturnType, std::string &Auto,
333+
SourceRange ReturnTypeCVRange, const FunctionDecl &F,
334+
const FriendDecl *Fr, const ASTContext &Ctx,
335+
const SourceManager &SM, const LangOptions &LangOpts,
336+
Preprocessor *PP) {
338337
// Check if there are specifiers inside the return type. E.g. unsigned
339338
// inline int.
340339
const auto *M = dyn_cast<CXXMethodDecl>(&F);
@@ -346,7 +345,7 @@ void UseTrailingReturnTypeCheck::keepSpecifiers(
346345
// Tokenize return type. If it contains macros which contain a mix of
347346
// qualifiers, specifiers and types, give up.
348347
std::optional<SmallVector<ClassifiedToken, 8>> MaybeTokens =
349-
classifyTokensBeforeFunctionName(F, Ctx, SM, LangOpts);
348+
classifyTokensBeforeFunctionName(F, Ctx, SM, LangOpts, PP);
350349
if (!MaybeTokens)
351350
return;
352351

@@ -423,7 +422,7 @@ void UseTrailingReturnTypeCheck::check(const MatchFinder::MatchResult &Result) {
423422
if (F->getDeclaredReturnType()->isFunctionPointerType() ||
424423
F->getDeclaredReturnType()->isMemberFunctionPointerType() ||
425424
F->getDeclaredReturnType()->isMemberPointerType()) {
426-
diag(F->getLocation(), Message);
425+
diag(F->getLocation(), ErrorMessageOnFunction);
427426
return;
428427
}
429428

@@ -440,24 +439,26 @@ void UseTrailingReturnTypeCheck::check(const MatchFinder::MatchResult &Result) {
440439
// FIXME: This may happen if we have __attribute__((...)) on the function.
441440
// We abort for now. Remove this when the function type location gets
442441
// available in clang.
443-
diag(F->getLocation(), Message);
442+
diag(F->getLocation(), ErrorMessageOnFunction);
444443
return;
445444
}
446445

447446
SourceLocation InsertionLoc =
448447
findTrailingReturnTypeSourceLocation(*F, FTL, Ctx, SM, LangOpts);
449448
if (InsertionLoc.isInvalid()) {
450-
diag(F->getLocation(), Message);
449+
diag(F->getLocation(), ErrorMessageOnFunction);
451450
return;
452451
}
453452

454453
// Using the declared return type via F->getDeclaredReturnType().getAsString()
455454
// discards user formatting and order of const, volatile, type, whitespace,
456455
// space before & ... .
457-
SourceRange ReturnTypeCVRange =
458-
findReturnTypeAndCVSourceRange(*F, FTL.getReturnLoc(), Ctx, SM, LangOpts);
459-
if (ReturnTypeCVRange.isInvalid())
456+
SourceRange ReturnTypeCVRange = findReturnTypeAndCVSourceRange(
457+
*F, FTL.getReturnLoc(), Ctx, SM, LangOpts, PP);
458+
if (ReturnTypeCVRange.isInvalid()) {
459+
diag(F->getLocation(), ErrorMessageOnFunction);
460460
return;
461+
}
461462

462463
// Check if unqualified names in the return type conflict with other entities
463464
// after the rewrite.
@@ -470,7 +471,7 @@ void UseTrailingReturnTypeCheck::check(const MatchFinder::MatchResult &Result) {
470471
UnqualNameVisitor UNV{*F};
471472
UNV.TraverseTypeLoc(FTL.getReturnLoc());
472473
if (UNV.Collision) {
473-
diag(F->getLocation(), Message);
474+
diag(F->getLocation(), ErrorMessageOnFunction);
474475
return;
475476
}
476477

@@ -486,10 +487,10 @@ void UseTrailingReturnTypeCheck::check(const MatchFinder::MatchResult &Result) {
486487
std::string Auto = NeedSpaceAfterAuto ? "auto " : "auto";
487488
std::string ReturnType =
488489
std::string(tooling::fixit::getText(ReturnTypeCVRange, Ctx));
489-
keepSpecifiers(ReturnType, Auto, ReturnTypeCVRange, *F, Fr, Ctx, SM,
490-
LangOpts);
490+
keepSpecifiers(ReturnType, Auto, ReturnTypeCVRange, *F, Fr, Ctx, SM, LangOpts,
491+
PP);
491492

492-
diag(F->getLocation(), Message)
493+
diag(F->getLocation(), ErrorMessageOnFunction)
493494
<< FixItHint::CreateReplacement(ReturnTypeCVRange, Auto)
494495
<< FixItHint::CreateInsertion(InsertionLoc, " -> " + ReturnType);
495496
}

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
#include "../ClangTidyCheck.h"
1313
#include "clang/Lex/Token.h"
14-
#include <optional>
1514

1615
namespace clang::tidy::modernize {
1716

@@ -39,23 +38,6 @@ class UseTrailingReturnTypeCheck : public ClangTidyCheck {
3938

4039
private:
4140
Preprocessor *PP = nullptr;
42-
43-
SourceLocation findTrailingReturnTypeSourceLocation(
44-
const FunctionDecl &F, const FunctionTypeLoc &FTL, const ASTContext &Ctx,
45-
const SourceManager &SM, const LangOptions &LangOpts);
46-
std::optional<SmallVector<ClassifiedToken, 8>>
47-
classifyTokensBeforeFunctionName(const FunctionDecl &F, const ASTContext &Ctx,
48-
const SourceManager &SM,
49-
const LangOptions &LangOpts);
50-
SourceRange findReturnTypeAndCVSourceRange(const FunctionDecl &F,
51-
const TypeLoc &ReturnLoc,
52-
const ASTContext &Ctx,
53-
const SourceManager &SM,
54-
const LangOptions &LangOpts);
55-
void keepSpecifiers(std::string &ReturnType, std::string &Auto,
56-
SourceRange ReturnTypeCVRange, const FunctionDecl &F,
57-
const FriendDecl *Fr, const ASTContext &Ctx,
58-
const SourceManager &SM, const LangOptions &LangOpts);
5941
};
6042

6143
} // namespace clang::tidy::modernize

clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type-cxx20.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %check_clang_tidy -std=c++20 %s modernize-use-trailing-return-type %t
1+
// RUN: %check_clang_tidy -std=c++20-or-later %s modernize-use-trailing-return-type %t
22

33
namespace std {
44
template <typename T, typename U>

0 commit comments

Comments
 (0)