This is an archive of the discontinued LLVM Phabricator instance.

Fix 31480 - warn more aggressively when assignments get used as a boolean values
Needs ReviewPublic

Authored by incertia on Apr 6 2018, 11:18 PM.

Details

Reviewers
rjmccall
rsmith
Summary

To my understanding, the contents of a condition will always be implicitly casted to an rvalue before evaluation. Thus, moving the check to PerformImplicitConversion should be correct. We can also check the more general case here of general boolean expressions. This should catch a few more cases vs gcc. The only examples that I can come up with that will bypass this check would be

bool & f(bool &b) { return b = true; }
f(b = false); // b = false is an lvalue, the reference passed into the function is an lvalue, and the reference returned out is an lvalue, which causes no warnings.

Clearly passing in the result of an assignment to a function is not very good, so we should probably have a separate check for that. On the other hand, I have no idea if we should even warn on returning the (boolean) result of assigning to a boolean reference or how such a check would be implemented, outside of brute force within ActOnReturnStmt, which would collide with the warnings in PerformImplicitConversion.

Diff Detail

Event Timeline

incertia created this revision.Apr 6 2018, 11:18 PM
incertia updated this revision to Diff 141477.Apr 6 2018, 11:19 PM

Pulled the check outside of the case statement.