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
11using namespace clang::ast_matchers;
12
13namespace clang::tidy::bugprone {
14
15namespace {
16
17AST_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
29void 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
39void 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

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

source code of clang-tools-extra/clang-tidy/bugprone/SwitchMissingDefaultCaseCheck.cpp