misc-redundant-expression: expression with calls to a non-pure function is considered as always-true
pavelkryukov opened this issue · comments
Bugzilla Link | 36505 |
Version | unspecified |
OS | Windows NT |
Attachments | Example of always-true false positive with calls to a non-pure function |
CC | @EugeneZelenko,@Xazax-hun |
Extended Description
Clang-Tidy produces a false positive warning about always-true expression (misc-redundant-expression check) if expression contains consequent calls to a non-pure function which have different return values:
$> clang-tidy -checks='*' ldfile.c -- -Wall
15 warnings generated.
./ldfile.c:11:32: warning: logical expression is always true [misc-redundant-expression]
if ((token = yylex()) != 1 || (token = yylex()) != 2) {
^
Suppressed 14 warnings (14 in non-user code).
$> clang ldfile.c -Wall -Wextra && ./a.out
false
The code is taken from GNU Binutils (/ld/ldfile.c) source file.
Reproduced with Clang-Tidy 11.0.0: https://godbolt.org/z/6nWMMa
Same thing for Clang-Tidy 10.
Godbolt example: https://godbolt.org/z/w7PggB
Reproduced with Clang-Tidy 9.0.1
Still there in Clang-Tidy 7.0.0
Issue is reproducable in Clang-Tidy 6.0.0
@EugeneZelenko @Xazax-hun I’ll try to get more attention, as wrong fix-it can be generated because of this.
This is unrelated to #54011 and even intended to some extent, I believe: Almost every function call might have side effects (and misc-redundant-expression
does not look into the called function), but taking this into account would prevent many legitimate warnings (arising from typos etc.) from firing, so the false positives here are tolerated.
Instead, to fix your example, I'd focus on the assignments. I've opened D122535 to this end.
Thank you!