| 1 |
.. title:: clang-tidy - llvm-prefer-isa-or-dyn-cast-in-conditionals |
| 2 |
|
| 3 |
llvm-prefer-isa-or-dyn-cast-in-conditionals |
| 4 |
=========================================== |
| 5 |
|
| 6 |
Looks at conditionals and finds and replaces cases of ``cast<>``, |
| 7 |
which will assert rather than return a null pointer, and |
| 8 |
``dyn_cast<>`` where the return value is not captured. Additionally, |
| 9 |
finds and replaces cases that match the pattern ``var && |
| 10 |
isa<X>(var)``, where ``var`` is evaluated twice. |
| 11 |
|
| 12 |
.. code-block:: c++ |
| 13 |
|
| 14 |
// Finds these: |
| 15 |
if (auto x = cast<X>(y)) {} |
| 16 |
// is replaced by: |
| 17 |
if (auto x = dyn_cast<X>(y)) {} |
| 18 |
|
| 19 |
if (cast<X>(y)) {} |
| 20 |
// is replaced by: |
| 21 |
if (isa<X>(y)) {} |
| 22 |
|
| 23 |
if (dyn_cast<X>(y)) {} |
| 24 |
// is replaced by: |
| 25 |
if (isa<X>(y)) {} |
| 26 |
|
| 27 |
if (var && isa<T>(var)) {} |
| 28 |
// is replaced by: |
| 29 |
if (isa_and_nonnull<T>(var.foo())) {} |
| 30 |
|
| 31 |
// Other cases are ignored, e.g.: |
| 32 |
if (auto f = cast<Z>(y)->foo()) {} |
| 33 |
if (cast<Z>(y)->foo()) {} |
| 34 |
if (X.cast(y)) {} |