clang-tools 22.0.0git
PreferIsaOrDynCastInConditionalsCheck.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_PREFERISAORDYNCASTINCONDITIONALSCHECK_H
10#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_PREFERISAORDYNCASTINCONDITIONALSCHECK_H
11
12#include "../ClangTidyCheck.h"
13
15
16/// Looks at conditionals and finds and replaces cases of ``cast<>``, which will
17/// assert rather than return a null pointer, and ``dyn_cast<>`` where
18/// the return value is not captured. Additionally, finds and replaces cases
19/// that match the pattern ``var && isa<X>(var)``, where ``var`` is evaluated
20/// twice.
21///
22/// Finds cases like these:
23/// \code
24/// if (auto x = cast<X>(y)) {}
25/// // is replaced by:
26/// if (auto x = dyn_cast<X>(y)) {}
27///
28/// if (cast<X>(y)) {}
29/// // is replaced by:
30/// if (isa<X>(y)) {}
31///
32/// if (dyn_cast<X>(y)) {}
33/// // is replaced by:
34/// if (isa<X>(y)) {}
35///
36/// if (var && isa<T>(var)) {}
37/// // is replaced by:
38/// if (isa_and_nonnull<T>(var.foo())) {}
39/// \endcode
40///
41/// // Other cases are ignored, e.g.:
42/// \code
43/// if (auto f = cast<Z>(y)->foo()) {}
44/// if (cast<Z>(y)->foo()) {}
45/// if (X.cast(y)) {}
46/// \endcode
47///
48/// For the user-facing documentation see:
49/// https://clang.llvm.org/extra/clang-tidy/checks/llvm/prefer-isa-or-dyn-cast-in-conditionals.html
51public:
53 ClangTidyContext *Context)
54 : ClangTidyCheck(Name, Context) {}
55 bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
56 return LangOpts.CPlusPlus;
57 }
58 void registerMatchers(ast_matchers::MatchFinder *Finder) override;
59 void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
60};
61
62} // namespace clang::tidy::llvm_check
63
64#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_PREFERISAORDYNCASTINCONDITIONALSCHECK_H
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override