dotnet / dotnet/roslyn

[Analyzer] New Extension Keyword

Open
#85,181 3 comments 0 reactions 0 assignees View on GitHub
Area-IDE Feature - Extension Everything
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

Here are a few issues with the new `extension` keyword and the Microsoft.CodeAnalysis.Analyzer v5.9.0 package.

Currently I'm seeing diagnostics for:

- Accessing static extension properties, produces: `IDE0002: Name can be simplified` on the the extended type
- Of note, the code fixer for this tries to strip the type being extended and replaces it with `object` - this is true for the example below, `TypeLibrary` replaced with `object`.
- Creating an extension class using the newer `extension` keyword produces: `CA1034: Nested types should not be visible` on the `extension` keyword itself.

Take the following example:

```csharp
public static class TypeLibraryExtensions
{
static readonly TypeIdentity _iResourceBuilder = new(typeof(IResourceBuilder<>));
static readonly TypeIdentity _defaultAspireResource = TypeIdentity.Create();

extension(TypeLibrary)
{
public static TypeIdentity IResourceBuilder => _iResourceBuilder;

public static TypeIdentity DefaultAspireResource => _defaultAspireResource;
}
}
```

Calling `TypeLibrary.DefaultAspireResource` now produces the `IDE0002` diagnostic. I don't want to ignore or disable this at a project or member/ type level because it has genuine value.

Contributor guide

Open the contributing guide

Research direction

Start with the provided C# example using the `extension` keyword and Microsoft.CodeAnalysis.Analyzer v5.9.0, then reproduce IDE0002 on `TypeLibrary.DefaultAspireResource` and CA1034 on the extension declaration. Trace the analyzer and code-fixer behavior for these diagnostics; done means valid extension members produce neither diagnostic and the fixer no longer replaces `TypeLibrary` with `object`.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
compilers, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.