diff options
author | Alexander Kornienko <alexfh@google.com> | 2015-09-10 16:37:46 +0000 |
---|---|---|
committer | Alexander Kornienko <alexfh@google.com> | 2015-09-10 16:37:46 +0000 |
commit | 7532d3e93d3980d628f10c63c2bc97bca2e7c2c1 (patch) | |
tree | 39a8b5ff46f69b31cdaacdf4b95512a9a63bd93c /clang-tools-extra/docs/clang-tidy | |
parent | e3b1f2b7658ea22b13d6bae34f5ac4c9f4dc8fc6 (diff) | |
download | bcm5719-llvm-7532d3e93d3980d628f10c63c2bc97bca2e7c2c1.tar.gz bcm5719-llvm-7532d3e93d3980d628f10c63c2bc97bca2e7c2c1.zip |
[clang-tidy] Add misc-sizeof-container check to find sizeof() uses on stl
containers.
Summary:
sizeof(some_std_string) is likely to be an error. This check finds this
pattern and suggests using .size() instead.
Reviewers: djasper, klimek, aaron.ballman
Subscribers: aaron.ballman, cfe-commits
Differential Revision: http://reviews.llvm.org/D12759
llvm-svn: 247297
Diffstat (limited to 'clang-tools-extra/docs/clang-tidy')
-rw-r--r-- | clang-tools-extra/docs/clang-tidy/checks/list.rst | 3 | ||||
-rw-r--r-- | clang-tools-extra/docs/clang-tidy/checks/misc-sizeof-container.rst | 20 |
2 files changed, 22 insertions, 1 deletions
diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index be7bf3b0f71..600b09d9273 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -31,6 +31,7 @@ List of clang-tidy Checks misc-macro-repeated-side-effects misc-move-constructor-init misc-noexcept-move-constructor + misc-sizeof-container misc-static-assert misc-swapped-arguments misc-undelegated-constructor @@ -54,4 +55,4 @@ List of clang-tidy Checks readability-named-parameter readability-redundant-smartptr-get readability-redundant-string-cstr - readability-simplify-boolean-expr
\ No newline at end of file + readability-simplify-boolean-expr diff --git a/clang-tools-extra/docs/clang-tidy/checks/misc-sizeof-container.rst b/clang-tools-extra/docs/clang-tidy/checks/misc-sizeof-container.rst new file mode 100644 index 00000000000..2e880d4ef1e --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/misc-sizeof-container.rst @@ -0,0 +1,20 @@ +misc-sizeof-container +===================== + +The check finds usages of ``sizeof`` on expressions of STL container types. Most +likely the user wanted to use ``.size()`` instead. + +Currently only ``std::string`` and ``std::vector<T>`` are supported. + +Examples: + +.. code:: c++ + + std::string s; + int a = 47 + sizeof(s); // warning: sizeof() doesn't return the size of the container. Did you mean .size()? + // The suggested fix is: int a = 47 + s.size(); + + int b = sizeof(std::string); // no warning, probably intended. + + std::string array_of_strings[10]; + int c = sizeof(array_of_strings) / sizeof(array_of_strings[0]); // no warning, definitely intended. |