clang-tools  14.0.0git
ContainerSizeEmptyCheck.h
Go to the documentation of this file.
1 //===--- ContainerSizeEmptyCheck.h - clang-tidy -----------------*- C++ -*-===//
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 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_CONTAINERSIZEEMPTYCHECK_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_CONTAINERSIZEEMPTYCHECK_H
11 
12 #include "../ClangTidyCheck.h"
13 
14 namespace clang {
15 namespace tidy {
16 namespace readability {
17 
18 /// Checks whether a call to the `size()` method can be replaced with a call to
19 /// `empty()`.
20 ///
21 /// The emptiness of a container should be checked using the `empty()` method
22 /// instead of the `size()` method. It is not guaranteed that `size()` is a
23 /// constant-time function, and it is generally more efficient and also shows
24 /// clearer intent to use `empty()`. Furthermore some containers may implement
25 /// the `empty()` method but not implement the `size()` method. Using `empty()`
26 /// whenever possible makes it easier to switch to another container in the
27 /// future.
29 public:
30  ContainerSizeEmptyCheck(StringRef Name, ClangTidyContext *Context);
31  bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
32  return LangOpts.CPlusPlus;
33  }
34  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
35  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
36  llvm::Optional<TraversalKind> getCheckTraversalKind() const override {
37  return TK_IgnoreUnlessSpelledInSource;
38  }
39 };
40 
41 } // namespace readability
42 } // namespace tidy
43 } // namespace clang
44 
45 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_CONTAINERSIZEEMPTYCHECK_H
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:54
clang::tidy::readability::ContainerSizeEmptyCheck
Checks whether a call to the size() method can be replaced with a call to empty().
Definition: ContainerSizeEmptyCheck.h:28
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:72
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::readability::ContainerSizeEmptyCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: ContainerSizeEmptyCheck.cpp:176
clang::tidy::readability::ContainerSizeEmptyCheck::isLanguageVersionSupported
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
Override this to disable registering matchers and PP callbacks if an invalid language version is bein...
Definition: ContainerSizeEmptyCheck.h:31
clang::tidy::readability::ContainerSizeEmptyCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: ContainerSizeEmptyCheck.cpp:99
clang::tidy::readability::ContainerSizeEmptyCheck::ContainerSizeEmptyCheck
ContainerSizeEmptyCheck(StringRef Name, ClangTidyContext *Context)
Definition: ContainerSizeEmptyCheck.cpp:95
clang::tidy::readability::ContainerSizeEmptyCheck::getCheckTraversalKind
llvm::Optional< TraversalKind > getCheckTraversalKind() const override
Definition: ContainerSizeEmptyCheck.h:36