File tree Expand file tree Collapse file tree 4 files changed +40
-17
lines changed Expand file tree Collapse file tree 4 files changed +40
-17
lines changed Original file line number Diff line number Diff line change 1313
1414namespace clang ::tidy::readability {
1515
16- // / Suggests using clearer std::span member functions first()/ last() instead of
17- // / equivalent subspan() calls where applicable.
16+ // / Suggests using clearer ' std::span' member functions ' first()'/' last()'
17+ // / instead of equivalent ' subspan()' calls where applicable.
1818// /
1919// / For example:
2020// / \code
@@ -33,9 +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- }
36+ bool isLanguageVersionSupported (const LangOptions &LangOpts) const override {
37+ return LangOpts.CPlusPlus20 ;
38+ }
3939
4040private:
4141 void handleSubspanCall (const ast_matchers::MatchFinder::MatchResult &Result,
@@ -44,4 +44,4 @@ class UseSpanFirstLastCheck : public ClangTidyCheck {
4444
4545} // namespace clang::tidy::readability
4646
47- #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_USESPANFIRSTLASTCHECK_H
47+ #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_USESPANFIRSTLASTCHECK_H
Original file line number Diff line number Diff line change @@ -142,13 +142,15 @@ New checks
142142 Finds cases when an uninstantiated virtual member function in a template class
143143 causes cross-compiler incompatibility.
144144
145- New check aliases
146- ^^^^^^^^^^^^^^^^^
145+ - New :doc: ` readability-use-span-first-last
146+ <clang-tidy/checks/readability/readability-use-span-first-last>` check.
147147
148- - New check `readability-use-span-first-last ` has been added that suggests using
149- ``std::span::first() `` and ``std::span::last() `` member functions instead of
148+ Suggests using ``std::span::first() `` and ``std::span::last() `` member functions instead of
150149 equivalent ``subspan() ``.
151150
151+ New check aliases
152+ ^^^^^^^^^^^^^^^^^
153+
152154- New alias :doc: `cert-arr39-c <clang-tidy/checks/cert/arr39-c >` to
153155 :doc: `bugprone-sizeof-expression
154156 <clang-tidy/checks/bugprone/sizeof-expression>` was added.
Original file line number Diff line number Diff line change @@ -9,12 +9,13 @@ to C++20 to provide more expressive alternatives to common subspan operations.
99
1010Covered scenarios:
1111
12- ==================================== ==================================
13- Expression Replacement
14- ------------------------------------ ----------------------------------
15- ``s.subspan(0, n) `` ``s.first(n) ``
16- ``s.subspan(s.size() - n) `` ``s.last(n) ``
17- ==================================== ==================================
12+ ========================== ====================
13+ Expression Replacement
14+ ------------------------- --------------------
15+ ``s.subspan(0, n) `` ``s.first(n) ``
16+ ``s.subspan(s.size() - n) `` ``s.last(n) ``
17+ ========================== ====================
18+
1819
1920Non-zero offset with count (like ``subspan(1, n) ``) or offset-only calls
2021(like ``subspan(n) ``) have no clearer equivalent using ``first() `` or
Original file line number Diff line number Diff line change @@ -48,4 +48,24 @@ void test() {
4848
4949 auto sub4 = s.subspan (1 , 2 ); // No warning
5050 auto sub5 = s.subspan (2 ); // No warning
51- }
51+
52+
53+ #define ZERO 0
54+ #define TWO 2
55+ #define SIZE_MINUS (s, n ) s.size() - n
56+ #define MAKE_SUBSPAN (obj, n ) obj.subspan(0 , n)
57+ #define MAKE_LAST_N (obj, n ) obj.subspan(obj.size() - n)
58+
59+ auto sub6 = s.subspan (SIZE_MINUS (s, 2 ));
60+ // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: prefer 'span::last()' over 'subspan()'
61+ // CHECK-FIXES: auto sub6 = s.last(2);
62+
63+ auto sub7 = MAKE_SUBSPAN (s, 3 );
64+ // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: prefer 'span::first()' over 'subspan()'
65+ // CHECK-FIXES: auto sub7 = s.first(3);
66+
67+ auto sub8 = MAKE_LAST_N (s, 2 );
68+ // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: prefer 'span::last()' over 'subspan()'
69+ // CHECK-FIXES: auto sub8 = s.last(2);
70+
71+ }
You can’t perform that action at this time.
0 commit comments