Changeset View
Standalone View
clang-tools-extra/docs/clang-tidy/checks/readability/use-early-exits.rst
- This file was added.
.. title:: clang-tidy - readability-use-early-exits | |||||
readability-use-early-exits | |||||
=========================== | |||||
Finds ``if`` statements inside functions and loops that could be flipped to | |||||
make an early exit. | |||||
Example: | |||||
.. code-block:: c++ | |||||
void Process(MyClass* C) { | |||||
if (C) { | |||||
// Do some long processing. | |||||
} | |||||
} | |||||
void Process(span<MyClass*> C){ | |||||
for (MyClass *Item : C) { | |||||
if (Item) { | |||||
// Do some long processing. | |||||
} | |||||
} | |||||
} | |||||
Would be transformed to: | |||||
.. code-block:: c++ | |||||
void Process(MyClass* C) { | |||||
if (!C) | |||||
return; | |||||
// Do some long processing. | |||||
} | |||||
void Process(span<MyClass*> C){ | |||||
for (MyClass *Item : C) { | |||||
if (!Item) | |||||
continue; | |||||
// Do some long processing. | |||||
} | |||||
} | |||||
Options | |||||
------- | |||||
.. option:: LineCountThreshold | |||||
Don't transform any ``if`` statement if the statement uses less than this | |||||
many lines. Default value is `10`. | |||||
.. option:: SplitConjunctions | |||||
If `true`, split up conditions with cunjunctions (``&&``) into multiple | |||||
``if`` statements. Default value is `false`. | |||||
JonasToth: typo `cunjunctions -> conjunctions` | |||||
When `true`: | |||||
.. code-block:: c++ | |||||
void Process(bool A, bool B) { | |||||
if (A && B) { | |||||
// Long processing. | |||||
Not Done ReplyInline Actionsif this option is false, the transformation would be if(!(A && B)), right? should demorgan rules be applied or at least be mentioned here? I think transforming to if (!A || !B) is at least a viable option for enough users. JonasToth: if this option is false, the transformation would be `if(!(A && B))`, right?
should demorgan… | |||||
Not Done ReplyInline ActionsOnce this is in, I plan to merge some common code with the simplify-boolean-expr logic for things like demorgan processing. Right now the transformation happens, the simplify boolean suggests a demorgan transformation of you run the output through clang tidy. njames93: Once this is in, I plan to merge some common code with the simplify-boolean-expr logic for… | |||||
a short reference to the readability-simplify-boolean-expr check in the user facing docs would be great. would this check then use the same settings as the readability-simplify-boolean-expr check? (that of course off topic and does not relate to this patch :) ) JonasToth: a short reference to the `readability-simplify-boolean-expr` check in the user facing docs… | |||||
I'm not sure it's really necessary to mention that the fix would likely need another fix, besides that comment would just be removed in the follow up. njames93: I'm not sure it's really necessary to mention that the fix would likely need another fix… | |||||
ok, thats good with me. JonasToth: ok, thats good with me. | |||||
} | |||||
} | |||||
Would be transformed to: | |||||
.. code-block:: c++ | |||||
void Process(bool A, bool B) { | |||||
if (!A) | |||||
return; | |||||
if (!B) | |||||
return; | |||||
// Long processing. | |||||
} | |||||
.. option:: WrapInBraces | |||||
If `true`, the early exit will be wrapped in braces. | |||||
If unspecified, the value will be inferred based on whether | |||||
:doc:`readability-braces-around-statements <braces-around-statements>` or one | |||||
of its alias' are active. | |||||
When `true`, The above exmples would be transformed to: | |||||
.. code-block:: c++ | |||||
void Process(MyClass *C) { | |||||
if (!C) { | |||||
return; | |||||
} | |||||
// Do some long processing | |||||
} | |||||
void Process(span<MyClass*> C){ | |||||
for (MyClass *Item : C) { | |||||
if (!Item) { | |||||
continue; | |||||
} | |||||
// Do some long processing. | |||||
} | |||||
} | |||||
Please add newline. Eugene.Zelenko: Please add newline. | |||||
maybe highlight the if as code with double quotes? JonasToth: maybe highlight the `if` as code with double quotes? |
typo cunjunctions -> conjunctions