Search code examples
objective-cautomatic-ref-countingclangclang-static-analyzer

How can clang warn me if I fail to initialize a variable while using ARC?


I forgot to initialize a local variable, and I got no warning when I used it. Since I'm using ARC, the variable was initialized to nil, so no harm was done, but I still want a warning when I used an uninitialized value. If I disable ARC, I get the warning I expect.

NSString *foo;
NSString *baz;
if (bar) {
    foo = @"fizz";
} else {
    foo = @"buzz";
}
NSLog(@"foo: %@", foo);  // foo: (fizz|buzz)
NSLog(@"baz: %@", baz);  // baz: (null)

Without ARC:

/blah/blah/blah/Blah.m:14:18: note: initialize the variable 'foo' to silence this warning
NSString *foo;
             ^

--EDIT--

I've figured out how to make uninitialized values impossible using local blocks. This obviates the need for the warning.


Solution

  • With ARC, pointers to Objective C objects are automatically initialized to nil, so there is no "uninitialized value" which the compiler can warn about.