Skip to content

Commit 7e2aaa4

Browse files
committed
feed
1 parent 4a9fc6e commit 7e2aaa4

File tree

5 files changed

+23
-25
lines changed

5 files changed

+23
-25
lines changed

clang-tools-extra/clang-tidy/readability/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ add_clang_library(clangTidyReadabilityModule STATIC
5757
UniqueptrDeleteReleaseCheck.cpp
5858
UppercaseLiteralSuffixCheck.cpp
5959
UseAnyOfAllOfCheck.cpp
60-
UseStdMinMaxCheck.cpp
6160
UseSpanFirstLastCheck.cpp
61+
UseStdMinMaxCheck.cpp
6262

6363
LINK_LIBS
6464
clangTidy

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,10 @@ class ReadabilityModule : public ClangTidyModule {
171171
"readability-uppercase-literal-suffix");
172172
CheckFactories.registerCheck<UseAnyOfAllOfCheck>(
173173
"readability-use-anyofallof");
174-
CheckFactories.registerCheck<UseStdMinMaxCheck>(
175-
"readability-use-std-min-max");
176174
CheckFactories.registerCheck<UseSpanFirstLastCheck>(
177175
"readability-use-span-first-last");
176+
CheckFactories.registerCheck<UseStdMinMaxCheck>(
177+
"readability-use-std-min-max");
178178
}
179179
};
180180

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

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ using namespace clang::ast_matchers;
1717
namespace clang::tidy::readability {
1818

1919
void UseSpanFirstLastCheck::registerMatchers(MatchFinder *Finder) {
20-
if (!getLangOpts().CPlusPlus20)
21-
return;
22-
2320
// Match span::subspan calls
2421
const auto HasSpanType =
2522
hasType(hasUnqualifiedDesugaredType(recordType(hasDeclaration(
@@ -53,56 +50,54 @@ void UseSpanFirstLastCheck::handleSubspanCall(
5350
// Check if this is subspan(0, n) -> first(n)
5451
bool IsZeroOffset = false;
5552
const Expr *OffsetE = Offset->IgnoreImpCasts();
56-
if (const auto *IL = dyn_cast<IntegerLiteral>(OffsetE)) {
53+
if (const auto *IL = dyn_cast<IntegerLiteral>(OffsetE))
5754
IsZeroOffset = IL->getValue() == 0;
58-
}
5955

6056
// Check if this is subspan(size() - n) -> last(n)
6157
bool IsSizeMinusN = false;
6258
const Expr *SizeMinusArg = nullptr;
63-
if (const auto *BO = dyn_cast<BinaryOperator>(OffsetE)) {
64-
if (BO->getOpcode() == BO_Sub) {
65-
if (const auto *SizeCall = dyn_cast<CXXMemberCallExpr>(BO->getLHS())) {
66-
if (SizeCall->getMethodDecl()->getName() == "size") {
67-
IsSizeMinusN = true;
68-
SizeMinusArg = BO->getRHS();
69-
}
70-
}
59+
60+
const auto *BO = dyn_cast<BinaryOperator>(OffsetE);
61+
if (BO && BO->getOpcode() == BO_Sub) {
62+
const auto *SizeCall = dyn_cast<CXXMemberCallExpr>(BO->getLHS());
63+
if (SizeCall && SizeCall->getMethodDecl()->getName() == "size") {
64+
IsSizeMinusN = true;
65+
SizeMinusArg = BO->getRHS();
7166
}
7267
}
7368

7469
// Build replacement text
7570
std::string Replacement;
7671
if (IsZeroOffset && Count) {
7772
// subspan(0, count) -> first(count)
78-
auto CountStr = Lexer::getSourceText(
73+
const auto CountStr = Lexer::getSourceText(
7974
CharSourceRange::getTokenRange(Count->getSourceRange()),
8075
Context.getSourceManager(), Context.getLangOpts());
8176
const auto *Base =
8277
cast<CXXMemberCallExpr>(Call)->getImplicitObjectArgument();
83-
auto BaseStr = Lexer::getSourceText(
78+
const StringRef BaseStr = Lexer::getSourceText(
8479
CharSourceRange::getTokenRange(Base->getSourceRange()),
8580
Context.getSourceManager(), Context.getLangOpts());
8681
Replacement = BaseStr.str() + ".first(" + CountStr.str() + ")";
8782
} else if (IsSizeMinusN && SizeMinusArg) {
8883
// subspan(size() - n) -> last(n)
89-
auto ArgStr = Lexer::getSourceText(
84+
const StringRef ArgStr = Lexer::getSourceText(
9085
CharSourceRange::getTokenRange(SizeMinusArg->getSourceRange()),
9186
Context.getSourceManager(), Context.getLangOpts());
9287
const auto *Base =
9388
cast<CXXMemberCallExpr>(Call)->getImplicitObjectArgument();
94-
auto BaseStr = Lexer::getSourceText(
89+
const StringRef BaseStr = Lexer::getSourceText(
9590
CharSourceRange::getTokenRange(Base->getSourceRange()),
9691
Context.getSourceManager(), Context.getLangOpts());
9792
Replacement = BaseStr.str() + ".last(" + ArgStr.str() + ")";
9893
}
9994

10095
if (!Replacement.empty()) {
10196
if (IsZeroOffset && Count) {
102-
diag(Call->getBeginLoc(), "prefer span::first() over subspan()")
97+
diag(Call->getBeginLoc(), "prefer 'span::first()' over 'subspan()'")
10398
<< FixItHint::CreateReplacement(Call->getSourceRange(), Replacement);
10499
} else {
105-
diag(Call->getBeginLoc(), "prefer span::last() over subspan()")
100+
diag(Call->getBeginLoc(), "prefer 'span::last()' over 'subspan()'")
106101
<< FixItHint::CreateReplacement(Call->getSourceRange(), Replacement);
107102
}
108103
}

clang-tools-extra/clang-tidy/readability/UseSpanFirstLastCheck.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class UseSpanFirstLastCheck : public ClangTidyCheck {
3333

3434
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
3535
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
36+
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
37+
return LangOpts.CPlusPlus20;
38+
}
3639

3740
private:
3841
void handleSubspanCall(const ast_matchers::MatchFinder::MatchResult &Result,

clang-tools-extra/test/clang-tidy/checkers/readability/use-span-first-last.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ void test() {
3434
std::span<int> s(arr, 5);
3535

3636
auto sub1 = s.subspan(0, 3);
37-
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: prefer span::first() over subspan()
37+
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: prefer 'span::first()' over 'subspan()'
3838
// CHECK-FIXES: auto sub1 = s.first(3);
3939

4040
auto sub2 = s.subspan(s.size() - 2);
41-
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: prefer span::last() over subspan()
41+
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: prefer 'span::last()' over 'subspan()'
4242
// CHECK-FIXES: auto sub2 = s.last(2);
4343

4444
__SIZE_TYPE__ n = 2;
4545
auto sub3 = s.subspan(0, n);
46-
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: prefer span::first() over subspan()
46+
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: prefer 'span::first()' over 'subspan()'
4747
// CHECK-FIXES: auto sub3 = s.first(n);
4848

4949
auto sub4 = s.subspan(1, 2); // No warning

0 commit comments

Comments
 (0)