Debian Codegraph @

llvm-project/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.h

raw
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::tidy::readability {
15
16 /// Checks whether a call to the `size()` method can be replaced with a call to
17 /// `empty()`.
18 ///
19 /// The emptiness of a container should be checked using the `empty()` method
20 /// instead of the `size()` method. It is not guaranteed that `size()` is a
21 /// constant-time function, and it is generally more efficient and also shows
22 /// clearer intent to use `empty()`. Furthermore some containers may implement
23 /// the `empty()` method but not implement the `size()` method. Using `empty()`
24 /// whenever possible makes it easier to switch to another container in the
25 /// future.
26 class ContainerSizeEmptyCheck : public ClangTidyCheck {
27 public:
28 ContainerSizeEmptyCheck(StringRef Name, ClangTidyContext *Context);
29 bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
30 return LangOpts.CPlusPlus;
31 }
32 void registerMatchers(ast_matchers::MatchFinder *Finder) override;
33 void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
34 std::optional<TraversalKind> getCheckTraversalKind() const override {
35 return TK_IgnoreUnlessSpelledInSource;
36 }
37 };
38
39 } // namespace clang::tidy::readability
40
41 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_CONTAINERSIZEEMPTYCHECK_H