This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Fix false positives in `misc-redundant-expression` check
ClosedPublic

Authored by fwolff on Mar 20 2022, 6:13 PM.

Details

Summary

It turns out that the right-hand sides of overloaded comparison operators are currently not checked at all in misc-redundant-expression, leading to such false positives as given in #54011 or here. This patch fixes this.

Diff Detail

Event Timeline

fwolff created this revision.Mar 20 2022, 6:13 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 20 2022, 6:13 PM
fwolff requested review of this revision.Mar 20 2022, 6:13 PM
aaron.ballman added inline comments.Mar 21 2022, 9:25 AM
clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp
605

This seems incorrect to me -- the LHS and RHS can be swapped along with which operator is used and still achieve the same semantic results.

While playing around to test the behavior we currently have, I was a bit surprised at the difference in results here: https://godbolt.org/z/hE4qfca1b.

Also, do we need to bind? Nothing seems to look at that binding that I can see.

fwolff updated this revision to Diff 417054.Mar 21 2022, 12:44 PM
fwolff marked an inline comment as done.Mar 21 2022, 12:48 PM

Thanks for the quick review @aaron.ballman! I think I've addressed your comments.

clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp
605

This seems incorrect to me -- the LHS and RHS can be swapped along with which operator is used and still achieve the same semantic results.

Fixed.

While playing around to test the behavior we currently have, I was a bit surprised at the difference in results here: https://godbolt.org/z/hE4qfca1b.

All four conditions get a warning with my changes. I've added your example to the test file.

Also, do we need to bind? Nothing seems to look at that binding that I can see.

Yes, it is hard to see, but here the constant is actually read.

aaron.ballman accepted this revision.Mar 22 2022, 7:45 AM

LGTM! Please also add a release note for the fix when you land it.

clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp
605

Yes, it is hard to see, but here the constant is actually read.

Oh, thanks for the info!

This revision is now accepted and ready to land.Mar 22 2022, 7:45 AM
This revision was automatically updated to reflect the committed changes.
fwolff marked an inline comment as done.