| 1 |
.. title:: clang-tidy - readability-avoid-return-with-void-value |
| 2 |
|
| 3 |
readability-avoid-return-with-void-value |
| 4 |
======================================== |
| 5 |
|
| 6 |
Finds return statements with ``void`` values used within functions with |
| 7 |
``void`` result types. |
| 8 |
|
| 9 |
A function with a ``void`` return type is intended to perform a task without |
| 10 |
producing a return value. Return statements with expressions could lead |
| 11 |
to confusion and may miscommunicate the function's intended behavior. |
| 12 |
|
| 13 |
Example: |
| 14 |
|
| 15 |
.. code-block:: |
| 16 |
|
| 17 |
void g(); |
| 18 |
void f() { |
| 19 |
// ... |
| 20 |
return g(); |
| 21 |
} |
| 22 |
|
| 23 |
In a long function body, the ``return`` statement suggests that the function |
| 24 |
returns a value. However, ``return g();`` is a combination of two statements |
| 25 |
that should be written as |
| 26 |
|
| 27 |
.. code-block:: |
| 28 |
|
| 29 |
g(); |
| 30 |
return; |
| 31 |
|
| 32 |
to make clear that ``g()`` is called and immediately afterwards the function |
| 33 |
returns (nothing). |
| 34 |
|
| 35 |
In C, the same issue is detected by the compiler if the ``-Wpedantic`` mode |
| 36 |
is enabled. |
| 37 |
|
| 38 |
Options |
| 39 |
------- |
| 40 |
|
| 41 |
.. option:: IgnoreMacros |
| 42 |
|
| 43 |
The value `false` specifies that return statements expanded |
| 44 |
from macros are not checked. The default value is `true`. |
| 45 |
|
| 46 |
.. option:: StrictMode |
| 47 |
|
| 48 |
The value `false` specifies that a direct return statement shall |
| 49 |
be excluded from the analysis if it is the only statement not |
| 50 |
contained in a block, like ``if (cond) return g();``. The default |
| 51 |
value is `true`. |