Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s |
| 2 | |
| 3 | typedef unsigned char BOOL; |
| 4 | |
| 5 | @interface BaseObject |
| 6 | + (instancetype)new; |
| 7 | @end |
| 8 | |
| 9 | @interface NSObject : BaseObject |
| 10 | - (BOOL)isEqual:(id)other; |
| 11 | @end |
| 12 | |
| 13 | @interface NSNumber : NSObject |
| 14 | + (NSNumber *)numberWithInt:(int)value; |
| 15 | + (NSNumber *)numberWithDouble:(double)value; |
| 16 | + (NSNumber *)numberWithBool:(BOOL)value; |
| 17 | @end |
| 18 | |
| 19 | @interface NSArray : NSObject |
| 20 | + (id)arrayWithObjects:(const id [])objects count:(unsigned long)cnt; |
| 21 | @end |
| 22 | |
| 23 | @interface NSDictionary : NSObject |
| 24 | + (id)dictionaryWithObjects:(const id [])objects forKeys:(const id [])keys count:(unsigned long)cnt; |
| 25 | @end |
| 26 | |
| 27 | @interface NSString : NSObject |
| 28 | @end |
| 29 | |
| 30 | void testComparisonsWithFixits(id obj) { |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame^] | 31 | if (obj == @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}} expected-note{{use 'isEqual:' instead}} |
| 32 | if (obj != @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}} expected-note{{use 'isEqual:' instead}} |
| 33 | if (@"" == obj) return; // expected-warning{{direct comparison of a string literal has undefined behavior}} expected-note{{use 'isEqual:' instead}} |
| 34 | if (@"" == @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}} expected-note{{use 'isEqual:' instead}} |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 35 | |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame^] | 36 | if (@[] == obj) return; // expected-warning{{direct comparison of an array literal has undefined behavior}} expected-note{{use 'isEqual:' instead}} |
| 37 | if (@{} == obj) return; // expected-warning{{direct comparison of a dictionary literal has undefined behavior}} expected-note{{use 'isEqual:' instead}} |
| 38 | if (@12 == obj) return; // expected-warning{{direct comparison of a numeric literal has undefined behavior}} expected-note{{use 'isEqual:' instead}} |
| 39 | if (@1.0 == obj) return; // expected-warning{{direct comparison of a numeric literal has undefined behavior}} expected-note{{use 'isEqual:' instead}} |
| 40 | if (@__objc_yes == obj) return; // expected-warning{{direct comparison of a numeric literal has undefined behavior}} expected-note{{use 'isEqual:' instead}} |
| 41 | if (@(1+1) == obj) return; // expected-warning{{direct comparison of a boxed expression has undefined behavior}} expected-note{{use 'isEqual:' instead}} |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | |
| 45 | @interface BadEqualReturnString : NSString |
| 46 | - (void)isEqual:(id)other; |
| 47 | @end |
| 48 | |
| 49 | @interface BadEqualArgString : NSString |
| 50 | - (BOOL)isEqual:(int)other; |
| 51 | @end |
| 52 | |
| 53 | |
| 54 | void testComparisonsWithoutFixits() { |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame^] | 55 | if ([BaseObject new] == @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}} |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 56 | |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame^] | 57 | if ([BadEqualReturnString new] == @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}} |
| 58 | if ([BadEqualArgString new] == @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}} |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 59 | |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame^] | 60 | if (@"" < @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}} |
| 61 | if (@"" > @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}} |
| 62 | if (@"" <= @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}} |
| 63 | if (@"" >= @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}} |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 64 | } |
| 65 | |