Skip to content

Roslyn analyzers for ErrorOr<T> to enforce safe access patterns. Prevents runtime InvalidOperationException by ensuring IsError is checked before accessing values or errors. Supports modern C# pattern matching, switch expressions, and control flow tracking.

License

Notifications You must be signed in to change notification settings

Joaolfelicio/Result.Analyzers

Repository files navigation

Result.Analyzers

Build Status NuGet Version License: MIT

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.

Supported Libraries

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.

Analyzers

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; // Safe

Ensures 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; // Safe

Ensures 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; // Safe

Features

Result.Analyzers utilizes the Roslyn IOperation tree to provide robust state tracking across various C# constructs:

  • Control Flow Analysis: Tracks object state through if/else blocks, 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.

Installation

Install the package via the .NET CLI:

dotnet add package Result.Analyzers

License

This project is licensed under the MIT License.

About

Roslyn analyzers for ErrorOr<T> to enforce safe access patterns. Prevents runtime InvalidOperationException by ensuring IsError is checked before accessing values or errors. Supports modern C# pattern matching, switch expressions, and control flow tracking.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages