47 lines · cpp
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-exception6//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(Case))21 return true;22 23 Case = Case->getNextSwitchCase();24 }25 return false;26}27} // namespace28 29void SwitchMissingDefaultCaseCheck::registerMatchers(MatchFinder *Finder) {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 39void SwitchMissingDefaultCaseCheck::check(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::bugprone47