clang-tools 22.0.0git
SwitchMissingDefaultCaseCheck.cpp
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
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(Case))
21 return true;
22
23 Case = Case->getNextSwitchCase();
24 }
25 return false;
26}
27} // namespace
28
30 Finder->addMatcher(
31 switchStmt(hasCondition(expr(unless(isInstantiationDependent()),
32 hasType(qualType(hasCanonicalType(
33 unless(hasDeclaration(enumDecl()))))))),
34 unless(hasDefaultCase()))
35 .bind("switch"),
36 this);
37}
38
40 const ast_matchers::MatchFinder::MatchResult &Result) {
41 const auto *Switch = Result.Nodes.getNodeAs<SwitchStmt>("switch");
42
43 diag(Switch->getSwitchLoc(), "switching on non-enum value without "
44 "default case may not cover all cases");
45}
46} // namespace clang::tidy::bugprone
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
AST_MATCHER(BinaryOperator, isRelationalOperator)