Skip to content

MA0190: Suggest converting [GeneratedRegex] partial methods to partial properties (C# 13+)#1060

Merged
meziantou merged 4 commits intomainfrom
copilot/add-rule-convert-partial-methods
Mar 24, 2026
Merged

MA0190: Suggest converting [GeneratedRegex] partial methods to partial properties (C# 13+)#1060
meziantou merged 4 commits intomainfrom
copilot/add-rule-convert-partial-methods

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 24, 2026

  • Add MA0190 constant to RuleIdentifiers.cs
  • Create analyzer UsePartialPropertyInsteadOfPartialMethodForGeneratedRegexAnalyzer.cs
  • Create code fixer UsePartialPropertyInsteadOfPartialMethodForGeneratedRegexFixer.cs
  • Create tests UsePartialPropertyInsteadOfPartialMethodForGeneratedRegexAnalyzerTests.cs
  • Create documentation docs/Rules/MA0190.md
  • Run documentation generator (updated README, editorconfig files)
  • All 8 tests pass with default Roslyn, 1 test passes with Roslyn 4.2/4.4
  • Fixed CI: CSharp12_NoDiagnostic test wrapped in #if CSHARP12_OR_GREATER; NoGeneratedRegexAttribute_NoDiagnostic kept outside #if to ensure CreateProjectBuilder is always referenced
  • Moved regexSymbol lookup to RegisterCompilationStartAction (fetched once per compilation, not per method)
  • Replaced manual foreach attribute loop with method.HasAttribute(generatedRegexAttributeSymbol) helper

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Add rule to suggest converting partial methods to properties MA0190: Suggest converting [GeneratedRegex] partial methods to partial properties (C# 13+) Mar 24, 2026
Copilot AI requested a review from meziantou March 24, 2026 17:40
@meziantou meziantou marked this pull request as ready for review March 24, 2026 18:10
@meziantou
Copy link
Copy Markdown
Owner

@copilot CI errors because the methods are not in #if CSHARP12_OR_GREATER

return;

// Must return Regex
var regexSymbol = context.Compilation.GetBestTypeByMetadataName("System.Text.RegularExpressions.Regex");
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Get the symbol in RegisterCompilationStartAction instead of using this method in each method.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in d7068a8. regexSymbol is now looked up once in RegisterCompilationStartAction and passed into AnalyzeMethod alongside generatedRegexAttributeSymbol.

return;

// Must have GeneratedRegex attribute
foreach (var attribute in method.GetAttributes())
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot I think there is a helper HasAttribute() in the project to replace this loop

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in d7068a8. Replaced the manual foreach loop with method.HasAttribute(generatedRegexAttributeSymbol).

…P12_OR_GREATER

Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Agent-Logs-Url: https://github.com/meziantou/Meziantou.Analyzer/sessions/548affa9-c004-4554-83b6-a1f6719edf3e
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 24, 2026

@copilot CI errors because the methods are not in #if CSHARP12_OR_GREATER

Fixed in 0a24c09. The CSharp12_NoDiagnostic test that used LanguageVersion.CSharp12 is now wrapped in #if CSHARP12_OR_GREATER. All other tests that use [GeneratedRegex] partial methods were already in #if CSHARP13_OR_GREATER. A simple NoGeneratedRegexAttribute_NoDiagnostic test (with an empty class) is kept outside any #if block to ensure the test class compiles and CreateProjectBuilder is referenced across all Roslyn versions.

…asAttribute helper

Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Agent-Logs-Url: https://github.com/meziantou/Meziantou.Analyzer/sessions/4f6b3d0a-6e6f-4d89-a9fa-153a0f3c71aa
This was referenced Apr 10, 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.

2 participants