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

Add fixer to convert from bound generic types in nameof to unbound types. #75976

Merged
merged 66 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 59 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
b363549
Add initial tests
CyrusNajmabadi Oct 3, 2024
f1f276b
Initial work
CyrusNajmabadi Oct 3, 2024
d5d1423
more working
CyrusNajmabadi Oct 3, 2024
f16971d
Better parsing
CyrusNajmabadi Oct 3, 2024
6d9a068
more work
CyrusNajmabadi Oct 3, 2024
b4ff542
names
CyrusNajmabadi Oct 3, 2024
092b89c
nesting
CyrusNajmabadi Oct 3, 2024
d0b2d12
Fixup tests
CyrusNajmabadi Oct 3, 2024
2d0155a
Fixup tests
CyrusNajmabadi Oct 3, 2024
db46266
Fixup tests
CyrusNajmabadi Oct 3, 2024
074bd23
Fix check
CyrusNajmabadi Oct 3, 2024
52dd592
Merge remote-tracking branch 'upstream/main' into openTypes
CyrusNajmabadi Oct 8, 2024
08e46f4
Fix test
CyrusNajmabadi Oct 8, 2024
5f1fd05
Merge branch 'main' into openTypes
CyrusNajmabadi Oct 9, 2024
bbdb37e
Merge remote-tracking branch 'upstream/main' into openTypes
CyrusNajmabadi Oct 10, 2024
2198c85
message id
CyrusNajmabadi Oct 10, 2024
ce2286a
Merge branch 'main' into openTypes
CyrusNajmabadi Nov 4, 2024
116d579
Merge remote-tracking branch 'upstream/main' into openTypes
CyrusNajmabadi Nov 14, 2024
da8e09e
Update src/Compilers/CSharp/Portable/Parser/LanguageParser.cs
CyrusNajmabadi Nov 14, 2024
3147497
ADd tests
CyrusNajmabadi Nov 14, 2024
1df770b
Remove unnecessary options
CyrusNajmabadi Nov 14, 2024
10b1414
Merge branch 'openTypes' of https://github.com/CyrusNajmabadi/roslyn …
CyrusNajmabadi Nov 14, 2024
5a695dc
Add tests
CyrusNajmabadi Nov 14, 2024
4fcadd9
Simplify tests
CyrusNajmabadi Nov 14, 2024
7aff344
Share open type binding
CyrusNajmabadi Nov 14, 2024
f006bbb
Update src/Compilers/CSharp/Portable/Binder/Binder_Invocation.cs
CyrusNajmabadi Nov 14, 2024
74a3fe0
Update src/Compilers/CSharp/Portable/Binder/TypeofBinder.cs
CyrusNajmabadi Nov 14, 2024
a671848
Add tests
CyrusNajmabadi Nov 14, 2024
ae6727f
Add tests
CyrusNajmabadi Nov 14, 2024
74bdd91
Merge branch 'openTypes' of https://github.com/CyrusNajmabadi/roslyn …
CyrusNajmabadi Nov 14, 2024
7a6328a
Simplify
CyrusNajmabadi Nov 14, 2024
cf0df29
Add semantic model test
CyrusNajmabadi Nov 14, 2024
5af3f7a
Update test
CyrusNajmabadi Nov 14, 2024
48688a5
Add testS
CyrusNajmabadi Nov 14, 2024
d0b7fa5
Update test
CyrusNajmabadi Nov 15, 2024
13e7eba
Update test
CyrusNajmabadi Nov 15, 2024
1e6366f
Add docs and tests
CyrusNajmabadi Nov 15, 2024
739a4ef
Update test
CyrusNajmabadi Nov 15, 2024
401ec47
Add more checks
CyrusNajmabadi Nov 15, 2024
9feec81
Merge branch 'main' into openTypes
CyrusNajmabadi Nov 15, 2024
6f6e987
Merge remote-tracking branch 'upstream/main' into openTypes
CyrusNajmabadi Nov 19, 2024
9497a5e
rename
CyrusNajmabadi Nov 19, 2024
7ed0bfe
do not use collection expression
CyrusNajmabadi Nov 19, 2024
30c1a45
do not use expr body or out var
CyrusNajmabadi Nov 19, 2024
efeea4c
Add tesets
CyrusNajmabadi Nov 19, 2024
f77d4d0
Adding tests
CyrusNajmabadi Nov 19, 2024
0a3e50a
use explicit else clause
CyrusNajmabadi Nov 19, 2024
21d4a4e
validate runtime behavior
CyrusNajmabadi Nov 19, 2024
0be4c20
Do not use expression bodies or patterns
CyrusNajmabadi Nov 19, 2024
a0e8e13
Add typeof teest
CyrusNajmabadi Nov 19, 2024
4d19b86
Add nameof teest
CyrusNajmabadi Nov 19, 2024
dd32790
Add analyzer
CyrusNajmabadi Nov 19, 2024
b2e545d
Add tests
CyrusNajmabadi Nov 19, 2024
7d828ee
Merge branch 'openTypes' into openTypesFixer
CyrusNajmabadi Nov 19, 2024
aaa10cb
Add tests
CyrusNajmabadi Nov 19, 2024
959bb32
Add tests
CyrusNajmabadi Nov 19, 2024
d573689
make sealed
CyrusNajmabadi Nov 19, 2024
0a8da15
Merge remote-tracking branch 'upstream/main' into openTypesFixer
CyrusNajmabadi Nov 19, 2024
5e9b7ed
Rename
CyrusNajmabadi Nov 19, 2024
3643cac
Renames
CyrusNajmabadi Nov 20, 2024
7ad7e11
Renames
CyrusNajmabadi Nov 20, 2024
f14f7b5
update test
CyrusNajmabadi Nov 20, 2024
60f3dcd
update test
CyrusNajmabadi Nov 20, 2024
189cf9f
Fix
CyrusNajmabadi Nov 20, 2024
991a2e3
update test
CyrusNajmabadi Nov 20, 2024
26f113c
rename files
CyrusNajmabadi Nov 20, 2024
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
1 change: 1 addition & 0 deletions src/Analyzers/CSharp/Analyzers/CSharpAnalyzers.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
<Compile Include="$(MSBuildThisFileDirectory)UsePatternMatching\CSharpUseNotPatternDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseSimpleUsingStatement\UseSimpleUsingStatementDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseThrowExpression\CSharpUseThrowExpressionDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseUnboundTypeInNameOf\UseUnboundTypeInNameOfDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseUtf8StringLiteral\UseUtf8StringLiteralDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ValidateFormatString\CSharpValidateFormatStringDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)NewLines\EmbeddedStatementPlacement\EmbeddedStatementPlacementDiagnosticAnalyzer.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,4 +422,7 @@
<value>Use 'System.Threading.Lock'</value>
<comment>{Locked="System.Threading.Lock"} "System.Threading.Lock" is the name of a .Net type and should not be localized.</comment>
</data>
<data name="Use_unbound_generic_type" xml:space="preserve">
<value>Use unbound generic type</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ internal CSharpSimplifierOptions GetSimplifierOptions()
public CodeStyleOption2<bool> PreferMethodGroupConversion => GetOption(CSharpCodeStyleOptions.PreferMethodGroupConversion);
public CodeStyleOption2<bool> PreferPrimaryConstructors => GetOption(CSharpCodeStyleOptions.PreferPrimaryConstructors);
public CodeStyleOption2<bool> PreferSystemThreadingLock => GetOption(CSharpCodeStyleOptions.PreferSystemThreadingLock);
public CodeStyleOption2<bool> PreferUnboundTypeInNameOf => GetOption(CSharpCodeStyleOptions.PreferUnboundTypeInNameOf);

// CodeGenerationOptions

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Linq;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Shared.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Text;

namespace Microsoft.CodeAnalysis.CSharp.UseUnboundTypeInNameOf;

/// <summary>
/// Looks for code of the form:
///
/// <code>
/// nameof(List&lt;...&gt;)
/// </code>
///
/// and converts it to:
///
/// <code>
/// nameof(List&lt;&gt;)
/// </code>
///
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal sealed class CSharpUseUnboundTypeInNameOfDiagnosticAnalyzer()
: AbstractBuiltInCodeStyleDiagnosticAnalyzer(
IDEDiagnosticIds.UseUnboundTypeInNameOfDiagnosticId,
EnforceOnBuildValues.UseUnboundTypeInNameOf,
CSharpCodeStyleOptions.PreferUnboundTypeInNameOf,
new LocalizableResourceString(
nameof(CSharpAnalyzersResources.Use_unbound_generic_type), CSharpAnalyzersResources.ResourceManager, typeof(CSharpAnalyzersResources)))
{
public override DiagnosticAnalyzerCategory GetAnalyzerCategory()
=> DiagnosticAnalyzerCategory.SemanticSpanAnalysis;

protected override void InitializeWorker(AnalysisContext context)
{
context.RegisterCompilationStartAction(context =>
{
// Tuples are only available in C# 14 and above.
var compilation = context.Compilation;
if (!compilation.LanguageVersion().IsCSharp14OrAbove())
return;

context.RegisterSyntaxNodeAction(
AnalyzeInvocationExpression,
SyntaxKind.InvocationExpression);
});
}

private void AnalyzeInvocationExpression(SyntaxNodeAnalysisContext syntaxContext)
{
var cancellationToken = syntaxContext.CancellationToken;
var styleOption = syntaxContext.GetCSharpAnalyzerOptions().PreferUnboundTypeInNameOf;
if (!styleOption.Value || ShouldSkipAnalysis(syntaxContext, styleOption.Notification))
return;

var invocation = (InvocationExpressionSyntax)syntaxContext.Node;
if (!invocation.IsNameOfInvocation())
return;

foreach (var typeArgumentList in invocation.DescendantNodesAndSelf().OfType<TypeArgumentListSyntax>())
{
foreach (var argument in typeArgumentList.Arguments)
{
if (argument.Kind() != SyntaxKind.OmittedTypeArgument)
{
syntaxContext.ReportDiagnostic(DiagnosticHelper.CreateWithLocationTags(
Descriptor,
invocation.GetFirstToken().GetLocation(),
styleOption.Notification,
syntaxContext.Options,
[invocation.GetLocation()],
additionalUnnecessaryLocations: [invocation.SyntaxTree.GetLocation(
TextSpan.FromBounds(typeArgumentList.LessThanToken.Span.End, typeArgumentList.GreaterThanToken.Span.Start))]));

return;
}
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/Analyzers/CSharp/CodeFixes/CSharpCodeFixes.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@
<Compile Include="$(MSBuildThisFileDirectory)UsePatternMatching\CSharpAsAndNullCheckCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UsePatternMatching\CSharpIsAndCastCheckCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseSimpleUsingStatement\UseSimpleUsingStatementCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseUnboundTypeInNameOf\CSharpUseUnboundTypeInNameOfCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseUtf8StringLiteral\UseUtf8StringLiteralCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)NewLines\EmbeddedStatementPlacement\EmbeddedStatementPlacementCodeFixProvider.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Shared.Extensions;

namespace Microsoft.CodeAnalysis.CSharp.UseUnboundTypeInNameOf;

using static SyntaxFactory;

[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.UseUnboundTypeInNameOf), Shared]
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed partial class CSharpUseUnboundTypeInNameOfCodeFixProvider() : SyntaxEditorBasedCodeFixProvider
{
private static readonly SyntaxNodeOrToken s_omittedArgument = (SyntaxNodeOrToken)OmittedTypeArgument();

public override ImmutableArray<string> FixableDiagnosticIds { get; }
= [IDEDiagnosticIds.UseUnboundTypeInNameOfDiagnosticId];

public override Task RegisterCodeFixesAsync(CodeFixContext context)
{
RegisterCodeFix(context, CSharpAnalyzersResources.Use_unbound_generic_type, nameof(CSharpAnalyzersResources.Use_unbound_generic_type));
return Task.CompletedTask;
}

protected override Task FixAllAsync(
Document document, ImmutableArray<Diagnostic> diagnostics,
SyntaxEditor editor, CancellationToken cancellationToken)
{
foreach (var diagnostic in diagnostics)
FixOne(editor, diagnostic, cancellationToken);

return Task.CompletedTask;
}

private static void FixOne(
SyntaxEditor editor, Diagnostic diagnostic, CancellationToken cancellationToken)
{
var nameofInvocation = (InvocationExpressionSyntax)diagnostic.AdditionalLocations[0].FindNode(getInnermostNodeForTie: true, cancellationToken);
if (!nameofInvocation.IsNameOfInvocation())
return;

foreach (var typeArgumentList in nameofInvocation.DescendantNodes().OfType<TypeArgumentListSyntax>().OrderByDescending(t => t.SpanStart))
{
if (typeArgumentList.Arguments.Any(a => a.Kind() != SyntaxKind.OmittedTypeArgument))
{
var list = NodeOrTokenList(typeArgumentList.Arguments.GetWithSeparators().Select(
t => t.IsToken ? t.AsToken().WithoutTrivia() : s_omittedArgument));
editor.ReplaceNode(typeArgumentList, typeArgumentList.WithArguments(SeparatedList<TypeSyntax>(list)));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@
<Compile Include="$(MSBuildThisFileDirectory)UseSystemHashCode\UseSystemHashCodeTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseThrowExpression\UseThrowExpressionTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseThrowExpression\UseThrowExpressionTests_FixAllTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseUnboundTypeInNameOf\UseUnboundTypeInNameOfTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseUtf8StringLiteral\UseUtf8StringLiteralTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseVarTestExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ValidateFormatString\ValidateFormatStringTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.UseTupleSwap;
CSharpUseTupleSwapDiagnosticAnalyzer,
CSharpUseTupleSwapCodeFixProvider>;

[Trait(Traits.Feature, Traits.Features.CodeActionsUseLocalFunction)]
public partial class UseTupleSwapTests
[Trait(Traits.Feature, Traits.Features.CodeActionsUseTupleSwap)]
public sealed class UseTupleSwapTests
{
[Fact]
public async Task TestMissingBeforeCSharp7()
Expand Down
Loading
Loading