This is an archive of the discontinued LLVM Phabricator instance.

new clang-tidy checker for assignments within condition clause of if statement
ClosedPublic

Authored by dodohand on Jun 6 2022, 7:39 AM.

Details

Summary

new clang-tidy checker for assignments within the condition clause of an 'if' statement.

Diff Detail

Event Timeline

dodohand created this revision.Jun 6 2022, 7:39 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 6 2022, 7:39 AM
dodohand requested review of this revision.Jun 6 2022, 7:39 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 6 2022, 7:39 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
dodohand edited the summary of this revision. (Show Details)
gribozavr2 added inline comments.
clang-tools-extra/docs/clang-tidy/checks/misc-assignment-in-if-clause.rst
9 ↗(On Diff #434472)

Why not improve -Wparentheses to catch these cases too?

dodohand added inline comments.Jun 6 2022, 8:08 AM
clang-tools-extra/docs/clang-tidy/checks/misc-assignment-in-if-clause.rst
9 ↗(On Diff #434472)

IMHO, -Wparentheses isn't broken. Its "disable warning by an extra set of parentheses" behavior has been around for a long time and is common across gcc and clang... it just isn't what is called for when trying to ensure that accidental assignments aren't occurring in even slightly complicated condition statements, or when trying to comply with BARR-group coding standard.

gribozavr2 added inline comments.Jun 6 2022, 8:32 AM
clang-tools-extra/clang-tidy/misc/AssignmentInIfClauseCheck.cpp
39 ↗(On Diff #434472)

Please follow Clang's message style. The message should start with a lowercase letter and should not end with a period. Suggested fixes (since there are multiple) should be in notes.

warning: an assignment within an 'if' condition is bug-prone
note: if it should be an assignment, move it out of the 'if' condition
note: if it is meant to be an equality check, change '=' to '=='

Also consider adding a fixit to the second note.

clang-tools-extra/docs/clang-tidy/checks/misc-assignment-in-if-clause.rst
3 ↗(On Diff #434472)

WDYT about bugprone-assignment-in-if-condition?

6–9 ↗(On Diff #434472)
9 ↗(On Diff #434472)

Oh, I see -- it wasn't clear to me that the silencing through the extra set of parens was specifically not desirable for you.

I suggested some edits to the documentation to directly highlight the difference.

I also suggest changing the example below to explicitly mention what assignments are flagged by -Wparentheses and which are flagged by this check.

10 ↗(On Diff #434472)
17 ↗(On Diff #434472)

The "static volatile" part is not relevant to the example, and it might distract the reader, suggesting that something is special about such variables.

clang-tools-extra/test/clang-tidy/checkers/misc-assignment-in-if-clause.cpp
68 ↗(On Diff #434472)

Please clang-format the test.

dodohand added inline comments.Jun 6 2022, 8:54 AM
clang-tools-extra/clang-tidy/misc/AssignmentInIfClauseCheck.cpp
39 ↗(On Diff #434472)

Will adjust the messages.

I'm specifically reluctant to add a fixit to this, as I'm not sure how I can tell an intended assignment (for which the fix would be moving the assignment out of the if clause) apart from an accidental assignment (for which the fix is changing '=' to '=='). Thoughts?

clang-tools-extra/docs/clang-tidy/checks/misc-assignment-in-if-clause.rst
3 ↗(On Diff #434472)

I'm in favor of that categorization - it reflects how I feel about having assignments within an 'if' condition. I'll change it accordingly

dodohand updated this revision to Diff 434610.Jun 6 2022, 2:15 PM

renamed from misc-assignment-in-if-clause to bugprone-assignment-in-if-condition per review input

Adjusted documentation per review input.

dodohand marked 7 inline comments as done.Jun 6 2022, 2:17 PM

@gribozavr2 , have I successfully addressed the points you brought up?

Hi @alexfh , @aaron.ballman, Could one of you have a look at this? I'd like to get it moving toward resolution and don't know who else ought to do a review...

gribozavr2 accepted this revision.Jul 5 2022, 9:29 AM

LGTM with a few minor edits.

Do you have commit access?

clang-tools-extra/clang-tidy/bugprone/AssignmentInIfConditionCheck.cpp
20–22
33–35

No need to repeat the code in comments.

clang-tools-extra/test/clang-tidy/checkers/bugprone-assignment-in-if-condition.cpp
2–5
This revision is now accepted and ready to land.Jul 5 2022, 9:29 AM
gribozavr2 added inline comments.Jul 5 2022, 9:30 AM
clang-tools-extra/test/clang-tidy/checkers/bugprone-assignment-in-if-condition.cpp
85
dodohand updated this revision to Diff 442338.Jul 5 2022, 9:43 AM

Addressing comments from @gribozavr2 identifying comments needing update/removal/improvement.

dodohand marked 4 inline comments as done.Jul 5 2022, 9:44 AM

Addressed comments from gribozavr2

Hi @gribozavr2, I don't have commit access... I'll address the merge issue ASAP though.

Thanks! I'll commit this for you once you upload a refreshed patch (that applies to the main branch cleanly).

dodohand updated this revision to Diff 442340.Jul 5 2022, 9:56 AM

Updated to merge successfully into latest version of main branch

dodohand updated this revision to Diff 442358.Jul 5 2022, 10:58 AM

somehow, clang-format needed to be re-run.