Alexander Kornienko | 1b677db | 2015-03-09 12:18:39 +0000 | [diff] [blame] | 1 | //===--- ContainerSizeEmptyCheck.h - clang-tidy -----------------*- C++ -*-===// |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Alexander Kornienko | 1b677db | 2015-03-09 12:18:39 +0000 | [diff] [blame] | 10 | #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_CONTAINERSIZEEMPTYCHECK_H |
| 11 | #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_CONTAINERSIZEEMPTYCHECK_H |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 12 | |
| 13 | #include "../ClangTidy.h" |
| 14 | |
| 15 | namespace clang { |
| 16 | namespace tidy { |
| 17 | namespace readability { |
| 18 | |
Alexander Kornienko | f8ed0a8 | 2015-08-27 18:01:58 +0000 | [diff] [blame] | 19 | /// Checks whether a call to the `size()` method can be replaced with a call to |
| 20 | /// `empty()`. |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 21 | /// |
Alexander Kornienko | f8ed0a8 | 2015-08-27 18:01:58 +0000 | [diff] [blame] | 22 | /// The emptiness of a container should be checked using the `empty()` method |
| 23 | /// instead of the `size()` method. It is not guaranteed that `size()` is a |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 24 | /// constant-time function, and it is generally more efficient and also shows |
Alexander Kornienko | f8ed0a8 | 2015-08-27 18:01:58 +0000 | [diff] [blame] | 25 | /// clearer intent to use `empty()`. Furthermore some containers may implement |
| 26 | /// the `empty()` method but not implement the `size()` method. Using `empty()` |
| 27 | /// whenever possible makes it easier to switch to another container in the |
| 28 | /// future. |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 29 | class ContainerSizeEmptyCheck : public ClangTidyCheck { |
| 30 | public: |
| 31 | ContainerSizeEmptyCheck(StringRef Name, ClangTidyContext *Context); |
| 32 | void registerMatchers(ast_matchers::MatchFinder *Finder) override; |
| 33 | void check(const ast_matchers::MatchFinder::MatchResult &Result) override; |
| 34 | }; |
| 35 | |
| 36 | } // namespace readability |
| 37 | } // namespace tidy |
| 38 | } // namespace clang |
| 39 | |
Alexander Kornienko | 1b677db | 2015-03-09 12:18:39 +0000 | [diff] [blame] | 40 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_CONTAINERSIZEEMPTYCHECK_H |