xref: /llvm-project/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.h (revision 30bb1863892a235fe1d90eb6b496bd24b0c177d8)
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 #include <vector>
14 
15 namespace clang::tidy::readability {
16 
17 /// Checks whether a call to the `size()`/`length()` method can be replaced with
18 /// a call to `empty()`.
19 ///
20 /// The emptiness of a container should be checked using the `empty()` method
21 /// instead of the `size()`/`length()` method. It shows clearer intent to use
22 /// `empty()`. Furthermore some containers (for example, a `std::forward_list`)
23 /// may implement the `empty()` method but not implement the `size()` or
24 /// `length()` method. Using `empty()` whenever possible makes it easier to
25 /// switch to another container in the 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   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
35   std::optional<TraversalKind> getCheckTraversalKind() const override {
36     return TK_IgnoreUnlessSpelledInSource;
37   }
38 
39 private:
40   std::vector<llvm::StringRef> ExcludedComparisonTypes;
41 };
42 
43 } // namespace clang::tidy::readability
44 
45 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_CONTAINERSIZEEMPTYCHECK_H
46