Rules / Correctness / unnecessary-nullable

unnecessary-nullable

cross-file/correctness/unnecessary-nullable
recommendedcross-file
Same as check-unnecessary-nullable · Dart Code Metrics
Cross-file rule. This rule runs in falcon's whole-project pass, analyzing relationships across every file rather than one file at a time. Configure it under the top-level cross-file key, and expect it to do more work on large projects than a single-file rule.

Report nullable parameters of private declarations that never receive null.

Flags a nullable parameter (T?) of a private (_-prefixed) function or method when no call site anywhere in the project passes it null, and its body never assigns null to it either. Because the declaration is private, every one of its call sites is visible in the analyzed set, which is what makes the conclusion sound; the ? then widens the type for a value that is always non-null, so callers and the body carry null handling that can never trigger. An argument counts as non-null only when proven so — a literal, a new or arithmetic expression, or a call whose declared return type the cross-file index resolves as non-nullable. Anything uncertain (an ambiguous name shared by multiple declarations, a tear-off reference, an argument that might be null) suppresses the report. This is a cross-file rule: it runs in the cross-file pass over the whole analyzed file set and is configured under the top-level cross-file section rather than linter.

Invalid

caller.dartdart
import 'service.dart';

void driver() {
  final s = Service();
  s._never(1);
  s._never(2);
  s._sometimes(1);
  s._sometimes(null); // makes _sometimes genuinely nullable → not flagged
  s._assigns('x');
  s._ambiguous(3);
  _topNever('hello');
  Other()._ambiguous(4);
  _viaProvenNonNull(nonNull());
  _viaNullableReturn(maybeNull());
}

No diagnostics in this file.

How to configure

Set the severity of unnecessary-nullable in your falcon.json under the cross-file section:

falcon.jsonjson
{
  "cross-file": {
    "rules": {
      "correctness": {
        "unnecessary-nullable": "error"
      }
    }
  }
}