[clang-tidy] Types of fixed size and access cause warning cppcoreguidelines-pro-bounds-avoid-unchecked-container-access
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
It seems that `cppcoreguidelines-pro-bounds-avoid-unchecked-container-access` triggers when accessing fixed-bounds types with a fixed index. The following example produces a warning for `my_var[0]` and all 3 instances of `my_var[1]`:
```cpp
std::array my_var = {1, 1};
if (some_var > 0) {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
file_stream.read(reinterpret_cast(my_var.data()), sizeof(my_var[0]));
}
if (some_var > 1)
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
file_stream.read(reinterpret_cast(&my_var[1]), sizeof(my_var[1]));
another_var *= my_var[1];
}
```
These should not produce any warnings since `my_var` is of fixed size (2) and it is being accessed with a fixed integer (not a variale index) that is within bounds. I left the unrelated code in this example in case that is a part of the bug.
The [C++ Core Guidelines SL.con.3](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#rsl-bounds) provide a similar example as acceptable for array access (here my `my_var` is like the `a` example):
```cpp
void f(std::vector& v, std::array a, int i)
{
v[0] = a[0]; // BAD
v.at(0) = a[0]; // OK (alternative 1)
at(v, 0) = a[0]; // OK (alternative 2)
v.at(0) = a[i]; // BAD
v.at(0) = a.at(i); // OK (alternative 1)
v.at(0) = at(a, i); // OK (alternative 2)
}
```
Clang-tidy version: `lvm-toolchain-noble-22/main amd64 clang-tidy-22 amd64 1:22.1.6~++20260514073821+fc4aad7b5db3-1~exp1~20260514073942.73`
Contributor guide
Research direction
Start by reproducing the reported examples with the clang-tidy check cppcoreguidelines-pro-bounds-avoid-unchecked-container-access, then locate that check's implementation and its existing tests. Verify how fixed-size std::array accesses with compile-time in-bounds indices are classified. Done means the reported accesses no longer produce warnings and regression coverage captures the behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100