1 //===--- SwitchMissingDefaultCaseCheck.cpp - clang-tidy -------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "SwitchMissingDefaultCaseCheck.h"
10 #include "clang/AST/ASTContext.h"
12 using namespace clang::ast_matchers
;
14 namespace clang::tidy::bugprone
{
18 AST_MATCHER(SwitchStmt
, hasDefaultCase
) {
19 const SwitchCase
*Case
= Node
.getSwitchCaseList();
21 if (DefaultStmt::classof(Case
))
24 Case
= Case
->getNextSwitchCase();
30 void SwitchMissingDefaultCaseCheck::registerMatchers(MatchFinder
*Finder
) {
32 switchStmt(hasCondition(expr(unless(isInstantiationDependent()),
33 hasType(qualType(hasCanonicalType(
34 unless(hasDeclaration(enumDecl()))))))),
35 unless(hasDefaultCase()))
40 void SwitchMissingDefaultCaseCheck::check(
41 const ast_matchers::MatchFinder::MatchResult
&Result
) {
42 const auto *Switch
= Result
.Nodes
.getNodeAs
<SwitchStmt
>("switch");
44 diag(Switch
->getSwitchLoc(), "switching on non-enum value without "
45 "default case may not cover all cases");
47 } // namespace clang::tidy::bugprone