Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not offer to convert typeof to nameof on a generic type #76920

Merged
merged 3 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ internal sealed class CSharpConvertTypeOfToNameOfDiagnosticAnalyzer()
{
private static readonly string s_title = CSharpAnalyzersResources.typeof_can_be_converted_to_nameof;

protected override bool SupportsUnboundGenerics(ParseOptions options)
=> options.LanguageVersion().IsCSharp14OrAbove();

protected override bool IsValidTypeofAction(OperationAnalysisContext context)
{
var node = context.Operation.Syntax;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,19 +267,7 @@ class Goo<T>
{
void M()
{
_ = [|typeof(Goo<int>).Name|];
}
}
}
""",
FixedCode = """
class Test
{
class Goo<T>
{
void M()
{
_ = nameof(Goo<>);
_ = typeof(Goo<int>).Name;
}
}
}
Expand Down Expand Up @@ -321,19 +309,7 @@ class Goo<T>
{
void M()
{
_ = [|typeof(Goo<>).Name|];
}
}
}
""",
FixedCode = """
class Test
{
class Goo<T>
{
void M()
{
_ = nameof(Goo<>);
_ = typeof(Goo<>).Name;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ public override DiagnosticAnalyzerCategory GetAnalyzerCategory()

protected abstract bool IsValidTypeofAction(OperationAnalysisContext context);

protected abstract bool SupportsUnboundGenerics(ParseOptions options);

protected override void InitializeWorker(AnalysisContext context)
{
context.RegisterOperationAction(AnalyzeAction, OperationKind.TypeOf);
}
=> context.RegisterOperationAction(AnalyzeAction, OperationKind.TypeOf);

protected void AnalyzeAction(OperationAnalysisContext context)
{
Expand All @@ -47,7 +43,7 @@ protected void AnalyzeAction(OperationAnalysisContext context)

}

private bool IsValidOperation(IOperation operation)
private static bool IsValidOperation(IOperation operation)
{
// Cast to a typeof operation & check parent is a property reference and member access
var typeofOperation = (ITypeOfOperation)operation;
Expand All @@ -69,11 +65,10 @@ private bool IsValidOperation(IOperation operation)
if (typeofOperation.TypeOperand is not INamedTypeSymbol namedType)
return false;

// Non-generic types are always convertible. typeof(X).Name can always be converted to nameof(X)
if (namedType.TypeArguments.Length == 0)
return true;

// Generic types are convertible if the lang supports it. e.g. typeof(X<Y>).Name can be converted to nameof(X<>).
return SupportsUnboundGenerics(operation.Syntax.SyntaxTree.Options);
// Note: generic names like `typeof(List<int>)` are not convertible (since the name will be List`1). However,
// it's fine if an outer part of the name is generic (like `typeof(List<int>.Enumerator)`). as that will be
// convertible to `nameof(List<>.Enumerator)`. So we only need to check the type arguments directly on the type
// here, not on any containing types.
return namedType.TypeArguments.Length == 0;
Copy link
Member Author

Choose a reason for hiding this comment

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

note: we have tests for the non-generic nested in a generic.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.ConvertTypeOfToNameOf
MyBase.New(s_title)
End Sub

Protected Overrides Function SupportsUnboundGenerics(options As ParseOptions) As Boolean
Return False
End Function

Protected Overrides Function IsValidTypeofAction(context As OperationAnalysisContext) As Boolean
Dim node = context.Operation.Syntax
Dim compilation = context.Compilation
Expand Down
Loading