Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

[clang-tidy] misc-redundant-expression assumes operator sides are equivalent in if-constexpr #43587

Open Quuxplusone opened 4 years ago

Quuxplusone commented 4 years ago
Bugzilla Link PR44617
Status NEW
Importance P normal
Reported by Dennis Felsing (dennis.felsing@sap.com)
Reported on 2020-01-22 01:41:41 -0800
Last modified on 2020-01-23 01:48:00 -0800
Version unspecified
Hardware PC Linux
CC alexfh@google.com, djasper@google.com, etienneb+llvm@google.com, eugene.zelenko@gmail.com, klimek@google.com, N.James93@hotmail.co.uk
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also
Hi,

Starting with clang-tidy 10 I'm seeing misc-redundant-expression detecting
equivalent operator sides inside of if-constexpr even though they are not
actually equivalent:

$ clang-tidy --version
LLVM (http://llvm.org/):
  LLVM version 10.0.175git
  Optimized build.
  Default target: x86_64-unknown-linux-gnu
  Host CPU: broadwell
$ cat .clang-tidy
Checks: misc-redundant-expression
$ cat x.cpp
struct S
{
    int a;
    int b;
};

int main(int /*argc*/, char** /*argv*/)
{
    if constexpr((sizeof(S) % sizeof(int) != 0) || (alignof(S) % alignof(int) != 0)) {
    }

    return 0;
}

$ clang-tidy --extra-arg=-std=c++1z x.cpp
/home/d067158/x.cpp:12:49: warning: both sides of operator are equivalent
[clang-tidy-misc-redundant-expression]
    if constexpr((sizeof(S) % sizeof(int) != 0) || (alignof(S) % alignof(int) != 0)) {
                                                ^

Best regards
Dennis
Quuxplusone commented 4 years ago
I'm gonna guess the cause of this is the compiler can fold away the expression
to

>if constexpr(false || false) {}

Which is why the expression is redundant.
Would be interesting to see how it handles templated code where S is a typename.
Quuxplusone commented 4 years ago

In the same way. When we saw this in production it was actually with a template.

Quuxplusone commented 4 years ago

Then this bug only concerns template instantiations and in reality has nothing to do with it being in a constexpr if condition