diff options
| author | Matthias Gehre <gehre.matthias@gmail.com> | 2019-09-22 23:19:41 +0200 |
|---|---|---|
| committer | Matthias Gehre <gehre.matthias@gmail.com> | 2019-11-06 09:27:02 +0100 |
| commit | 24130d661ed42c30f009b695d3c9ce57d2208b5e (patch) | |
| tree | ef198d4aeec67df10b1e84c682db274940e8fe59 /clang-tools-extra/docs | |
| parent | 092452d402d793c731c3861ba920a85c5c4e1fff (diff) | |
| download | bcm5719-llvm-24130d661ed42c30f009b695d3c9ce57d2208b5e.tar.gz bcm5719-llvm-24130d661ed42c30f009b695d3c9ce57d2208b5e.zip | |
[clang-tidy] Add readability-make-member-function-const
Summary:
Finds non-static member functions that can be made ``const``
because the functions don't use ``this`` in a non-const way.
The check conservatively tries to preserve logical costness in favor of
physical costness. See readability-make-member-function-const.rst for more
details.
Reviewers: aaron.ballman, gribozavr, hokein, alexfh
Subscribers: mgorny, xazax.hun, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D68074
Diffstat (limited to 'clang-tools-extra/docs')
3 files changed, 74 insertions, 0 deletions
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 4a7fef5bb03..40b7aed4178 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -147,6 +147,12 @@ Improvements to clang-tidy Finds classes, structs, and unions that contain redundant member access specifiers. +- New :doc:`readability-make-member-function-const + <clang-tidy/checks/readability-make-member-function-const>` check. + + Finds non-static member functions that can be made ``const`` + because the functions don't use ``this`` in a non-const way. + Improvements to include-fixer ----------------------------- diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index 2801168d632..b499a84fdcf 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -361,6 +361,7 @@ Clang-Tidy Checks readability-inconsistent-declaration-parameter-name readability-isolate-declaration readability-magic-numbers + readability-make-member-function-const readability-misleading-indentation readability-misplaced-array-index readability-named-parameter diff --git a/clang-tools-extra/docs/clang-tidy/checks/readability-make-member-function-const.rst b/clang-tools-extra/docs/clang-tidy/checks/readability-make-member-function-const.rst new file mode 100644 index 00000000000..2d1de5fdeb7 --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/readability-make-member-function-const.rst @@ -0,0 +1,67 @@ +.. title:: clang-tidy - readability-make-member-function-const + +readability-make-member-function-const +====================================== + +Finds non-static member functions that can be made ``const`` +because the functions don't use ``this`` in a non-const way. + +This check tries to annotate methods according to +`logical constness <https://isocpp.org/wiki/faq/const-correctness#logical-vs-physical-state>`_ +(not physical constness). +Therefore, it will suggest to add a ``const`` qualifier to a non-const +method only if this method does something that is already possible though the +public interface on a ``const`` pointer to the object: + +* reading a public member variable +* calling a public const-qualified member function +* returning const-qualified ``this`` +* passing const-qualified ``this`` as a parameter. + +This check will also suggest to add a ``const`` qualifier to a non-const +method if this method uses private data and functions in a limited number of +ways where logical constness and physical constness coincide: + +* reading a member variable of builtin type + +Specifically, this check will not suggest to add a ``const`` to a non-const +method if the method reads a private member variable of pointer type because +that allows to modify the pointee which might not preserve logical constness. +For the same reason, it does not allow to call private member functions +or member functions on private member variables. + +In addition, this check ignores functions that + +* are declared ``virtual`` +* contain a ``const_cast`` +* are templated or part of a class template +* have an empty body +* do not (implicitly) use ``this`` at all + (see `readability-convert-member-functions-to-static <readability-convert-member-functions-to-static.html>`_). + +The following real-world examples will be preserved by the check: + +.. code-block:: c++ + + class E1 { + Pimpl &getPimpl() const; + public: + int &get() { + // Calling a private member function disables this check. + return getPimpl()->i; + } + ... + }; + + class E2 { + public: + const int *get() const; + // const_cast disables this check. + S *get() { + return const_cast<int*>(const_cast<const C*>(this)->get()); + } + ... + }; + +After applying modifications as suggested by the check, runnnig the check again +might find more opportunities to mark member functions ``const``. |

