This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Fix a false positive in bugprone-assignment-in-if-condition
ClosedPublic

Authored by njames93 on Aug 27 2022, 3:08 AM.

Details

Summary

Fixed a false positive where a lambda expression in the condition which contained an assignement would trigger a warning.
Fixes #56729

Diff Detail

Event Timeline

njames93 created this revision.Aug 27 2022, 3:08 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 27 2022, 3:08 AM
njames93 requested review of this revision.Aug 27 2022, 3:08 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 27 2022, 3:08 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
gribozavr2 accepted this revision.Aug 27 2022, 4:59 AM
This revision is now accepted and ready to land.Aug 27 2022, 4:59 AM

IMO you have just introduced a bug, not fixed one.
A lambda expression in an if statement condition clause is exactly the kind of thing that this checker was designed to flag.
The BARR group coding guideline, with which this is intended to comply in spirit, makes no exception for lambda expressions. (see 8.2c of https://barrgroup.com/sites/default/files/barr_c_coding_standard_2018.pdf)

It would seem to me that rather than this change as a default behavior, it would be better if @njames93 created an option flag for this checker which enabled this exclusion as a special case. This would preserve the intent of the original check, while also allowing the use case which njames93 has in mind.

Is it possible to retroactively reject/un-commit a contribution?

IMO you have just introduced a bug, not fixed one.
A lambda expression in an if statement condition clause is exactly the kind of thing that this checker was designed to flag.
The BARR group coding guideline, with which this is intended to comply in spirit, makes no exception for lambda expressions. (see 8.2c of https://barrgroup.com/sites/default/files/barr_c_coding_standard_2018.pdf)

It would seem to me that rather than this change as a default behavior, it would be better if @njames93 created an option flag for this checker which enabled this exclusion as a special case. This would preserve the intent of the original check, while also allowing the use case which njames93 has in mind.

Is it possible to retroactively reject/un-commit a contribution?

Granted that the coding guidelines this was intended for are designed for writing embedded C code, there was never any need to consider lambdas. As such, this change just makes the check a little bit more useful on C++ codebases.
Going by the name of this, it gives the impression that we are trying to detect bugs where an equality comparison was intended, or handle the case when an assignment isn't executed due to things like short circuiting rules. Neither of these use cases are expected when the assignment appears in the body of a lambda
I'd be happy to support this as an option though.