[clang-tidy] Add new checker: llvm-prefer-isa-or-dyn-cast-in-conditionals
authorDon Hinton <hintonda@gmail.com>
Wed, 24 Apr 2019 21:25:57 +0000 (21:25 +0000)
committerDon Hinton <hintonda@gmail.com>
Wed, 24 Apr 2019 21:25:57 +0000 (21:25 +0000)
commit28413dd87aa21c72ab4d3e3f686647999eb59903
treefd1bf161a117977083606ab6324221f43fc7aba7
parentc95c08baa1afb3b654bd756edfaac1c0a063fed9
[clang-tidy] Add new checker: llvm-prefer-isa-or-dyn-cast-in-conditionals

Summary:
Looks at conditionals and finds cases of ``cast<>``, which will
assert rather than return a null pointer, and ``dyn_cast<>`` where
the return value is not captured. Additionally, finds cases that
match the pattern ``var.foo() && isa<X>(var.foo())``, where the
method is called twice and could be expensive.

.. code-block:: c++

  // Finds cases like these:
  if (auto x = cast<X>(y)) <...>
  if (cast<X>(y)) <...>

  // But not cases like these:
  if (auto f = cast<Z>(y)->foo()) <...>
  if (cast<Z>(y)->foo()) <...>

Reviewers: alexfh, rjmccall, hokein, aaron.ballman, JonasToth

Reviewed By: aaron.ballman

Subscribers: xbolva00, Eugene.Zelenko, mgorny, xazax.hun, cfe-commits

Tags: #clang-tools-extra, #clang

Differential Revision: https://reviews.llvm.org/D59802

llvm-svn: 359142
clang-tools-extra/clang-tidy/llvm/CMakeLists.txt
clang-tools-extra/clang-tidy/llvm/LLVMTidyModule.cpp
clang-tools-extra/clang-tidy/llvm/PreferIsaOrDynCastInConditionalsCheck.cpp [new file with mode: 0644]
clang-tools-extra/clang-tidy/llvm/PreferIsaOrDynCastInConditionalsCheck.h [new file with mode: 0644]
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/docs/clang-tidy/checks/list.rst
clang-tools-extra/docs/clang-tidy/checks/llvm-prefer-isa-or-dyn-cast-in-conditionals.rst [new file with mode: 0644]
clang-tools-extra/test/clang-tidy/llvm-prefer-isa-or-dyn-cast-in-conditionals.cpp [new file with mode: 0644]