This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Resolve readability-else-after-return false positive for constexpr if.
ClosedPublic

Authored by curdeius on Oct 17 2018, 6:28 AM.

Details

Summary

It fixes the false positive when using constexpr if and where else cannot be removed:

Example:

if constexpr (sizeof(int) > 4)
  // ...
  return /* ... */;
else // This else cannot be removed.
  // ...
  return /* ... */;

Diff Detail

Event Timeline

curdeius created this revision.Oct 17 2018, 6:28 AM

I think it would be good to add some more explanation as to *why* that else has to be kept.

I think it would be good to add some more explanation as to *why* that else has to be kept.

Do you mean add a comment in the code or just an explanation for the review?

For the latter, e.g.:

// unrelated types:
struct type_a {};
struct type_b {};

auto f()
{
  if constexpr(condition)
  {
    return type_a{};
  }
  else
  {
    return type_b{};
  }
}

In this case removing else will just provoke a compilation error. There may be some cases where you may remove else though.

Actually, I can make it an option for this check to skip or not constexpr ifs, WDYT?

I think it would be good to add some more explanation as to *why* that else has to be kept.

Do you mean add a comment in the code or just an explanation for the review?
In this case removing else will just provoke a compilation error. There may be some cases where you may remove else though.

I was indeed talking about the review, thank you.

Actually, I can make it an option for this check to skip or not constexpr ifs, WDYT?

I'm not sure, maybe this shouldn't be an option.

aaron.ballman added inline comments.Oct 18 2018, 7:14 AM
test/clang-tidy/readability-else-after-return-if-constexpr.cpp
10

Please add some of the warning text -- any warning will match this.

24

This seems unnecessary.

curdeius updated this revision to Diff 170101.Oct 18 2018, 9:10 AM

Applied changes as per comments.

Hmm, the latest patch only seems to have the changes to the test but not the implementation?

curdeius updated this revision to Diff 170152.Oct 19 2018, 12:21 AM

Fixed diff.

This revision is now accepted and ready to land.Oct 19 2018, 8:15 AM
This revision was automatically updated to reflect the committed changes.