Skip to content

Commit cd2fce3

Browse files
committed
feedback
1 parent c707971 commit cd2fce3

File tree

4 files changed

+40
-17
lines changed

4 files changed

+40
-17
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
namespace 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

4040
private:
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

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff 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.

clang-tools-extra/docs/clang-tidy/checks/readability/use-span-first-last.rst

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ to C++20 to provide more expressive alternatives to common subspan operations.
99

1010
Covered 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

1920
Non-zero offset with count (like ``subspan(1, n)``) or offset-only calls
2021
(like ``subspan(n)``) have no clearer equivalent using ``first()`` or

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff 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+
}

0 commit comments

Comments
 (0)