From ce2d44b0ab6da147931e9711e01d44a672ec3854 Mon Sep 17 00:00:00 2001 From: Jorge Pinto Sousa Date: Mon, 3 Jul 2023 16:39:31 +0000 Subject: [PATCH] [clang-tidy] Accessing checks not done for aliases of `std::array` Index accessing checks are not performed for aliases of `std::array`, as only `std::array` itself seems to be checked. This patch aims to extend it for aliases such as: `using MyArray = std::array;` Reviewed By: PiotrZSL Differential Revision: https://reviews.llvm.org/D154297 --- .../ProBoundsConstantArrayIndexCheck.cpp | 3 ++- clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++ .../pro-bounds-constant-array-index.cpp | 22 ++++++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp index da7e6a3..fbc3cd3 100644 --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp @@ -49,7 +49,8 @@ void ProBoundsConstantArrayIndexCheck::registerMatchers(MatchFinder *Finder) { cxxOperatorCallExpr( hasOverloadedOperatorName("[]"), hasArgument( - 0, hasType(cxxRecordDecl(hasName("::std::array")).bind("type"))), + 0, hasType(hasUnqualifiedDesugaredType(recordType(hasDeclaration( + cxxRecordDecl(hasName("::std::array")).bind("type")))))), hasArgument(1, expr().bind("index"))) .bind("expr"), this); diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 84333f6c..7c6efe6 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -321,6 +321,10 @@ Changes in existing checks :doc:`cppcoreguidelines-use-default-member-init ` instead. +- Improved :doc:`cppcoreguidelines-pro-bounds-constant-array-index + ` check + to cover type aliases of ``std::array``. + - Fixed a false positive in :doc:`cppcoreguidelines-slicing ` check when warning would be emitted in constructor for virtual base class initialization. diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-constant-array-index.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-constant-array-index.cpp index 91c7fc1..e689677 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-constant-array-index.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-constant-array-index.cpp @@ -44,6 +44,28 @@ void f(std::array a, int pos) { a[1] = 3; // OK, constant index and inside bounds a[9] = 3; // OK, constant index and inside bounds a[const_index(6)] = 3; // OK, constant index and inside bounds + + using MyArray = std::array; + MyArray m{}; + m [ pos / 2 /*comment*/] = 1; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use array subscript when the index is not an integer constant expression [cppcoreguidelines-pro-bounds-constant-array-index] + int jj = m[pos - 1]; + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: do not use array subscript when the index is not an integer constant expression + + m.at(pos-1) = 2; // OK, at() instead of [] + gsl::at(m, pos-1) = 2; // OK, gsl::at() instead of [] + m[-1] = 3; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: std::array<> index -1 is negative [cppcoreguidelines-pro-bounds-constant-array-index] + m[10] = 4; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: std::array<> index 10 is past the end of the array (which contains 10 elements) [cppcoreguidelines-pro-bounds-constant-array-index] + + m[const_index(7)] = 3; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: std::array<> index 10 is past the end of the array (which contains 10 elements) + + m[0] = 3; // OK, constant index and inside bounds + m[1] = 3; // OK, constant index and inside bounds + m[9] = 3; // OK, constant index and inside bounds + m[const_index(6)] = 3; // OK, constant index and inside bounds } void g() { -- 2.7.4