The result of a call that can return null is dereferenced without a check

tx0/csharp:null-dereference-across-callsallwarningreliability/null-safetydepth fastbug, deep

What it finds

FindUser(id).Name where FindUser returns null on some path, Environment.GetEnvironmentVariable("HOME").Trim(): a method returns null, and its caller dereferences the result without a test, which throws NullReferenceException.

Calls go to methods of the same file, by name and number of arguments (instance methods called through this or without a receiver, static methods by their type), and to methods of the base class library that return null when they find nothing, in the table models/null.txt. A method counts as returning null only where it writes null or returns the result of such a call; a result that is null only when an argument is does not count. What an async method returns applies where its call is awaited. Also reported: passing null, or such a result, to a parameter that the callee dereferences on every path before any test. Conditional access, ??, patterns, string.IsNullOrEmpty, ArgumentNullException.ThrowIfNull and the null-forgiving operator ! are understood. Findings inside a try with a handler for the call are left out. Calls of a method with overloads are not followed, because the overload is not known; HasValue, GetValueOrDefault and extension methods that accept null, such as AsSpan and IsNullOrEmpty, are no dereference.

From the rule's source, null_dereference_across_calls.rs.

Example from the rule's tests
using System;using System.Collections.Generic;using System.Threading.Tasks; namespace Shop; public class UserService{    private readonly Dictionary<string, User> users = new();     public User? Find(string id)    {        if (users.TryGetValue(id, out var user))        {            return user;        }        return null;    }     public string NameOf(string id)    {        return Find(id).Name;tx0 reports: UserService.Find(...) can return null    }     public string Conditional(string id)    {        return Find(id)?.Name ?? "";    }     public string Checked(string id)    {        var user = Find(id);        if (user is null)        {            throw new ArgumentException(id);        }        return user.Name;    }     public string Pattern(string id)    {        if (Find(id) is User u)        {            return u.Name;        }        return "";    }     public string Forgiven(string id)    {        return Find(id)!.Name;    }     public string Home()    {        return Environment.GetEnvironmentVariable("HOME").Trim();tx0 reports: Environment.GetEnvironmentVariable(...) can return null    }     public string HomeChecked()    {        var home = Environment.GetEnvironmentVariable("HOME");        if (string.IsNullOrEmpty(home))        {            return "/";        }        return home.Trim();    }     public async Task<User?> LoadAsync(string id)    {        await Task.Delay(1);        return null;    }     public async Task<string> LoadName(string id)    {        var user = await LoadAsync(id);        return user.Name;tx0 reports: UserService.LoadAsync(...) can return null    }     public Task<User?> Pending(string id)    {        var task = LoadAsync(id);        task.ConfigureAwait(false);        return task;    }} public class User{    public string Name { get; set; } = "";}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the all tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/csharp:null-dereference-across-calls" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/csharp:null-dereference-across-calls] reason="..."

tx0 explain tx0/csharp:null-dereference-across-calls shows the same in the terminal. Found a false positive? Report it.