xref: /llvm-project/clang-tools-extra/clang-tidy/llvm/PreferIsaOrDynCastInConditionalsCheck.h (revision 4718da506091a37ca4863d979bc541e359b79b10)
128413dd8SDon Hinton //===--- PreferIsaOrDynCastInConditionalsCheck.h - clang-tidy ---*- C++ -*-===//
228413dd8SDon Hinton //
328413dd8SDon Hinton // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
428413dd8SDon Hinton // See https://llvm.org/LICENSE.txt for license information.
528413dd8SDon Hinton // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
628413dd8SDon Hinton //
728413dd8SDon Hinton //===----------------------------------------------------------------------===//
828413dd8SDon Hinton 
9b75e7eaeSDon Hinton #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_PREFERISAORDYNCASTINCONDITIONALSCHECK_H
10b75e7eaeSDon Hinton #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_PREFERISAORDYNCASTINCONDITIONALSCHECK_H
1128413dd8SDon Hinton 
1228413dd8SDon Hinton #include "../ClangTidyCheck.h"
1328413dd8SDon Hinton 
14*4718da50SCarlos Galvez namespace clang::tidy::llvm_check {
1528413dd8SDon Hinton 
16282dc72cSDmitri Gribenko /// Looks at conditionals and finds and replaces cases of ``cast<>``, which will
1728413dd8SDon Hinton /// assert rather than return a null pointer, and ``dyn_cast<>`` where
1828413dd8SDon Hinton /// the return value is not captured.  Additionally, finds and replaces cases that match the
1928413dd8SDon Hinton /// pattern ``var && isa<X>(var)``, where ``var`` is evaluated twice.
2028413dd8SDon Hinton ///
2128413dd8SDon Hinton /// Finds cases like these:
2228413dd8SDon Hinton /// \code
2328413dd8SDon Hinton ///  if (auto x = cast<X>(y)) {}
2428413dd8SDon Hinton ///  // is replaced by:
2528413dd8SDon Hinton ///  if (auto x = dyn_cast<X>(y)) {}
2628413dd8SDon Hinton ///
2728413dd8SDon Hinton ///  if (cast<X>(y)) {}
2828413dd8SDon Hinton ///  // is replaced by:
2928413dd8SDon Hinton ///  if (isa<X>(y)) {}
3028413dd8SDon Hinton ///
3128413dd8SDon Hinton ///  if (dyn_cast<X>(y)) {}
3228413dd8SDon Hinton ///  // is replaced by:
3328413dd8SDon Hinton ///  if (isa<X>(y)) {}
3428413dd8SDon Hinton ///
3528413dd8SDon Hinton ///  if (var && isa<T>(var)) {}
3628413dd8SDon Hinton ///  // is replaced by:
3728413dd8SDon Hinton ///  if (isa_and_nonnull<T>(var.foo())) {}
3828413dd8SDon Hinton /// \endcode
3928413dd8SDon Hinton ///
4028413dd8SDon Hinton ///  // Other cases are ignored, e.g.:
4128413dd8SDon Hinton /// \code
4228413dd8SDon Hinton ///  if (auto f = cast<Z>(y)->foo()) {}
4328413dd8SDon Hinton ///  if (cast<Z>(y)->foo()) {}
4428413dd8SDon Hinton ///  if (X.cast(y)) {}
4528413dd8SDon Hinton /// \endcode
4628413dd8SDon Hinton ///
4728413dd8SDon Hinton /// For the user-facing documentation see:
486e566bc5SRichard /// http://clang.llvm.org/extra/clang-tidy/checks/llvm/prefer-isa-or-dyn-cast-in-conditionals.html
4928413dd8SDon Hinton class PreferIsaOrDynCastInConditionalsCheck : public ClangTidyCheck {
5028413dd8SDon Hinton public:
PreferIsaOrDynCastInConditionalsCheck(StringRef Name,ClangTidyContext * Context)5128413dd8SDon Hinton   PreferIsaOrDynCastInConditionalsCheck(StringRef Name,
5228413dd8SDon Hinton                                         ClangTidyContext *Context)
5328413dd8SDon Hinton       : ClangTidyCheck(Name, Context) {}
isLanguageVersionSupported(const LangOptions & LangOpts)54e40a742aSNathan James   bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
55e40a742aSNathan James     return LangOpts.CPlusPlus;
56e40a742aSNathan James   }
5728413dd8SDon Hinton   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
5828413dd8SDon Hinton   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
5928413dd8SDon Hinton };
6028413dd8SDon Hinton 
61*4718da50SCarlos Galvez } // namespace clang::tidy::llvm_check
6228413dd8SDon Hinton 
63b75e7eaeSDon Hinton #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_PREFERISAORDYNCASTINCONDITIONALSCHECK_H
64