| 1 | //===--- SwitchMissingDefaultCaseCheck.cpp - clang-tidy -------------------===// |
|---|---|
| 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 | #include "SwitchMissingDefaultCaseCheck.h" |
| 10 | |
| 11 | using namespace clang::ast_matchers; |
| 12 | |
| 13 | namespace clang::tidy::bugprone { |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | AST_MATCHER(SwitchStmt, hasDefaultCase) { |
| 18 | const SwitchCase *Case = Node.getSwitchCaseList(); |
| 19 | while (Case) { |
| 20 | if (DefaultStmt::classof(T: Case)) |
| 21 | return true; |
| 22 | |
| 23 | Case = Case->getNextSwitchCase(); |
| 24 | } |
| 25 | return false; |
| 26 | } |
| 27 | } // namespace |
| 28 | |
| 29 | void SwitchMissingDefaultCaseCheck::registerMatchers(MatchFinder *Finder) { |
| 30 | Finder->addMatcher( |
| 31 | NodeMatch: switchStmt(hasCondition(InnerMatcher: expr(unless(isInstantiationDependent()), |
| 32 | hasType(InnerMatcher: qualType(hasCanonicalType( |
| 33 | InnerMatcher: unless(hasDeclaration(InnerMatcher: enumDecl()))))))), |
| 34 | unless(hasDefaultCase())) |
| 35 | .bind(ID: "switch"), |
| 36 | Action: this); |
| 37 | } |
| 38 | |
| 39 | void SwitchMissingDefaultCaseCheck::check( |
| 40 | const ast_matchers::MatchFinder::MatchResult &Result) { |
| 41 | const auto *Switch = Result.Nodes.getNodeAs<SwitchStmt>(ID: "switch"); |
| 42 | |
| 43 | diag(Loc: Switch->getSwitchLoc(), Description: "switching on non-enum value without " |
| 44 | "default case may not cover all cases"); |
| 45 | } |
| 46 | } // namespace clang::tidy::bugprone |
| 47 |
