This patch introduces a new warning flag -Wtautological-negation-compare grouped in -Wtautological-compare that warns on the use of && or || operators against a variable and its negation.
e.g. x || !x and !x && x
This also makes the -Winfinite-recursion diagnose more cases.
Details
Diff Detail
Event Timeline
clang/lib/Analysis/CFG.cpp | ||
---|---|---|
1113–1114 |
Seems like a nice idea to me, thanks!
Do you have any numbers on how often this fires in practice, and what the true positive rate is? (Build some large-ish open source project with this, and see what it finds.)
Did you verify that this has negligible compile time impact for building a large-ish project?
With that, looks good.
clang/lib/Analysis/CFG.cpp | ||
---|---|---|
1096 | Do you want to IgnoreParens() on the ! subexpr too? |
Do you have any numbers on how often this fires in practice, and what the true positive rate is? (Build some large-ish open source project with this, and see what it finds.)
I built rui314/mold and saw no new warnings emitted from this change. I searched several repositories to find only one instance in CBLAS code that this change could be relevant.
https://github.com/Reference-LAPACK/lapack/blob/c57e36a43bb1c25c7ec15a3c84f4800942a5ca43/CBLAS/src/cblas_xerbla.c#L69-L70
I'm not sure what exactly they are doing there, but the comment (Force link of our F77 error handler) suggests it is intentional, so it could be counted as false positive. But, I don't see any reason for not doing if (0) instead.
Did you verify that this has negligible compile time impact for building a large-ish project?
About the compile time impact, I don't expect this patch to have a noticeable change. The changes in this patch does not contain heavy computations like AST traversals. At least the build times of rui314/mold by clang ToT and patched clang did not have noticeable difference.
clang/lib/Analysis/CFG.cpp | ||
---|---|---|
1096 | I wanted to, but it looks like most codes containing parentheses in negation subexpression are macros, so now I don't think we need it. |
Generally LGTM, just a minor suggestion on slightly rewording the diagnostics and combining them.
clang/include/clang/Basic/DiagnosticSemaKinds.td | ||
---|---|---|
9726–9731 | Let's combine these if we can. I'm not 100% certain that %select is going to like &&||| though, so if there's an issue, separate is also fine. |
Thanks for the review
clang/include/clang/Basic/DiagnosticSemaKinds.td | ||
---|---|---|
9726–9731 | %select(&&|||)0 doesn't seem to work as we want it to, so I kept it separated |
Let's combine these if we can. I'm not 100% certain that %select is going to like &&||| though, so if there's an issue, separate is also fine.