blob: 23cb42bc625c595fddaeb1a790c4e1c82a3b8ba1 [file] [log] [blame]
Jordan Rose9f63a452012-06-08 21:14:25 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
2
3typedef 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
30void testComparisonsWithFixits(id obj) {
31 if (obj == @"") return; // expected-error{{direct comparison of a string literal is not allowed; use -isEqual: instead}}
32 if (obj != @"") return; // expected-error{{direct comparison of a string literal is not allowed; use -isEqual: instead}}
33 if (@"" == obj) return; // expected-error{{direct comparison of a string literal is not allowed; use -isEqual: instead}}
34 if (@"" == @"") return; // expected-error{{direct comparison of a string literal is not allowed; use -isEqual: instead}}
35
36 if (@[] == obj) return; // expected-error{{direct comparison of an array literal is not allowed; use -isEqual: instead}}
37 if (@{} == obj) return; // expected-error{{direct comparison of a dictionary literal is not allowed; use -isEqual: instead}}
38 if (@12 == obj) return; // expected-error{{direct comparison of a numeric literal is not allowed; use -isEqual: instead}}
39 if (@1.0 == obj) return; // expected-error{{direct comparison of a numeric literal is not allowed; use -isEqual: instead}}
40 if (@__objc_yes == obj) return; // expected-error{{direct comparison of a numeric literal is not allowed; use -isEqual: instead}}
41 if (@(1+1) == obj) return; // expected-error{{direct comparison of a boxed expression is not allowed; use -isEqual: instead}}
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
54void testComparisonsWithoutFixits() {
55 // All of these verifications use regex form to ensure we /don't/ append
56 // "use -isEqual: instead" to any of these.
57
58 if ([BaseObject new] == @"") return; // expected-error-re{{direct comparison of a string literal is not allowed$}}
59
60 if ([BadEqualReturnString new] == @"") return; // expected-error-re{{direct comparison of a string literal is not allowed$}}
61 if ([BadEqualArgString new] == @"") return; // expected-error-re{{direct comparison of a string literal is not allowed$}}
62
63 if (@"" < @"") return; // expected-error-re{{direct comparison of a string literal is not allowed$}}
64 if (@"" > @"") return; // expected-error-re{{direct comparison of a string literal is not allowed$}}
65 if (@"" <= @"") return; // expected-error-re{{direct comparison of a string literal is not allowed$}}
66 if (@"" >= @"") return; // expected-error-re{{direct comparison of a string literal is not allowed$}}
67}
68