This introduces a new check, readability-containter-data-pointer. This
check is meant to catch the cases where the user may be trying to
materialize the data pointer by taking the address of the 0-th member of
a container. With C++11 or newer, the data member should be used for
this. This provides the following benefits:
- .data() is easier to read than &[0]
- it avoids an unnecessary re-materialization of the pointer
- this doesn't matter in the case of optimized code, but in the case of unoptimized code, this will be visible
- it avoids a potential invalid memory de-reference caused by the indexing when the container is empty (in debug mode, clang will normally optimize away the re-materialization in optimized builds).
The small potential behavioural change raises the question of where the
check should belong. A reasoning of defense in depth applies here, and
this does an unchecked conversion, with the assumption that users can
use the static analyzer to catch cases where we can statically identify
an invalid memory de-reference. For the cases where the static analysis
is unable to prove the size of the container, UBSan can be used to track
the invalid access.
Special thanks to Aaron Ballman for the discussion on whether this
check would be useful and where to place it.
This partially resolves PR26817!
Please separate with empty line.