Extend the check to catch these conditions:
if (e) return true; else return x; // return e || x; if (e) return false; else return x; // return !e && x; if (e) return true; return x; // return e || x; if (e) return false; return x; // return !e && x;
Paths
| Differential D133102
[clang-tidy] Extend simplify-boolean-expr check Needs RevisionPublic Authored by njames93 on Sep 1 2022, 4:22 AM.
Details Summary Extend the check to catch these conditions: if (e) return true; else return x; // return e || x; if (e) return false; else return x; // return !e && x; if (e) return true; return x; // return e || x; if (e) return false; return x; // return !e && x;
Diff Detail
Event TimelineComment Actions FWIW, I'm not sure this will usually be a simplification of the code. Splitting the condition and using early return is quite a frequent recommendation in code reviews, and I would expect a strong pushback to automated suggestions going in the opposite direction. This revision now requires changes to proceed.Sep 7 2022, 4:12 AM Comment Actions For example, LLVM coding standards recommends using early exits: https://llvm.org/docs/CodingStandards.html#use-early-exits-and-continue-to-simplify-code Comment Actions
How is the proposed replacement not an early exit? It's an unconditional exit: it either returns a bool constant or it returns a variable that is a bool.
Revision Contents
Diff 457228 clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.h
clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/docs/clang-tidy/checks/readability/simplify-boolean-expr.rst
clang-tools-extra/test/clang-tidy/checkers/readability/simplify-bool-expr-case.cpp
clang-tools-extra/test/clang-tidy/checkers/readability/simplify-bool-expr.cpp
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||