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. 28 class ContainerSizeEmptyCheck : public ClangTidyCheck { 29 public: 30 ContainerSizeEmptyCheck(StringRef Name, ClangTidyContext *Context); isLanguageVersionSupported(const LangOptions & LangOpts)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 }; 37 38 } // namespace readability 39 } // namespace tidy 40 } // namespace clang 41 42 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_CONTAINERSIZEEMPTYCHECK_H 43