Index: clang/lib/StaticAnalyzer/Checkers/BasicObjCFoundationChecks.cpp =================================================================== --- clang/lib/StaticAnalyzer/Checkers/BasicObjCFoundationChecks.cpp +++ clang/lib/StaticAnalyzer/Checkers/BasicObjCFoundationChecks.cpp @@ -109,6 +109,7 @@ mutable Selector SetObjectForKeySel; mutable Selector SetObjectForKeyedSubscriptSel; mutable Selector RemoveObjectForKeySel; + mutable Selector ObjectForKeyedSubscriptSel; void warnIfNilExpr(const Expr *E, const char *Msg, @@ -293,6 +294,8 @@ SetObjectForKeyedSubscriptSel = getKeywordSelector(Ctx, "setObject", "forKeyedSubscript"); RemoveObjectForKeySel = getKeywordSelector(Ctx, "removeObjectForKey"); + ObjectForKeyedSubscriptSel = + getKeywordSelector(Ctx, "objectForKeyedSubscript"); } if (S == DictionaryWithObjectForKeySel || S == SetObjectForKeySel) { @@ -303,6 +306,8 @@ Arg = 1; } else if (S == RemoveObjectForKeySel) { Arg = 0; + } else if (S == ObjectForKeyedSubscriptSel) { + Arg = 0; } } Index: clang/test/Analysis/NSContainers.m =================================================================== --- clang/test/Analysis/NSContainers.m +++ clang/test/Analysis/NSContainers.m @@ -67,6 +67,7 @@ - (NSUInteger)count; - (id)objectForKey:(id)aKey; - (NSEnumerator *)keyEnumerator; +- (id)objectForKeyedSubscript:(id)aKey; @end @@ -310,3 +311,13 @@ // here either. [subviews addObject:view]; // no-warning } + +void testNilSubscriptToDict(NSDictionary *d, NSString *s) { + if (s) {} + id obj = d[s]; // expected-warning{{Value argument to 'objectForKeyedSubscript:' cannot be nil}} +} + +void testNilSubscriptToMutableDict(NSMutableDictionary *d, NSString *s) { + if (s) {} + id obj = d[s]; // expected-warning{{Value argument to 'objectForKeyedSubscript:' cannot be nil}} +}