Result.Analyzers is a suite of Roslyn analyzers that enforces safe access patterns for result-like types. It prevents runtime exceptions by ensuring that the state of an object is verified before accessing its value or associated errors.
The analyzer uses a configuration-driven architecture to support multiple result and option packages.
| Library | Supported Type | Status |
|---|---|---|
| ErrorOr | ErrorOr<T> |
Supported |
| Ardalis.Result | Result<T> |
Planned |
| FluentResults | Result<T> |
Planned |
Support for additional libraries can be added via configuration.
Ensures that .Value is accessed only after verifying the result is a success.
var result = GetUser();
return result.Value; // RA001: Unsafe access
if (!result.IsError)
return result.Value; // SafeEnsures that .FirstError is accessed only after verifying the result is a failure.
var result = GetUser();
return result.FirstError; // RA002: Unsafe access
if (result.IsError)
return result.FirstError; // SafeEnsures that .Errors is accessed only after verifying the result is a failure.
var result = GetUser();
return result.Errors; // RA003: Unsafe access
if (result is { IsError: true } failure)
return failure.Errors; // SafeResult.Analyzers utilizes the Roslyn IOperation tree to provide robust state tracking across various C# constructs:
- Control Flow Analysis: Tracks object state through
if/elseblocks, ternary operators, and early return patterns. - Pattern Matching: Supports property patterns (
is { IsError: false }) and switch expressions. - Logical Expressions: Handles compound conditions such as
if (res.IsSuccess && isValid). - Variable Tracking: Maintains state awareness when check results are stored in local boolean variables.
Install the package via the .NET CLI:
dotnet add package Result.AnalyzersThis project is licensed under the MIT License.