llvm-project/clang/test/SemaObjC/objc-container-subscripting-2.m
Aaron Ballman 7deaeb2a05 Use functions with prototypes when appropriate; NFC
A significant number of our tests in C accidentally use functions
without prototypes. This patch converts the function signatures to have
a prototype for the situations where the test is not specific to K&R C
declarations. e.g.,

  void func();

becomes

  void func(void);

This is the fourth batch of tests being updated (there are a significant
number of other tests left to be updated).
2022-02-07 15:29:36 -05:00

50 lines
1.7 KiB
Objective-C

// RUN: %clang_cc1 -fsyntax-only -verify %s
typedef unsigned int size_t;
@protocol P @end
@interface NSMutableArray
- (id)objectAtIndexedSubscript:(size_t)index;
- (void)setObject:(id)object atIndexedSubscript:(size_t)index;
@end
@interface NSMutableDictionary
- (id)objectForKeyedSubscript:(id)key;
- (void)setObject:(id)object forKeyedSubscript:(size_t)key;
@end
id func(void) {
NSMutableArray *array;
float f;
array[f] = array; // expected-error {{indexing expression is invalid because subscript type 'float' is not an integral or Objective-C pointer type}}
return array[3.14]; // expected-error {{indexing expression is invalid because subscript type 'double' is not an integral or Objective-C pointer type}}
}
void test_unused(void) {
NSMutableArray *array;
array[10]; // expected-warning {{container access result unused - container access should not be used for side effects}}
NSMutableDictionary *dict;
dict[array]; // expected-warning {{container access result unused - container access should not be used for side effects}}
}
void testQualifiedId(id<P> qualifiedId) {
id object = qualifiedId[10]; // expected-error {{expected method to read array element not found on object of type 'id<P>'}}
qualifiedId[10] = qualifiedId; // expected-error {{expected method to write array element not found on object of type 'id<P>'}}
}
void testUnqualifiedId(id unqualId) {
id object = unqualId[10];
unqualId[10] = object;
}
@protocol Subscriptable
- (id)objectAtIndexedSubscript:(size_t)index;
- (void)setObject:(id)object atIndexedSubscript:(size_t)index;
@end
void testValidQualifiedId(id<Subscriptable> qualifiedId) {
id object = qualifiedId[10];
qualifiedId[10] = object;
}