Skip to content

Add code fixes for MA0094-MA0097#1078

Merged
meziantou merged 7 commits intomainfrom
add-codefix-ma0094-0097
Mar 31, 2026
Merged

Add code fixes for MA0094-MA0097#1078
meziantou merged 7 commits intomainfrom
add-codefix-ma0094-0097

Conversation

@meziantou
Copy link
Copy Markdown
Owner

Why

These four equality/comparison rules were already enforced by diagnostics, but they did not provide automated fixes. This made adoption slower and left users to manually implement repetitive interface/member/operator boilerplate.

What changed

Implemented code fixes in EqualityShouldBeCorrectlyImplementedFixer for:

  • MA0094 (CompareTo(T) type should implement IComparable<T>)
  • MA0095 (type implementing IEquatable<T> should override Equals(object))
  • MA0096 (type implementing IComparable<T> should also implement IEquatable<T>)
  • MA0097 (types implementing comparable contracts should declare comparison/equality operators)

Key implementation details:

  • Extended FixableDiagnosticIds and RegisterCodeFixesAsync to register rule-specific actions.
  • Added helper logic to add missing interfaces while avoiding duplicates.
  • For MA0095, generated override bool Equals(object obj) delegating to IEquatable<T>.Equals.
  • For MA0096, added IEquatable<T> and generated Equals(T other) when missing, based on IComparable<T>.CompareTo.
  • For MA0097, generated any missing operators among <, <=, >, >=, ==, != using Comparer<T>.Default/EqualityComparer<T>.Default.

Tests

Added/updated targeted fixer tests:

  • EqualityShouldBeCorrectlyImplementedAnalyzerMA0094Tests
    • MA0094_CodeFix
    • MA0096_CodeFix
  • EqualityShouldBeCorrectlyImplementedAnalyzerMA0095Tests
    • MA0095_CodeFix
  • EqualityShouldBeCorrectlyImplementedAnalyzerMA0097Tests
    • MA0097_CodeFix

Validation performed:

  • dotnet test tests\Meziantou.Analyzer.Test\Meziantou.Analyzer.Test.csproj --filter "FullyQualifiedName~EqualityShouldBeCorrectlyImplementedAnalyzer"
  • dotnet test tests\Meziantou.Analyzer.Test\Meziantou.Analyzer.Test.csproj /p:RoslynVersion=roslyn4.2 --filter "FullyQualifiedName~EqualityShouldBeCorrectlyImplementedAnalyzer"

Documentation

Regenerated documentation via:

  • dotnet run --project src\DocumentationGenerator

This updated:

  • README.md
  • docs/README.md
  • docs/Rules/MA0094.md
  • docs/Rules/MA0095.md
  • docs/Rules/MA0096.md
  • docs/Rules/MA0097.md

Notably, rule tables now mark MA0094–MA0097 as having code fixes, and rule pages include the fixer source reference.

meziantou and others added 7 commits March 30, 2026 22:28
- Extend EqualityShouldBeCorrectlyImplementedFixer to support MA0094, MA0095, MA0096, and MA0097.

- Add targeted code fix tests for each rule and update generated documentation.

- Keep MA0077 behavior intact while sharing fixer implementation patterns.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Replace ImmutableArray collection expression with ImmutableArray.Create to support older Roslyn matrix builds.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Apply Simplifier.Annotation to generated parsed expressions and type nodes so global:: qualifiers are reduced in user-facing fixes.

Update corresponding code-fix test expectations and regenerate documentation.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Generate cleaner Equals implementations using direct method calls instead of explicit interface casts.

Keep simplifier annotations and update tests to reflect simplified output.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Generate MA0097 operators with simplified comparer/equality expressions and no global:: prefixes.

Update expected fixed code and keep docs in sync.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Replace parsed expression strings with explicit Roslyn syntax node construction in EqualityShouldBeCorrectlyImplementedFixer.

Keep generated output and tests stable while removing ParseExpression usage.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@meziantou meziantou marked this pull request as ready for review March 31, 2026 03:18
@meziantou meziantou enabled auto-merge (squash) March 31, 2026 03:18
@meziantou meziantou merged commit bec0f12 into main Mar 31, 2026
13 checks passed
@meziantou meziantou deleted the add-codefix-ma0094-0097 branch March 31, 2026 03:21
This was referenced Mar 31, 2026
This was referenced Apr 5, 2026
Chris-Wolfgang added a commit to Chris-Wolfgang/DbContextBuilder that referenced this pull request Apr 6, 2026
Updated [Azure.Identity](https://github.com/Azure/azure-sdk-for-net)
from 1.19.0 to 1.20.0.

<details>
<summary>Release notes</summary>

_Sourced from [Azure.Identity's
releases](https://github.com/Azure/azure-sdk-for-net/releases)._

## 1.20.0

## 1.20.0 (2026-03-30)

### Features Added

- Added a JSON schema segment to the NuGet package that provides
IntelliSense and validation for Azure.Identity credential configuration
in `appsettings.json`.

### Breaking Changes

- `AddAzureClient`, `AddKeyedAzureClient`, and `WithAzureCredential`
return type changed from `IHostApplicationBuilder` to `IClientBuilder`
to align with the `IClientBuilder` composition change in
System.ClientModel.


Commits viewable in [compare
view](Azure/azure-sdk-for-net@Azure.Identity_1.19.0...Azure.Identity_1.20.0).
</details>

Updated
[Meziantou.Analyzer](https://github.com/meziantou/Meziantou.Analyzer)
from 3.0.29 to 3.0.44.

<details>
<summary>Release notes</summary>

_Sourced from [Meziantou.Analyzer's
releases](https://github.com/meziantou/Meziantou.Analyzer/releases)._

## 3.0.44

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.44>

## What's Changed
* MA0011: suppress diagnostic when all AppendFormat template arguments
are culture-insensitive by @​Copilot in
meziantou/Meziantou.Analyzer#1085


**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.43...3.0.44

## 3.0.43

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.43>

## What's Changed
* Fix expression parenthesis in code fixers by @​meziantou in
meziantou/Meziantou.Analyzer#1083


**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.42...3.0.43

## 3.0.42

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.42>

## What's Changed
* Cache IsAwaitable result for the no-SyntaxNode overload by @​meziantou
in meziantou/Meziantou.Analyzer#1082


**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.41...3.0.42

## 3.0.41

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.41>

## What's Changed
* Improve MA0042 async overload matching coverage by @​meziantou in
meziantou/Meziantou.Analyzer#1065


**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.40...3.0.41

## 3.0.40

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.40>

## What's Changed
* Add MA0191: Detect null! and default! usage by @​Copilot in
meziantou/Meziantou.Analyzer#1081


**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.39...3.0.40

## 3.0.39

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.39>

## What's Changed
* Add code fixes for MA0094-MA0097 by @​meziantou in
meziantou/Meziantou.Analyzer#1078


**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.38...3.0.39

## 3.0.38

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.38>

## What's Changed
* Fix typos and grammar across all 188 rule documentation files by
@​Copilot in meziantou/Meziantou.Analyzer#1076
* Add code fixers for MA0118, MA0161, and MA0186 by @​meziantou in
meziantou/Meziantou.Analyzer#1079
* Validate fix conditions in RegisterCodeFixesAsync before registering
code fixes by @​Copilot in
meziantou/Meziantou.Analyzer#1077


**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.37...3.0.38

## 3.0.37

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.37>

## What's Changed
* docs: require updating rule docs when rules change by @​meziantou in
meziantou/Meziantou.Analyzer#1075
* Add code fixers for MA0138/44/46/52/56/57/58/60/69/73 by @​meziantou
in meziantou/Meziantou.Analyzer#1074


**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.36...3.0.37

## 3.0.36

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.36>

## What's Changed
* Add missing code fixers for 10 analyzer rules by @​meziantou in
meziantou/Meziantou.Analyzer#1073


**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.35...3.0.36

## 3.0.35

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.35>

## What's Changed
* Add missing code fixers for MA0088-MA0137 by @​meziantou in
meziantou/Meziantou.Analyzer#1072


**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.34...3.0.35

## 3.0.34

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.34>

## What's Changed
* Add code fixer for MA0174 (Record should use explicit 'class' keyword)
by @​Copilot in
meziantou/Meziantou.Analyzer#1070


**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.33...3.0.34

## 3.0.33

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.33>

## What's Changed
* Add code fixer for MA0173 (Use LazyInitializer.EnsureInitialized) by
@​Copilot in meziantou/Meziantou.Analyzer#1071


**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.32...3.0.33

## 3.0.32

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.32>

## What's Changed
* Add code fixer for MA0175 (Remove explicit `class` keyword from record
declarations) by @​Copilot in
meziantou/Meziantou.Analyzer#1069


**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.31...3.0.32

## 3.0.31

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.31>

## What's Changed
* Add code fixer for MA0137 and MA0138 (async suffix naming) by
@​Copilot in meziantou/Meziantou.Analyzer#1068


**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.30...3.0.31

## 3.0.30

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.30>

## What's Changed
* MA0137: Add `exclude_test_methods` configuration option by @​Copilot
in meziantou/Meziantou.Analyzer#1067


**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.29...3.0.30

Commits viewable in [compare
view](meziantou/Meziantou.Analyzer@3.0.29...3.0.44).
</details>

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore <dependency name> major version` will close this
group update PR and stop Dependabot creating any more for the specific
dependency's major version (unless you unignore this specific
dependency's major version or upgrade to it yourself)
- `@dependabot ignore <dependency name> minor version` will close this
group update PR and stop Dependabot creating any more for the specific
dependency's minor version (unless you unignore this specific
dependency's minor version or upgrade to it yourself)
- `@dependabot ignore <dependency name>` will close this group update PR
and stop Dependabot creating any more for the specific dependency
(unless you unignore this specific dependency or upgrade to it yourself)
- `@dependabot unignore <dependency name>` will remove all of the ignore
conditions of the specified dependency
- `@dependabot unignore <dependency name> <ignore condition>` will
remove the ignore condition of the specified dependency and ignore
conditions


</details>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant