clang-tools 22.0.0git
UnaryStaticAssertCheck.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#include "clang/ASTMatchers/ASTMatchFinder.h"
11
12using namespace clang::ast_matchers;
13
14namespace clang::tidy::modernize {
15
17 Finder->addMatcher(staticAssertDecl().bind("static_assert"), this);
18}
19
20void UnaryStaticAssertCheck::check(const MatchFinder::MatchResult &Result) {
21 const auto *MatchedDecl =
22 Result.Nodes.getNodeAs<StaticAssertDecl>("static_assert");
23 const auto *AssertMessage =
24 dyn_cast_if_present<StringLiteral>(MatchedDecl->getMessage());
25
26 SourceLocation Loc = MatchedDecl->getLocation();
27
28 if (!AssertMessage || AssertMessage->getLength() ||
29 AssertMessage->getBeginLoc().isMacroID() || Loc.isMacroID())
30 return;
31
32 diag(Loc,
33 "use unary 'static_assert' when the string literal is an empty string")
34 << FixItHint::CreateRemoval(AssertMessage->getSourceRange());
35}
36
37} // namespace clang::tidy::modernize
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override