Add new clang-tidy check for string_view(nullptr)
authorCJ Johnson <johnsoncj@google.com>
Thu, 2 Dec 2021 13:17:12 +0000 (13:17 +0000)
committerYitzhak Mandelbaum <yitzhakm@google.com>
Thu, 2 Dec 2021 13:25:28 +0000 (13:25 +0000)
commit6a9487df73e917c4faf5e060f2bb33c6ade3f967
tree8d2a53fe991b36bd2b464d4af9ede7cc7d95af51
parent99961b4f1c73f135e5022d7c09dca05eb867cf7b
Add new clang-tidy check for string_view(nullptr)

Checks for various ways that the `const CharT*` constructor of `std::basic_string_view` can be passed a null argument and replaces them with the default constructor in most cases. For the comparison operators, braced initializer list does not compile so instead a call to `.empty()` or the empty string literal are used, where appropriate.

This prevents code from invoking behavior which is unconditionally undefined. The single-argument `const CharT*` constructor does not check for the null case before dereferencing its input. The standard is slated to add an explicitly-deleted overload to catch some of these cases: wg21.link/p2166

https://reviews.llvm.org/D114823 is a companion change to prevent duplicate warnings from the `bugprone-string-constructor` check.

Reviewed By: ymandel

Differential Revision: https://reviews.llvm.org/D113148
clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
clang-tools-extra/clang-tidy/bugprone/StringviewNullptrCheck.cpp [new file with mode: 0644]
clang-tools-extra/clang-tidy/bugprone/StringviewNullptrCheck.h [new file with mode: 0644]
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/docs/clang-tidy/checks/bugprone-stringview-nullptr.rst [new file with mode: 0644]
clang-tools-extra/docs/clang-tidy/checks/list.rst
clang-tools-extra/test/clang-tidy/checkers/bugprone-stringview-nullptr.cpp [new file with mode: 0644]