R06WARNINGfunction

Return Type Nullable

Flags when a function's return type gains null or undefined as a possible value. Callers that do not check for null may crash at runtime.


Applies to

TypeScriptPythonGoJavaRust

Why it matters

When a function that previously always returned a value starts returning null, any caller that directly accesses properties on the return value will crash: getUser().name throws TypeError: Cannot read property 'name' of null if getUser() returns null.

Example

Before (always returns User)
// users.ts
export function getUser(id: string): User {
  return db.findUser(id)!; // always returns
}
After (may return null)
// users.ts (WARNING — return may be null)
export function getUser(id: string): User | null {
  return db.findUser(id); // may return null
}

What you see in the terminal

$ npx dg check

  [WARNING] getUser (signature_change)
  src/users.ts:2
  Return type now includes null. Callers not checking for null may crash.

How detection works

The classifier compares the old and new return type strings. If the new return type contains 'null', 'undefined', or 'None' and the old one did not, the rule fires.

Real-world scenario

A previous version used a non-null assertion (`!`) to guarantee a return value. A new version removes this assertion for correctness, meaning the function can now return null when a user is not found. All callers doing `getUser(id).email` need to add null checks.

Edge cases

  • Going from T to T | undefined is also caught by this rule
  • Python Optional[T] is equivalent to T | None
  • Rust's raw type to Option is a breaking change in Rust land, handled differently