Skip to content

MA0110: Derive generated regex names from field/variable context#1010

Merged
meziantou merged 5 commits intomainfrom
copilot/fix-ma0110-default-name-suggestion
Feb 18, 2026
Merged

MA0110: Derive generated regex names from field/variable context#1010
meziantou merged 5 commits intomainfrom
copilot/fix-ma0110-default-name-suggestion

Conversation

Copy link
Contributor

Copilot AI commented Feb 18, 2026

MA0110's code fix always suggested "MyRegex" regardless of where the Regex instantiation occurred. This leads to poor code clarity and naming conflicts.

Changes

Fixer (UseRegexSourceGeneratorFixer.cs)

  • Walk operation tree via operation.Ancestors() to detect IFieldInitializerOperation or IVariableDeclaratorOperation
  • For fields (length > 1): use field name as-is
  • For variables (length > 1): convert to PascalCase
  • Compute suggested name directly in the fixer
  • Fall back to "MyRegex" for single-char names or no context
  • Existing uniqueness checking handles conflicts

Analyzer (UseRegexSourceGeneratorAnalyzer.cs)

  • No changes - remains focused on detection only

Example

Before:

class Sample {
    private static readonly Regex EmailPattern = new Regex(@"\w+@\w+");
    // Code fix suggested: MyRegex() or MyRegex_()
}

After:

class Sample {
    private static readonly Regex EmailPattern = EmailPattern_();
    // Code fix now suggests: EmailPattern() (with _ if needed for uniqueness)
    
    [GeneratedRegex(@"\w+@\w+")]
    private static partial Regex EmailPattern_();
}

Variables follow same pattern with PascalCase conversion:

void Method() {
    Regex emailPattern = new Regex(@"\w+@\w+");
    // Suggests: EmailPattern()
}

Limitations

Does not yet remove original field/variable declarations or replace all usages—would require symbol finding and reference replacement across the document.


💡 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 and others added 2 commits February 18, 2026 16:59
Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix MA0110 to suggest better regex names based on context MA0110: Derive generated regex names from field/variable context Feb 18, 2026
Copilot AI requested a review from meziantou February 18, 2026 17:13
Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Copilot AI requested a review from meziantou February 18, 2026 17:22
@meziantou meziantou marked this pull request as ready for review February 18, 2026 18:13
Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
This was referenced Feb 27, 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