Skip to content

False positives when using misc-redundant-expression clang-tidy checker. #118885

@earnol

Description

@earnol

The misc-redundant-expression produces a warning with latest trunk build where it should not.
In the example below two different defines are being compared. Those defines different, yet happens to have the same value due to compile defines configuration.
The warning generated is:

<source>:13:20: warning: both sides of operator are equivalent [misc-redundant-expression]
   13 |   if (VAL_1 != VAL_2)

where operator != is highlighted.
I believe the programmer intention is very clear here as programmer wants to have function foo return 0 or 1 depending on the define passed into the compilation line, so the condition is not redundant.

I have created a minimal reproducer for the issue:

//#define A
#ifndef A
#define VAL_1 2
#else
#define VAL_1 3
#endif

#define VAL_2 2

unsigned foo() {
  if (VAL_1 != VAL_2)
  {
    return 0;
  }
  else
  {
    return 1;
  }
}


int main() {
  return foo();
}

The example is also available on the godbolt: https://godbolt.org/z/3sd6qTfTP.

It look like a bug for me because in file clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp in function
void RedundantExpressionCheck::check(const MatchFinder::MatchResult &Result)
the check areExprsFromDifferentMacros(LhsConst, RhsConst, Result.Context) is called explicitly to prevent exactly this false positive.
Here is the link to the source:

if (areExprsFromDifferentMacros(LhsConst, RhsConst, Result.Context) ||

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions