Skip to content

Fix MA0148/MA0149 false positives with implicit conversions#1170

Merged
meziantou merged 1 commit into
mainfrom
meziantou/ma0149-implicit-conversion
May 23, 2026
Merged

Fix MA0148/MA0149 false positives with implicit conversions#1170
meziantou merged 1 commit into
mainfrom
meziantou/ma0149-implicit-conversion

Conversation

@meziantou
Copy link
Copy Markdown
Owner

Summary

MA0148 and MA0149 could suggest replacing == or != with is patterns when the comparison only worked through an implicit user-defined conversion. In those cases, the suggested is/is not rewrite is invalid C#.

This change makes the analyzer and fixer skip those cases so diagnostics are only reported when the pattern-matching rewrite is valid.

What changed

  • Added HasImplicitUserDefinedConversion in UsePatternMatchingForEqualityComparisonsCommon.
  • Updated the MA0148/MA0149 analyzer path to skip constant-comparison diagnostics when the non-constant operand depends on an implicit user-defined conversion.
  • Updated the fixer to apply the same guard for single comparisons and logical merge candidates.
  • Added regression tests for implicit-conversion equality and inequality cases, plus a mixed-expression case to ensure only valid expressions are rewritten.
  • Updated docs/Rules/MA0148.md and docs/Rules/MA0149.md with explicit examples of ignored implicit-conversion scenarios.

Validation

  • dotnet test tests/Meziantou.Analyzer.Test --filter "FullyQualifiedName~UsePatternMatchingForEqualityComparisonsAnalyzerTests"
  • dotnet test tests/Meziantou.Analyzer.Test /p:RoslynVersion=roslyn4.2 --filter "FullyQualifiedName~UsePatternMatchingForEqualityComparisonsAnalyzerTests"
  • dotnet build Meziantou.Analyzer.slnx
  • dotnet run --project src/DocumentationGenerator

Skip constant-pattern diagnostics and fixes when ==/!= relies on implicit user-defined conversions, add regression tests, and document the behavior.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@meziantou meziantou enabled auto-merge (squash) May 23, 2026 15:48
@meziantou meziantou merged commit e9055f1 into main May 23, 2026
13 checks passed
@meziantou meziantou deleted the meziantou/ma0149-implicit-conversion branch May 23, 2026 15:53
This was referenced May 23, 2026
This was referenced May 25, 2026
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