blob: bde83f8830f47b967a45525fd21ef6affccc0a9b [file] [log] [blame]
Alexander Kornienko1b677db2015-03-09 12:18:39 +00001//===--- ContainerSizeEmptyCheck.h - clang-tidy -----------------*- C++ -*-===//
Alexander Kornienko4babd682015-01-15 15:46:58 +00002//
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 Kornienko1b677db2015-03-09 12:18:39 +000010#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_CONTAINERSIZEEMPTYCHECK_H
11#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_CONTAINERSIZEEMPTYCHECK_H
Alexander Kornienko4babd682015-01-15 15:46:58 +000012
13#include "../ClangTidy.h"
14
15namespace clang {
16namespace tidy {
17namespace readability {
18
Alexander Kornienkof8ed0a82015-08-27 18:01:58 +000019/// Checks whether a call to the `size()` method can be replaced with a call to
20/// `empty()`.
Alexander Kornienko4babd682015-01-15 15:46:58 +000021///
Alexander Kornienkof8ed0a82015-08-27 18:01:58 +000022/// 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 Kornienko4babd682015-01-15 15:46:58 +000024/// constant-time function, and it is generally more efficient and also shows
Alexander Kornienkof8ed0a82015-08-27 18:01:58 +000025/// 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 Kornienko4babd682015-01-15 15:46:58 +000029class ContainerSizeEmptyCheck : public ClangTidyCheck {
30public:
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 Kornienko1b677db2015-03-09 12:18:39 +000040#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_CONTAINERSIZEEMPTYCHECK_H