This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Misc redundant expressions checker updated for confused operators
ClosedPublic

Authored by barancsuk on Oct 31 2017, 5:16 AM.

Details

Summary

Redundant Expression Checker is updated to target expressions with suspicious operator usage.

The checker finds the following two cases, where operators are suspected to be mistaken for another operator, that is considered to be more effective in that particular expression.

1.) Left shift operator might be confused with right shift operator
The expression always evaluates to zero, because Y is shifted left with more bits that the leftmost effective (1) bit of 0xff.

Y = (Y << 8) & 0xff;

// Possible intended expression:
Y = (Y >> 8) & 0xff;

2.) Bitwise NOT mistaken for logical NOT
A logical not is applied to a bitwise operator expression with integer constant operands, then assigned to an int.

int K = !(1 | 2 | 4);

// Possible intended expression:
int K = ~(1 | 2 | 4);

Diff Detail

Event Timeline

barancsuk created this revision.Oct 31 2017, 5:16 AM
xazax.hun added inline comments.Oct 31 2017, 5:32 AM
clang-tidy/misc/RedundantExpressionCheck.cpp
698

Are you sure you wanted hasDescendant here? Would has be sufficient?

1040

I think either all of the binds should be successful in your matcher expression or none of them. So I think this might better be transformed into an assert.

1045

The first check should be transformed into an assert.

1051

Same as above.

barancsuk updated this revision to Diff 120965.Oct 31 2017, 5:50 AM
barancsuk marked 4 inline comments as done.

Address review comments

xazax.hun added inline comments.Oct 31 2017, 6:02 AM
clang-tidy/misc/RedundantExpressionCheck.cpp
1040

I think looking up these AST nodes could be postponed until they are needed, so in case an early return is triggered, no lookup necessary.

barancsuk updated this revision to Diff 120973.Oct 31 2017, 6:10 AM
barancsuk marked an inline comment as done.

Postpone AST node lookup

Could you make sure that we do not offer a fixit when the location of the negation (!) is from a macro expansion?
Also, it might be a good idea to have some tests where some of the types do not match.

For example:
Y = (Y << 8) & 0xff;
Where Y is a long.
And also tests where one of the constants are longs.

clang-tidy/misc/RedundantExpressionCheck.cpp
29

Include <cmath> instead.

1025

This snippet is too long, probably not clang formatted.

barancsuk updated this revision to Diff 121513.Nov 3 2017, 10:45 AM

Add new tests for unmatched types.
Do not provide fixit for ! operator from macros.

barancsuk marked 2 inline comments as done.Nov 3 2017, 10:46 AM
xazax.hun accepted this revision.Nov 6 2017, 6:19 AM

Otherwise, from a nit that is probably an artifact from a rebase it looks good to me. But please wait for one more reviewer to accept this just to be sure.

clang-tidy/misc/RedundantExpressionCheck.cpp
135–136

Is this change intended or an artifact from a rebase?

This revision is now accepted and ready to land.Nov 6 2017, 6:19 AM
aaron.ballman edited edge metadata.Nov 6 2017, 12:09 PM

Some quick concerns (I'll give it a more thorough look when I have a spare moment).

clang-tidy/misc/RedundantExpressionCheck.cpp
29

Please keep the include list sorted appropriately.

1030

Extra preceding space in the diagnostic.

1061

Extra preceding space here as well.

barancsuk updated this revision to Diff 122672.Nov 13 2017, 9:19 AM
barancsuk marked 4 inline comments as done.

Address review comments, note modifications in ReleaseNotes

aaron.ballman added inline comments.Nov 15 2017, 11:17 AM
clang-tidy/misc/RedundantExpressionCheck.cpp
1030–1031

How about: "ineffective logical negation operator used; did you mean '~'?"

1061–1062

Diagnostics should not be complete sentences with full stops. How about: "ineffective bitwise and operation; did you mean '>>'?"

One thing that's strange about this is that the diagnostic location is under the bitwise and rather than the shift operation. Is that intentional?

test/clang-tidy/misc-redundant-expression.cpp
710

Ineffective

barancsuk updated this revision to Diff 123346.Nov 17 2017, 8:17 AM
barancsuk marked 3 inline comments as done.

Address review comments

barancsuk added inline comments.Nov 17 2017, 8:18 AM
clang-tidy/misc/RedundantExpressionCheck.cpp
135–136

This change should have been introduced in the Misc redundant expressions checker updated for overloaded operators patch, so it must be an artifact from a rebase.

1061–1062

The main idea behind the diagnostic placement was that the first part of the sentence refers to the "bitwise and operation" rather than the shift operation.
However, after the first part of the message the diagnostic also asks about the shift operator, so I will easily change the placement, if you think that it would help the user understand the message better.

aaron.ballman added inline comments.Nov 18 2017, 11:59 AM
clang-tidy/misc/RedundantExpressionCheck.cpp
1061–1062

It's odd because the ineffective part of the written operation is the &, but the presumed cause of it being ineffective is due to using << instead of >>. It might also be reasonable to presume the & is at fault and that the user meant to write | to flip some bits on. I wonder if the best approach is to highlight the & and drop the "did you mean" part, because it seems like multiple things could have been at fault.

What do you think?

barancsuk updated this revision to Diff 123604.Nov 20 2017, 8:59 AM
barancsuk marked an inline comment as done.

Drop first part of diagnostic message

barancsuk added inline comments.Nov 20 2017, 9:00 AM
clang-tidy/misc/RedundantExpressionCheck.cpp
1061–1062

It is a good point, that the ineffectiveness of the expression is due to the interaction of two operators, and it is unclear which one of the two is to blame.

I agree that the best way to approach this is to simply omit the second part of the diagnostic message and highlight the AND operation.

aaron.ballman added inline comments.Nov 20 2017, 10:17 PM
clang-tidy/misc/RedundantExpressionCheck.cpp
1061

Drop the full-stop from the end of the diagnostic.

test/clang-tidy/misc-redundant-expression.cpp
712

When you correct the diagnostic, you'll need to update the test cases to match.

barancsuk updated this revision to Diff 129630.Jan 12 2018, 7:46 AM

Remove full stop from end of diagnostic

barancsuk marked 2 inline comments as done.Jan 15 2018, 7:24 AM
barancsuk updated this revision to Diff 134044.Feb 13 2018, 8:25 AM

Rebase on master

barancsuk updated this revision to Diff 135638.Feb 23 2018, 7:24 AM

Rebase on master 2.0

xazax.hun closed this revision.Mar 6 2018, 3:06 AM

Hmm, it looks like these changes are already upstreamed. Probably as part of other patch or maybe just someone (probably me) forgot to put the proper revision in the commit message? I close this revision now.