This patch introduces a new warning flag -Wcomparison-op-parentheses in -Wparentheses to issue warnings with its fixit hint for comparison operators within another comparison operator.
This fixes https://github.com/llvm/llvm-project/issues/60256
Differential D142800
[Clang][Diagnostic] Add `-Wcomparison-op-parentheses` to warn on chained comparisons hazohelet on Jan 27 2023, 10:48 PM. Authored by
Details
This patch introduces a new warning flag -Wcomparison-op-parentheses in -Wparentheses to issue warnings with its fixit hint for comparison operators within another comparison operator. This fixes https://github.com/llvm/llvm-project/issues/60256
Diff Detail
Unit Tests Event Timeline
Comment Actions
My 2c is that -Wparentheses is already a very stylistic warning (even correct code, without knowing about this esoteric/specific suppression style of adding parens, is likely to be caught by this general class of diagnostic) so I'm pretty apprehensive about opening it up further - but data would be the decider. Running the proposed slices (different operators in different orders, etc) of warning over some broad codebases to get a sense of the false positive rate would be really helpful/necessary to decide which things to include and exclude. Comment Actions Address comments from erichkeane:
Implement my proposal for fix-it hint:
Comment Actions I'm reasonably happy here. I think the chained-comparisons getting the && suggestion is nice. However in each case, the diagnostic is REALLY poor. It isn't really clear to most folks (barely clear to me!) that '<' within '>' really means anything. I'd love if we could spend some time brainstorming some better diagnostic wording that makes it clear what the problem is.
Comment Actions
As for the false positive rate, I have checked for instances of this warning in the codebases for 'oneapi-src/oneTBB', 'rui314/mold', and 'microsoft/lightgbm', but did not find any new cases. I set the diagnostic disabled by default for compatibility with gcc. Considering the test against tensorflow above, it would be too noisy if we turned on the suggest-parentheses diagnostic by default (From my best guess, it would generate at least 18 new instances of warning on the tensorflow build for the six lines).
Note that the 'chaining comparisons' in the document are
URL: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0893r0.html Although this paper is five years old now, I think we can conclude chaining relational operators are bug-prone and should be diagnosed by default. tensorflow/tensorflow newly-warned lines:
Comment Actions Thank you for doing the extra testing! Sorry for the delayed response, this review fell off my radar for a bit.
Thank you for reporting this, that's really good information.
I tend to agree -- I was searching around sourcegraph (https://sourcegraph.com/search?q=context:global+lang:C+lang:C%2B%2B+%5BA-Za-z0-9_%5D%2B%5Cs*%28%3D%3D%7C%21%3D%7C%3C%3D%7C%3E%3D%29%5Cs*%5BA-Za-z0-9_%5D%2B%5Cs*%28%3D%3D%7C%21%3D%7C%3C%3D%7C%3E%3D%29&patternType=regexp&sm=1&groupBy=repo) and I can't find a whole lot of evidence for chained operators outside of comments. Comment Actions Update the differential
Comment Actions Thanks for the review and the sourcegraph search! Comment Actions Thought to point out that there is a clang-tidy check in review doing the similar thing - https://reviews.llvm.org/D144429. |
Spit-balling wording ideas:
'%0' results in a boolean value which is then compared using '%1'; did you mean a three-way comparison instead?
comparing the boolean result of '%0' with '%1' does not perform a three-way comparison
Also, why are these diagnostics off by default? Do we have some idea as to the false positive rate?