[PATCH] Enhance clang-tidy readability-simplify-boolean-expr to handle 'if (e) return true; return false; '

Richard legalize at xmission.com
Sun May 24 13:40:13 PDT 2015


After sorting out some things with the clang tree for testing, I discovered that the code had errors.  This diff corrects them.

Specifically `compoundStmt(hasAnySubstatement(ifStmt()))` will always match the first `if` statement in a compound statement.  The matcher will not be invoked for each `if` statement in the block.  So when a block contains chained `if` statements like:

  if (i < 5) return false;
  if (i > 10) return true;
  return false;

The first `if` statement would have been matched, but then determined that it did not immediately precede the `return` statement and the entire block would be left alone.  The consequence is that I can use a matcher that finds all compound statements containing both an `if` statement and a `return` statement, but I then have to iterate over the compound statement manually to locate the `if` immediately preceding the `return`.  Test cases have been added to cover this situation.  Running the check on clang/LLVM code now does the sensible thing.


http://reviews.llvm.org/D9810

Files:
  clang-tidy/readability/SimplifyBooleanExprCheck.cpp
  clang-tidy/readability/SimplifyBooleanExprCheck.h
  test/clang-tidy/readability-simplify-bool-expr.cpp

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D9810.26388.patch
Type: text/x-patch
Size: 11938 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150524/5b54ad2b/attachment.bin>


More information about the cfe-commits mailing list