<!--
{
  "documentType" : "article",
  "framework" : "Xcode",
  "identifier" : "/documentation/Xcode/nonnull-return-value-violation",
  "metadataVersion" : "0.1.0",
  "role" : "article",
  "title" : "Nonnull return value violation"
}
-->

# Nonnull return value violation

Detects when a function incorrectly returns null.

## Overview

Use this check to detect when a function with the `returns_nonnull` attribute, or a function with a return type that has the `_Nonnull` annotation, returns null. Available in Xcode 9 and later.

> Note: The nonnull violation check for return types with the `_Nonnull` annotation is off by default. You can turn it on by enabling the `-fsanitize=nullability-return` compiler flag.

### Violation of the nonnull attribute for a function in C

In the following code, there is a violation of the `returns_nonnull` attribute of the `nonnull_returning_function` function:

```occ
__attribute__((returns_nonnull)) int *nonnull_returning_function(int *p) {
    return p; // Warning: NULL can be returned here
}
nonnull_returning_function(NULL); // Error: nonnull return value attribute violation
```

#### Solution

Correct logic errors, add any necessary null guards to the function, or remove the `returns_nonnull` attribute and rework the function caller logic accordingly.

### Violation of the nonnull annotation for a return type in C

The following code violates the `_Nonnull` annotation of the return type for the `nonnull_returning_function` function:

```occ
int *_Nonnull nonnull_returning_function(int *p) {
    return p; // Warning: NULL can be returned here
}
nonnull_returning_function(NULL); // Error: nonnull return value attribute violation
```

#### Solution

Correct logic errors, add any necessary null guards to the function, or remove the `_Nonnull` annotation and rework the function caller logic accordingly.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)