This is an archive of the discontinued LLVM Phabricator instance.

[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 Timeline

njames93 created this revision.Sep 1 2022, 4:22 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 1 2022, 4:22 AM
njames93 requested review of this revision.Sep 1 2022, 4:22 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 1 2022, 4:22 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
njames93 updated this revision to Diff 457228.Sep 1 2022, 4:24 AM

Remove unnecessary includes

alexfh requested changes to this revision.Sep 7 2022, 4:12 AM

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

For example, LLVM coding standards recommends using early exits: https://llvm.org/docs/CodingStandards.html#use-early-exits-and-continue-to-simplify-code

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.