1

Is it possible to solve the following issue clang tidy is throwing out. error: do not use pointer arithmetic [cppcoreguidelines-pro-bounds-pointer-arithmetic,-warnings-as-errors]

The project I am using is a mix of C/C++ and no changes can be made on the C side.

extern const Test_Ptr* test;
auto enable = (Test_Ptr->pIndex[1].base == 1)
                        ? true
                        : false;
tester123
  • 399
  • 1
  • 3
  • 10

2 Answers2

0

Wrap the pointer in a span and access through that:

// these should be defined somewhere in the C library in some form
constexpr std::size_t pIndexLength = 2;  // or whatever is the actual length
using T = decltype(*test);               // or whatever the pointed type is

span<T> pIndex {Test_Ptr->pIndex, pIndexLength};
int base = pIndex[1].base;

There is no standard implementation of span until C++20, so in older language version you need to use a non-standard implementation.

Another approach is to not use that convention validation option.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

That warning (promoted to error by your configuration) is, frankly, silly.

You could work around it but only by making your code harder to follow and maintain.

Just turn off cppcoreguidelines-pro-bounds-pointer-arithmetic.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055