CLANG WARN NULLABLE TO NONNULL CONVERSION

Xcode 7 includes a project setting named "Apple LLVM 7.0 - Warnings - All languages > Incorrect Uses of Nullable Values", or CLANG_WARN_NULLABLE_TO_NONNULL_CONVERSION.

You can pass it as a compiler flag using -Wnullable-to-nonnull-conversion.

What does it do?

To demonstrate, consider the following command-line app:

#import <Foundation/Foundation.h>

void welcomeToClowntown(NSString *_Nonnull greeting) {
  NSLog(@"%@, welcome to clowntown!", greeting);
}

int main(int argc, const char * argv[]) {
  // Warning: Null passed to a callee that requires 
  //          a non-null argument
  welcomeToClowntown(nil); 
  return 0;
}

Notice that we're passing a nil literal to our function welcomeToClowntown(), even though it takes a parameter of type NSString *_Nonnull. This always emits a warning, regardless of whether CLANG_WARN_NULLABLE_TO_NONNULL_CONVERSION is enabled.

But what about the following case?

$3+ patron-only content

Examples of when `CLANG_WARN_NULLABLE_TO_NONNULL_CONVERSION` produces additional warnings.

I am a patronBecome a patron

When should I use it?

I recommend enabling this setting before adding nullability identifiers to your Objective-C headers. That way, as you add restrictions on the non-nullability of your interfaces, the compiler will warn you of callsites that violate these restrictions.

Is it fool-proof?

No. Like many compiler warnings, it can be side-stepped:

$3+ patron-only content

Examples of when `CLANG_WARN_NULLABLE_TO_NONNULL_CONVERSION` fails to prevent programming errors.

I am a patronBecome a patron

Why shouldn't I enable this warning?

$3+ patron-only content

A reason why not to enable `CLANG_WARN_NULLABLE_TO_NONNULL_CONVERSION`.

I am a patronBecome a patron

UPDATE: Clang static analyzer improvements

The latest version of the Clang static analyzer was released on October 28, 2015. It catches the "side-step" I point out above:

$3+ patron-only content

An example of how the Clang static analyzer catches errors in a way similar to `CLANG_WARN_NULLABLE_TO_NONNULL_CONVERSION`.

I am a patronBecome a patron