|
| 1 | +// RUN: %clang_cc1 -x c++ -fsyntax-only -Wparentheses -verify %s |
| 2 | +// RUN: %clang_cc1 -x c -fsyntax-only -Wparentheses -verify %s |
| 3 | + |
| 4 | +// INFO: This warning is only issued for C and C++. |
| 5 | +// This file is executed 3 times once for each language mention in the run-command. |
| 6 | + |
| 7 | +#ifdef __cplusplus |
| 8 | + |
| 9 | +// Do not emit the warning for compound-assignments. |
| 10 | +bool f(int x) { return x = 0; } // expected-warning {{suggest parentheses around assignment used as truth value}} |
| 11 | +bool f2(int x) { return x += 0; } |
| 12 | + |
| 13 | +bool f3(bool x) { return x = 0; } |
| 14 | + |
| 15 | +void test() { |
| 16 | + int x; |
| 17 | + if (x = 0) {} // expected-warning {{suggest parentheses around assignment used as truth value}} |
| 18 | + if (x = 4 && x){} // expected-warning {{suggest parentheses around assignment used as truth value}} |
| 19 | + |
| 20 | + (void)bool(x = 1); // expected-warning {{suggest parentheses around assignment used as truth value}} |
| 21 | + (void)(bool)(x = 1); |
| 22 | + |
| 23 | + // This should still emit since the RHS is casted to `int` before being casted back to `bool`. |
| 24 | + (void)bool(x = false); // expected-warning {{suggest parentheses around assignment used as truth value}} |
| 25 | + |
| 26 | + // Should only issue warning once, even if multiple implicit casts. |
| 27 | + // FIXME: This only checks that warning occurs not how often. |
| 28 | + (void)bool(bool(x = 1)); // expected-warning {{suggest parentheses around assignment used as truth value}} |
| 29 | + (void)bool(int(bool(x = 1))); // expected-warning {{suggest parentheses around assignment used as truth value}} |
| 30 | + (void)bool(int(x = 1)); |
| 31 | + |
| 32 | + bool _a = x = 3; // expected-warning {{suggest parentheses around assignment used as truth value}} |
| 33 | + |
| 34 | + // Shouldn't warn for above cases if parentheses were provided. |
| 35 | + if ((x = 0)) {} |
| 36 | + (void)bool((x = 1)); |
| 37 | + bool _b= (x = 3); |
| 38 | +} |
| 39 | + |
| 40 | +#elif defined(__OBJC__) |
| 41 | + |
| 42 | +// NOTE: This warning shouldn't affect Objective-C |
| 43 | +#import <Foundation/Foundation.h> |
| 44 | + |
| 45 | +BOOL f(int x) { return x = 0; } |
| 46 | +BOOL f2(int x) { return x += 0; } |
| 47 | + |
| 48 | +BOOL f3(BOOL x) { return x = 0; } |
| 49 | + |
| 50 | +void test() { |
| 51 | + int x; |
| 52 | + |
| 53 | + if (x = 0) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} |
| 54 | + if (x = 4 && x){} // expected-warning {{using the result of an assignment as a condition without parentheses}} |
| 55 | + |
| 56 | + (void)(BOOL)(x = 1); |
| 57 | + (void)(BOOL)(int)(x = 1); |
| 58 | + |
| 59 | + BOOL _a = x = 3; |
| 60 | + |
| 61 | + if ((x = 0)) {} |
| 62 | + (void)BOOL((x = 1)); |
| 63 | + BOOL _b= (x = 3); |
| 64 | +} |
| 65 | + |
| 66 | +#else |
| 67 | + |
| 68 | +// NOTE: Don't know if tests allow includes. |
| 69 | +#include <stdbool.h> |
| 70 | + |
| 71 | +// Do not emit the warning for compound-assignments. |
| 72 | +bool f(int x) { return x = 0; } // expected-warning {{suggest parentheses around assignment used as truth value}} |
| 73 | +bool f2(int x) { return x += 0; } |
| 74 | + |
| 75 | +bool f3(bool x) { return x = 0; } |
| 76 | + |
| 77 | +void test() { |
| 78 | + int x; |
| 79 | + |
| 80 | + // This should emit the `warn_condition_is_assignment` warning since |
| 81 | + // C doesn't do implicit conversion booleans for conditions |
| 82 | + if (x = 0) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
| 83 | + // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 84 | + // expected-note{{place parentheses around the assignment to silence this warning}} |
| 85 | + if (x = 4 && x){} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
| 86 | + // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 87 | + // expected-note{{place parentheses around the assignment to silence this warning}} |
| 88 | + |
| 89 | + (void)(bool)(x = 1); |
| 90 | + (void)(bool)(int)(x = 1); |
| 91 | + |
| 92 | + |
| 93 | + bool _a = x = 3; // expected-warning {{suggest parentheses around assignment used as truth value}} |
| 94 | + |
| 95 | + // Shouldn't warn for above cases if parentheses were provided. |
| 96 | + if ((x = 0)) {} |
| 97 | + bool _b = (x = 3); |
| 98 | +} |
| 99 | + |
| 100 | +#endif |
0 commit comments