[all-commits] [llvm/llvm-project] 6a9487: Add new clang-tidy check for string_view(nullptr)

CJ Johnson via All-commits all-commits at lists.llvm.org
Thu Dec 2 05:26:46 PST 2021


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 6a9487df73e917c4faf5e060f2bb33c6ade3f967
      https://github.com/llvm/llvm-project/commit/6a9487df73e917c4faf5e060f2bb33c6ade3f967
  Author: CJ Johnson <johnsoncj at google.com>
  Date:   2021-12-02 (Thu, 02 Dec 2021)

  Changed paths:
    M clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
    M clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
    A clang-tools-extra/clang-tidy/bugprone/StringviewNullptrCheck.cpp
    A clang-tools-extra/clang-tidy/bugprone/StringviewNullptrCheck.h
    M clang-tools-extra/docs/ReleaseNotes.rst
    A clang-tools-extra/docs/clang-tidy/checks/bugprone-stringview-nullptr.rst
    M clang-tools-extra/docs/clang-tidy/checks/list.rst
    A clang-tools-extra/test/clang-tidy/checkers/bugprone-stringview-nullptr.cpp

  Log Message:
  -----------
  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




More information about the All-commits mailing list