This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Fix false positive on `ArrayInitIndexExpr` inside `ProBoundsConstantArrayIndexCheck`
ClosedPublic

Authored by isuckatcs on Aug 25 2022, 6:37 AM.

Details

Summary

There are 3 different cases when an ArrayInitLoopExpr is used to initialize an array.

  • in the implicit copy/move constructor for a class with an array member
  • when a lambda-expression captures an array by value
  • when a decomposition declaration decomposes an array

The AST of such expression (usually) looks like this:

|-ArrayInitLoopExpr 'int[3]'
| | |-OpaqueValueExpr 'int[3]' lvalue
| | | `-DeclRefExpr 'int[3]' lvalue Var 'arr' 'int[3]'
| | `-ImplicitCastExpr 'int' <LValueToRValue>
| |   `-ArraySubscriptExpr 'int' lvalue
| |     |-ImplicitCastExpr 'int *' <ArrayToPointerDecay>
| |     | `-OpaqueValueExpr 'int[3]' lvalue
| |     |   `-DeclRefExpr 'int[3]' lvalue Var 'arr' 'int[3]'
| |     `-ArrayInitIndexExpr <<invalid sloc>> 'unsigned long'

We basically always have an ArraySubscriptExpr, where the index is an ArrayInitIndexExpr.
ArrayInitIndexExpr is not a constant, so the checker mentioned in the title reports a warning.
This false positive warning only happens in case of decomposition declaration and lambda capture.

Some example without this patch:

void foo() {
    int arr[3];

    auto [a, b, c] = arr;
}
---------------------------------------------------------------
warning: do not use array subscript when the index is not an integer constant expression [cppcoreguidelines-pro-bounds-constant-array-index]
auto [a, b, c] = arr;
                 ^
void foo() {
    int arr[3];

    [arr]() {};
}
---------------------------------------------------------------
warning: do not use array subscript when the index is not an integer constant expression [cppcoreguidelines-pro-bounds-constant-array-index]
[arr]() {};
 ^

Diff Detail

Event Timeline

isuckatcs created this revision.Aug 25 2022, 6:37 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 25 2022, 6:37 AM
isuckatcs requested review of this revision.Aug 25 2022, 6:37 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 25 2022, 6:37 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
gribozavr2 accepted this revision.Aug 30 2022, 6:48 AM
gribozavr2 added inline comments.
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-constant-array-index.cpp
2

You shouldn't need to add an extra RUN line, check_clang_tidy already loops over all language standards.

This revision is now accepted and ready to land.Aug 30 2022, 6:48 AM
isuckatcs marked an inline comment as done.

Removed the unnecessary extra RUN command.

gribozavr2 accepted this revision.Aug 30 2022, 10:34 AM