Index: clang/lib/Sema/SemaExprCXX.cpp =================================================================== --- clang/lib/Sema/SemaExprCXX.cpp +++ clang/lib/Sema/SemaExprCXX.cpp @@ -4136,6 +4136,7 @@ } case ICK_Boolean_Conversion: + DiagnoseAssignmentAsCondition(From->IgnoreImpCasts()); // Perform half-to-boolean conversion via float. if (From->getType()->isHalfType()) { From = ImpCastExprToType(From, Context.FloatTy, CK_FloatingCast).get(); Index: clang/test/Sema/assigment_in_bool_context.cpp =================================================================== --- /dev/null +++ clang/test/Sema/assigment_in_bool_context.cpp @@ -0,0 +1,57 @@ +// RUN: %clang_cc1 -Wparentheses -fsyntax-only -verify %s + +bool warn1(int x) { + return x = 0; // expected-warning {{using the result of an assignment as a condition without parentheses}} + // expected-note@-1 {{place parentheses around the assignment to silence this warning}} + // expected-note@-2 {{use '==' to turn this assignment into an equality comparison}} +} + +bool warn2(int x, bool a, bool b) { + return x = 0 || (a && b); // expected-warning {{using the result of an assignment as a condition without parentheses}} + // expected-note@-1 {{place parentheses around the assignment to silence this warning}} + // expected-note@-2 {{use '==' to turn this assignment into an equality comparison}} +} + +bool warn3(int x, bool a) { + return x = 0 || a; // expected-warning {{using the result of an assignment as a condition without parentheses}} + // expected-note@-1 {{place parentheses around the assignment to silence this warning}} + // expected-note@-2 {{use '==' to turn this assignment into an equality comparison}} +} + +bool warn4(int x) { + return bool(x = 0); // expected-warning {{using the result of an assignment as a condition without parentheses}} + // expected-note@-1 {{place parentheses around the assignment to silence this warning}} + // expected-note@-2 {{use '==' to turn this assignment into an equality comparison}} +} + +int warn5(int x) { + return bool(x = 0); // expected-warning {{using the result of an assignment as a condition without parentheses}} + // expected-note@-1 {{place parentheses around the assignment to silence this warning}} + // expected-note@-2 {{use '==' to turn this assignment into an equality comparison}} +} + +bool warn6(int x, int a) { + return x = a; // expected-warning {{using the result of an assignment as a condition without parentheses}} + // expected-note@-1 {{place parentheses around the assignment to silence this warning}} + // expected-note@-2 {{use '==' to turn this assignment into an equality comparison}} +} + +int nowarn1(int x) { + return (x = 0); +} + +int nowarn2(int x) { + return x = 0; +} + +int nowarn3(int x) { + return x == 0; +} + +bool nowarn4(int x) { + return x == 0; +} + +void nowarn5(int x) { + return void(x = 0); +}