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