Monad.NET.Analyzers 1.1.2

There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Monad.NET.Analyzers --version 1.1.2
                    
NuGet\Install-Package Monad.NET.Analyzers -Version 1.1.2
                    
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="Monad.NET.Analyzers" Version="1.1.2">
  <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="Monad.NET.Analyzers" Version="1.1.2" />
                    
Directory.Packages.props
<PackageReference Include="Monad.NET.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 Monad.NET.Analyzers --version 1.1.2
                    
#r "nuget: Monad.NET.Analyzers, 1.1.2"
                    
#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 Monad.NET.Analyzers@1.1.2
                    
#: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=Monad.NET.Analyzers&version=1.1.2
                    
Install as a Cake Addin
#tool nuget:?package=Monad.NET.Analyzers&version=1.1.2
                    
Install as a Cake Tool

Monad.NET.Analyzers

Roslyn analyzers for Monad.NET that catch common misuse patterns at compile-time.

Installation

dotnet add package Monad.NET.Analyzers

Diagnostics

Safety Warnings

ID Title Severity Description
MNT001 Unchecked Unwrap Warning Detects Unwrap() without checking IsSome/IsOk first
MNT005 Discarded Monad Warning Detects when Result/Option return values are ignored
MNT006 Throw in Match Warning Throwing in None/Err branch defeats pattern matching purpose
MNT007 Nullable to Some Warning Passing nullable to Option.Some will throw at runtime
MNT010 Null Comparison Warning Comparing Option (a struct) to null is always wrong
MNT011 Unwrap in LINQ Warning Using Unwrap inside LINQ can throw unexpectedly

Style Suggestions

ID Title Severity Description
MNT002 Redundant Map Chain Info Consecutive Map calls can be combined
MNT003 Map+GetOrElse Info Map().GetOrElse() can be Match() or MapOr()
MNT004 FlatMap to Map Info FlatMap(x => Some(f(x))) can be Map(f)
MNT008 Filter Constant Info Filter(_ => true) or Filter(_ => false) is redundant
MNT009 Map Identity Info Map(x => x) has no effect
MNT012 Double Negation Info !option.IsNone should be option.IsSome

Examples

MNT001: Unchecked Unwrap

// Warning: Option should be checked before calling Unwrap()
var value = option.Unwrap();

// Safe alternatives:
if (option.IsSome) { var value = option.Unwrap(); }
var value = option.GetOrElse(defaultValue);
var value = option.Match(some: x => x, none: () => default);

MNT006: Throw in Match

// Warning: Throwing in None branch defeats pattern matching purpose
var value = option.Match(
    some: x => x,
    none: () => throw new Exception("No value")); // Use Expect() instead

// Better:
var value = option.Expect("No value");

MNT007: Nullable to Some

string? name = GetName(); // Might be null

// Warning: Will throw if name is null
var option = Option<string>.Some(name);

// Safe:
var option = Option<string>.FromNullable(name);

MNT010: Null Comparison

// Warning: Option is a struct, never null
if (option != null) { ... }

// Correct:
if (option.IsSome) { ... }

MNT003: Map+GetOrElse

// Info: Can be simplified
var result = option.Map(x => x.ToString()).GetOrElse("default");

// Simplified:
var result = option.Match(
    some: x => x.ToString(),
    none: () => "default");
// Or:
var result = option.MapOr("default", x => x.ToString());

MNT004: FlatMap to Map

// Info: FlatMap returning Some can be Map
var result = option.FlatMap(x => Option<int>.Some(x * 2));

// Simplified:
var result = option.Map(x => x * 2);

MNT012: Double Negation

// Info: Double negation
if (!option.IsNone) { ... }

// Simplified:
if (option.IsSome) { ... }

Configuration

Configure severity in .editorconfig:

[*.cs]
# Disable specific rule
dotnet_diagnostic.MNT001.severity = none

# Make suggestion an error
dotnet_diagnostic.MNT002.severity = error

# Set all Monad.NET analyzers to warning
dotnet_analyzer_diagnostic.category-Monad.NET.severity = warning

Suppression

#pragma warning disable MNT001
var value = option.Unwrap(); // I checked elsewhere
#pragma warning restore MNT001
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
2.0.0-beta.3 86 2/23/2026
2.0.0-beta.2 84 2/2/2026
2.0.0-beta.1 76 2/1/2026
1.1.2 171 1/25/2026
1.1.1 133 1/7/2026
1.1.0 127 12/30/2025
1.0.0 133 12/28/2025