UnionGenerator.Analyzers.CodeFixes
0.1.0
dotnet add package UnionGenerator.Analyzers.CodeFixes --version 0.1.0
NuGet\Install-Package UnionGenerator.Analyzers.CodeFixes -Version 0.1.0
<PackageReference Include="UnionGenerator.Analyzers.CodeFixes" Version="0.1.0"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="UnionGenerator.Analyzers.CodeFixes" Version="0.1.0" />
<PackageReference Include="UnionGenerator.Analyzers.CodeFixes"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add UnionGenerator.Analyzers.CodeFixes --version 0.1.0
#r "nuget: UnionGenerator.Analyzers.CodeFixes, 0.1.0"
#:package UnionGenerator.Analyzers.CodeFixes@0.1.0
#addin nuget:?package=UnionGenerator.Analyzers.CodeFixes&version=0.1.0
#tool nuget:?package=UnionGenerator.Analyzers.CodeFixes&version=0.1.0
UnionGenerator.Analyzers.CodeFixes
Roslyn code fix providers for UnionGenerator analyzers. Provides automatic code fixes and refactorings for common union-related issues detected at compile time.
🚀 Quick Start
1. Install
dotnet add package UnionGenerator.Analyzers.CodeFixes
2. That's it!
Code fixes are automatically available in your IDE. Build your project and you'll see lightbulb icons (💡) in the editor for fixable diagnostics.
📚 Features
✅ Automatic Code Fixes
One-click fixes for analyzer warnings. Just press Ctrl+. (or Cmd+.) in your IDE.
✅ Safe Refactoring
Code fixes are validated and tested to preserve semantics.
✅ Zero Configuration
Works out-of-the-box with UnionGenerator.Analyzers. No setup required.
✅ IDE Integration
Full integration with Visual Studio, VS Code (with Roslyn extension), and Rider.
🔧 Supported Fixes
Fix UG4010: Map Union to IActionResult
Diagnostic: Union result returned directly from controller without mapping.
Before:
public Result<User, NotFoundError> GetUser(int id)
{
return _service.GetUser(id);
}
After (Generated):
public IActionResult GetUser(int id)
{
return _service.GetUser(id).ToActionResult();
}
What Changed:
- Return type changed from
Result<T, E>toIActionResult .ToActionResult()call added (maps union to HTTP response)
When Applied:
- Lightbulb appears on method declaration
- Action: "Convert to IActionResult and add ToActionResult()"
- Applied to single method or all methods in controller via batch fix
Fix UG4011: Add Status Code to Error Type
Diagnostic: Error type used in union doesn't have explicit status code.
Before:
public class CustomError
{
public string Message { get; set; }
}
After (Option 1 - Attribute):
[UnionStatusCode(400)]
public class CustomError
{
public string Message { get; set; }
}
After (Option 2 - Property):
public class CustomError
{
public string Message { get; set; }
public int StatusCode => 400;
}
What Gets Fixed:
- Adds
[UnionStatusCode(400)]attribute (or configurable code) - OR adds
StatusCodeproperty returning HTTP code - Requires manual selection of which approach (attribute vs property)
When Applied:
- Lightbulb appears on error class declaration
- Options shown: "Add [UnionStatusCode] attribute" or "Add StatusCode property"
Fix UG4012: Make Status Code Explicit
Diagnostic: Status code inferred by convention, but explicit is better.
Before:
public class NotFoundError { }
After:
[UnionStatusCode(404)]
public class NotFoundError { }
What Changed:
- Adds explicit
[UnionStatusCode]based on naming convention - Maps:
*NotFound*→ 404,*BadRequest*→ 400, etc.
When Applied:
- Hidden by default (info diagnostic)
- Enable in .editorconfig if team prefers explicit code
- Lightbulb shows: "Make status code explicit"
⚙️ Configuration
Enable Fixes in IDE
Visual Studio 2022+:
- Tools → Options → Text Editor → C# → Code Analysis → General
- Enable "Enable analyzers from NuGet packages"
- Rebuild project
VS Code with Roslyn Extension:
- Install "Roslyn Analyzers" extension
- Reload window
- Fixes appear automatically
JetBrains Rider:
- Settings → Tools → Roslyn Analyzers
- Enable "UnionGenerator.Analyzers.CodeFixes"
- Rebuild project
Configure Severity for Batch Fix
In .editorconfig:
# Show all fixable diagnostics
[**/*.cs]
dotnet_diagnostic.UG4010.severity = suggestion
dotnet_diagnostic.UG4011.severity = suggestion
dotnet_diagnostic.UG4012.severity = suggestion
# Or disable specific fixes
dotnet_diagnostic.UG4010.severity = silent # Don't suggest IActionResult conversion
Scope of Fixes
Single Fix: Apply to one occurrence
- Right-click diagnostic → "Quick Fix" → applies to current line
Scope Fix: Apply to current file
- In some IDEs: "Fix all in document"
Project Fix: Apply to entire project
- Visual Studio: right-click solution, "Run Code Analysis and Fix"
- CLI:
dotnet fix --verbosity diag
📋 Common Scenarios
Scenario 1: Bulk Convert Controllers to IActionResult
Problem: All your controller methods return Result<T, E> instead of IActionResult.
Solution:
- Open
.csprojand add (temporary):<PropertyGroup> <TreatWarningsAsErrors>true</TreatWarningsAsErrors> </PropertyGroup> - Build project (forces all UG4010 to appear)
- Visual Studio → Analyze → Run Code Analysis → Fix Code
- Remove
TreatWarningsAsErrorsafter batch fix
Result: All controller methods now return IActionResult with .ToActionResult() calls.
Scenario 2: Standardize Error Status Codes
Problem: Some error types have explicit [UnionStatusCode], others rely on convention.
Solution:
- In
.editorconfig, enable UG4012:dotnet_diagnostic.UG4012.severity = suggestion - Build project
- Apply fix "Make status code explicit" to all types
- All errors now have explicit attributes
Result: Clear, consistent error status codes across codebase.
Scenario 3: Add Missing Status Codes to New Errors
Problem: You create a new error type but forget to add status code.
Solution:
- Build project (UG4011 warning appears)
- Click lightbulb (💡) in editor
- Choose "Add [UnionStatusCode] attribute"
- Editor prompts for HTTP code
- Code is inserted automatically
Result: Error type is immediately production-ready.
🔍 How Code Fixes Work
Detection Phase
Code fix provider listens for these diagnostics:
UG4010- Union not mapped to IActionResultUG4011- Error lacks status codeUG4012- Convention override recommended
Analysis Phase
For each diagnostic, the code fix:
- Parses the syntax tree
- Finds the target method/class
- Analyzes current code structure
- Determines appropriate fix
Generation Phase
Code is generated and inserted:
- Preserves original formatting and style
- Maintains indentation
- Respects existing attributes/properties
- Updates using statements if needed
Validation Phase
Generated code is validated:
- Syntax is valid C#
- No naming conflicts
- Type references are correct
- Compilation succeeds
🛠️ Advanced Usage
Programmatic Code Fixing
If you want to apply fixes programmatically (e.g., in a build step):
// Using Roslyn CodeFixProvider API
var workspace = MSBuildWorkspace.Create();
var project = await workspace.OpenProjectAsync("MyProject.csproj");
foreach (var document in project.Documents)
{
var semanticModel = await document.GetSemanticModelAsync();
var diagnostics = await document.GetDiagnosticsAsync();
foreach (var diagnostic in diagnostics)
{
if (diagnostic.Id == "UG4010")
{
// Apply fix programmatically
// Details: See Roslyn SDK docs
}
}
}
Note: This requires the Roslyn SDK and is beyond typical usage.
📊 Fix Application Workflow
Build Project
↓
Analyzer detects issue (UG4010, UG4011, UG4012)
↓
Code fix provider registers fix
↓
IDE shows lightbulb (💡)
↓
Developer clicks "Quick Fix" or "Apply Fix"
↓
Code is generated and inserted
↓
Project recompiles
↓
✅ Issue resolved
🎯 Best Practices
✅ DO
- Apply fixes as soon as diagnostics appear (don't let them pile up)
- Review generated code before committing (should be clean and idiomatic)
- Use batch fixes for large-scale refactoring (but review carefully)
- Keep analyzers enabled in CI/CD (prevents regression)
- Document why a fix was declined (if you suppress a diagnostic)
❌ DON'T
- Apply all fixes without reviewing (they're usually correct, but review first)
- Ignore batches of diagnostics and then try to apply all at once
- Use fixes as a substitute for proper error handling design (fix the root cause)
- Suppress diagnostics without documenting why
- Disable code analysis in production builds
🔗 Related Packages
- UnionGenerator.Analyzers: Diagnostic rules (UG4010, UG4011, UG4012)
- UnionGenerator.AspNetCore: Provides the
.ToActionResult()extension that fixes depend on
🚨 Troubleshooting
Lightbulb Not Appearing
Problem: No fix lightbulb shows up for diagnostics
Solution:
- Ensure
UnionGenerator.Analyzers.CodeFixesNuGet package is installed - Rebuild project:
dotnet clean && dotnet build - Reload IDE window (Visual Studio: File → Recent Projects → Reopen)
- Check IDE settings: Code analyzers must be enabled
Fix Not Working as Expected
Problem: Applied fix produces incorrect code
Solution:
- Undo the fix (Ctrl+Z)
- Check C# language version (11+ recommended)
- Verify
.editorconfigisn't overriding fix behavior - Report issue with minimal reproduction case
Status Code Not Suggested
Problem: Fix for UG4011 doesn't suggest the right status code
Solution:
- UG4011 fix defaults to 400 (Bad Request)
- Manually change to correct code if different
- Or use naming convention (NotFound → 404) and apply UG4012 fix instead
Batch Fix Partial Application
Problem: Batch fix applies to some files but not others
Solution:
- May be due to syntax errors in some files
- Fix syntax errors first:
dotnet build - Re-run batch fix operation
- Check build output for details
📄 License
MIT License - Part of UnionGenerator project
✨ Summary
| Feature | Benefit |
|---|---|
| One-Click Fixes | Faster refactoring |
| Batch Operations | Bulk modernization |
| IDE Integrated | Works in all major editors |
| Safe Refactoring | Preserves semantics |
| Zero Config | Works out-of-box |
Quick action: Install the package and press Ctrl+. on any analyzer warning to see available fixes! 🚀
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- UnionGenerator.Analyzers (>= 0.1.0)
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 |
|---|---|---|
| 0.1.0 | 95 | 1/21/2026 |