Skip to content

Create an analyzer that explicitly flags naming clash for extensions#158

Merged
andrewlock merged 2 commits intomainfrom
analyzer-for-naming-clash
Jul 1, 2025
Merged

Create an analyzer that explicitly flags naming clash for extensions#158
andrewlock merged 2 commits intomainfrom
analyzer-for-naming-clash

Conversation

@andrewlock
Copy link
Owner

Currently, you can generate extension types that result in the same extension class being generated, which causes clashes. For example, the following generates SomeNamespace.MyEnumExtensions twice, one for each enum:

namespace SomeNamespace;

[EnumExtensions]
public enum MyEnum
{
    One,
    Two
}

public class Nested
{
    [EnumExtensions]
    public enum MyEnum
    {
        One,
        Two
    }
}

Ideally we would disambiguate by generating SomeNamespace.Nested.MyEnumExtensions as a nested class, but extension method classes can't be nested classes. Another option would be to include the class name in the generated namespace, but then there's another issue that can generate clashes. Ultimately, it would be possible to generate an extension method something like Nested_MyEnum, but there's always ways to get clashes (or you have to generate something really ugly). Plus it would be a breaking change.

Given that these types of clashes are going to be rare, this PR just adds an analyzer NEEG001 that flags the fact there's a clash on the [EnumExtensions] attribute directly.

This isn't strictly necessary, because generating duplicate extension classes results in a lot of compiler errors, but having an analyzer will hopefully make it more obvious exactly what's happened.

@github-actions
Copy link

github-actions bot commented Jul 1, 2025

Test Results

    162 files  ± 0      162 suites  ±0   2m 32s ⏱️ +34s
  7 445 tests +19    7 441 ✅ +19   4 💤 ±0  0 ❌ ±0 
129 149 runs  +19  129 077 ✅ +19  72 💤 ±0  0 ❌ ±0 

Results for commit 78a1131. ± Comparison against base commit bed9e9c.

@andrewlock andrewlock merged commit 81c2bb3 into main Jul 1, 2025
5 checks passed
@andrewlock andrewlock deleted the analyzer-for-naming-clash branch July 1, 2025 21:49
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