diff options
Diffstat (limited to 'clang-tools-extra/docs')
| -rw-r--r-- | clang-tools-extra/docs/ReleaseNotes.rst | 8 | ||||
| -rw-r--r-- | clang-tools-extra/docs/clang-tidy/checks/modernize-avoid-bind.rst | 57 |
2 files changed, 62 insertions, 3 deletions
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index b96feecdf3d..91a196deb6f 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -196,6 +196,14 @@ Improvements to clang-tidy <clang-tidy/checks/readability-redundant-string-init>` check now supports a `StringNames` option enabling its application to custom string classes. +- Improved :doc:`modernize-avoid-bind + <clang-tidy/checks/modernize-avoid-bind>` check. + + The check now supports supports diagnosing and fixing arbitrary callables instead of + only simple free functions. The `PermissiveParameterList` option has also been + added to address situations where the existing fix-it logic would sometimes generate + code that no longer compiles. + Improvements to include-fixer ----------------------------- diff --git a/clang-tools-extra/docs/clang-tidy/checks/modernize-avoid-bind.rst b/clang-tools-extra/docs/clang-tidy/checks/modernize-avoid-bind.rst index 7ea9beca8e8..82c290e4a21 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/modernize-avoid-bind.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/modernize-avoid-bind.rst @@ -3,10 +3,15 @@ modernize-avoid-bind ==================== -The check finds uses of ``std::bind`` and replaces simple uses with lambdas. -Lambdas will use value-capture where required. +The check finds uses of ``std::bind`` and ``boost::bind`` and replaces them +with lambdas. Lambdas will use value-capture unless reference capture is +explicitly requested with ``std::ref`` or ``boost::ref``. -Right now it only handles free functions, not member functions. +It supports arbitrary callables including member functions, function objects, +and free functions, and all variations thereof. Anything that you can pass +to the first argument of ``bind`` should be diagnosable. Currently, the only +known case where a fix-it is unsupported is when the same placeholder is +specified multiple times in the parameter list. Given: @@ -35,3 +40,49 @@ is replaced by: ``std::bind`` can be hard to read and can result in larger object files and binaries due to type information that will not be produced by equivalent lambdas. + +Options +------- + +.. option:: PermissiveParameterList + + If the option is set to non-zero, the check will append ``auto&&...`` to the end + of every placeholder parameter list. Without this, it is possible for a fix-it + to perform an incorrect transformation in the case where the result of the ``bind`` + is used in the context of a type erased functor such as ``std::function`` which + allows mismatched arguments. For example: + + +.. code-block:: c++ + + int add(int x, int y) { return x + y; } + int foo() { + std::function<int(int,int)> ignore_args = std::bind(add, 2, 2); + return ignore_args(3, 3); + } + +is valid code, and returns `4`. The actual values passed to ``ignore_args`` are +simply ignored. Without ``PermissiveParameterList``, this would be transformed into + +.. code-block:: c++ + + int add(int x, int y) { return x + y; } + int foo() { + std::function<int(int,int)> ignore_args = [] { return add(2, 2); } + return ignore_args(3, 3); + } + +which will *not* compile, since the lambda does not contain an ``operator()`` that +that accepts 2 arguments. With permissive parameter list, it instead generates + +.. code-block:: c++ + + int add(int x, int y) { return x + y; } + int foo() { + std::function<int(int,int)> ignore_args = [](auto&&...) { return add(2, 2); } + return ignore_args(3, 3); + } + +which is correct. + +This check requires using C++14 or higher to run. |

