Esox.SharpAndRusty.Analyzers 1.0.0

dotnet add package Esox.SharpAndRusty.Analyzers --version 1.0.0
                    
NuGet\Install-Package Esox.SharpAndRusty.Analyzers -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Esox.SharpAndRusty.Analyzers" Version="1.0.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Esox.SharpAndRusty.Analyzers" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Esox.SharpAndRusty.Analyzers">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Esox.SharpAndRusty.Analyzers --version 1.0.0
                    
#r "nuget: Esox.SharpAndRusty.Analyzers, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Esox.SharpAndRusty.Analyzers@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Esox.SharpAndRusty.Analyzers&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Esox.SharpAndRusty.Analyzers&version=1.0.0
                    
Install as a Cake Tool

Esox.SharpAndRusty.Analyzers

A Roslyn analyzer that enforces proper handling of Result<T, E> and Option<T> types from the Esox.SharpAndRusty library, similar to Rust's #[must_use] attribute.

Features

ESOX1001: Result value must be used

This analyzer warns when a method returning Result<T, E> is called but its return value is ignored. This prevents developers from accidentally ignoring potential errors.

❌ Bad:

public Result<int, string> GetValue() => Result<int, string>.Ok(42);

public void Process()
{
    GetValue(); // Warning ESOX1001
}

✅ Good:

public void Process()
{
    var result = GetValue();
    
    // Or use it directly
    result.Match(
        ok => Console.WriteLine($"Success: {ok}"),
        err => Console.WriteLine($"Error: {err}"));
        
    // Or explicitly discard if you really don't care (not recommended)
    _ = GetValue();
}

ESOX1002: Option value must be used

This analyzer warns when a method returning Option<T> is called but its return value is ignored.

❌ Bad:

public Option<string> GetName() => new Option<string>.Some("John");

public void Process()
{
    GetName(); // Warning ESOX1002
}

✅ Good:

public void Process()
{
    var nameOption = GetName();
    
    // Handle it properly
    if (nameOption is Option<string>.Some(var name))
    {
        Console.WriteLine($"Name: {name}");
    }
}

Installation

The analyzer is available as a separate optional package:

# First, install the core library (required)
dotnet add package Esox.SharpAndRusty

# Then, optionally install the analyzer for compile-time warnings
dotnet add package Esox.SharpAndRusty.Analyzers

Once installed, the analyzer will start warning about unhandled Result and Option types immediately in your IDE and during builds.

Rationale

In Rust, the Result type is annotated with #[must_use], which causes the compiler to emit a warning if a Result is not used. This is a powerful safety feature that prevents developers from accidentally ignoring errors.

This analyzer brings the same safety to C# code using the Esox.SharpAndRusty library, helping create more robust applications.

Configuration

The analyzers are enabled by default with Warning severity. You can configure them in your .editorconfig or globalconfig file:

# Promote to error
dotnet_diagnostic.ESOX1001.severity = error
dotnet_diagnostic.ESOX1002.severity = error

# Or disable (not recommended)
dotnet_diagnostic.ESOX1001.severity = none
dotnet_diagnostic.ESOX1002.severity = none

Suppression

If you have a legitimate reason to not handle a Result or Option (rare!), you can:

  1. Explicitly discard it:

    _ = GetValue(); // Shows intent
    
  2. Suppress the warning:

    #pragma warning disable ESOX1001
    GetValue();
    #pragma warning restore ESOX1001
    

How It Works

The analyzer detects when:

  • A method call returns Result<T, E> or Option<T>
  • The return value is not:
    • Assigned to a variable
    • Returned from the calling method
    • Used as an argument
    • Used in pattern matching
    • Used in any other meaningful way

Contributing

Contributions are welcome! Please see the main repository for contribution guidelines.

License

This analyzer is part of the Esox.SharpAndRusty project and is licensed under the same terms.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 94 5/16/2026