Skip to content

Commit

Permalink
Merge pull request #74359 from CyrusNajmabadi/primaryConstructors
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi authored Jul 11, 2024
2 parents a7af126 + 0a6f8a5 commit 7241b85
Show file tree
Hide file tree
Showing 108 changed files with 347 additions and 894 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,19 @@
// 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.Composition;
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis.AddAccessibilityModifiers;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Host.Mef;

namespace Microsoft.CodeAnalysis.CSharp.AddAccessibilityModifiers;

[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.AddAccessibilityModifiers), Shared]
internal class CSharpAddAccessibilityModifiersCodeFixProvider : AbstractAddAccessibilityModifiersCodeFixProvider
[method: ImportingConstructor]
[method: SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
internal sealed class CSharpAddAccessibilityModifiersCodeFixProvider() : AbstractAddAccessibilityModifiersCodeFixProvider
{
[ImportingConstructor]
[SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
public CSharpAddAccessibilityModifiersCodeFixProvider()
{
}

protected override SyntaxNode MapToDeclarator(SyntaxNode node)
{
switch (node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
namespace Microsoft.CodeAnalysis.CSharp.AddAccessibilityModifiers;

[ExportLanguageService(typeof(IAddAccessibilityModifiersService), LanguageNames.CSharp), Shared]
internal class CSharpAddAccessibilityModifiersService : CSharpAddAccessibilityModifiers, IAddAccessibilityModifiersService
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public CSharpAddAccessibilityModifiersService()
{
}
}
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed class CSharpAddAccessibilityModifiersService() : CSharpAddAccessibilityModifiers, IAddAccessibilityModifiersService;
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,16 @@
namespace Microsoft.CodeAnalysis.CSharp.AddAnonymousTypeMemberName;

[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.AddAnonymousTypeMemberName), Shared]
internal class CSharpAddAnonymousTypeMemberNameCodeFixProvider
[method: ImportingConstructor]
[method: SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
internal sealed class CSharpAddAnonymousTypeMemberNameCodeFixProvider()
: AbstractAddAnonymousTypeMemberNameCodeFixProvider<
ExpressionSyntax,
AnonymousObjectCreationExpressionSyntax,
AnonymousObjectMemberDeclaratorSyntax>
{
private const string CS0746 = nameof(CS0746); // Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

[ImportingConstructor]
[SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
public CSharpAddAnonymousTypeMemberNameCodeFixProvider()
{
}

public override ImmutableArray<string> FixableDiagnosticIds { get; }
= [CS0746];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.Diagnostics;
Expand All @@ -16,14 +15,10 @@
namespace Microsoft.CodeAnalysis.CSharp.Diagnostics.AddBraces;

[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.AddBraces), Shared]
internal sealed class CSharpAddBracesCodeFixProvider : SyntaxEditorBasedCodeFixProvider
[method: ImportingConstructor]
[method: SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
internal sealed class CSharpAddBracesCodeFixProvider() : SyntaxEditorBasedCodeFixProvider
{
[ImportingConstructor]
[SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
public CSharpAddBracesCodeFixProvider()
{
}

public override ImmutableArray<string> FixableDiagnosticIds
=> [IDEDiagnosticIds.AddBracesDiagnosticId];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,15 @@ namespace Microsoft.CodeAnalysis.CSharp.CodeFixes.AddInheritdoc;
using static CSharpSyntaxTokens;

[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.AddInheritdoc), Shared]
internal sealed class AddInheritdocCodeFixProvider : SyntaxEditorBasedCodeFixProvider
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed class AddInheritdocCodeFixProvider() : SyntaxEditorBasedCodeFixProvider
{
/// <summary>
/// CS1591: Missing XML comment for publicly visible type or member 'Type_or_Member'
/// </summary>
private const string CS1591 = nameof(CS1591);

[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public AddInheritdocCodeFixProvider()
{
}

public override ImmutableArray<string> FixableDiagnosticIds => [CS1591];

public override async Task RegisterCodeFixesAsync(CodeFixContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,17 @@
namespace Microsoft.CodeAnalysis.CSharp.AddObsoleteAttribute;

[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.AddObsoleteAttribute), Shared]
internal class CSharpAddObsoleteAttributeCodeFixProvider
: AbstractAddObsoleteAttributeCodeFixProvider
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed class CSharpAddObsoleteAttributeCodeFixProvider()
: AbstractAddObsoleteAttributeCodeFixProvider(CSharpSyntaxFacts.Instance, CSharpCodeFixesResources.Add_Obsolete)
{
public override ImmutableArray<string> FixableDiagnosticIds { get; } =
[
"CS0612",
"CS0618",
"CS0672",
"CS1062",
"CS1064"
, // The best overloaded Add method 'MyCollection.Add(int)' for the collection initializer element is obsolete"
"CS1064",
];

[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public CSharpAddObsoleteAttributeCodeFixProvider()
: base(CSharpSyntaxFacts.Instance, CSharpCodeFixesResources.Add_Obsolete)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System.Collections.Immutable;
using System.Composition;
using System.Diagnostics.CodeAnalysis;
Expand All @@ -18,7 +16,9 @@ namespace Microsoft.CodeAnalysis.CSharp.AddParameter;

[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.AddParameter), Shared]
[ExtensionOrder(Before = PredefinedCodeFixProviderNames.GenerateConstructor)]
internal class CSharpAddParameterCodeFixProvider : AbstractAddParameterCodeFixProvider<
[method: ImportingConstructor]
[method: SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
internal sealed class CSharpAddParameterCodeFixProvider() : AbstractAddParameterCodeFixProvider<
ArgumentSyntax,
AttributeArgumentSyntax,
ArgumentListSyntax,
Expand All @@ -34,12 +34,6 @@ internal class CSharpAddParameterCodeFixProvider : AbstractAddParameterCodeFixPr

private static readonly ImmutableArray<string> AddParameterFixableDiagnosticIds = [CS1501, CS1503, CS1660, CS1729, CS1739];

[ImportingConstructor]
[SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
public CSharpAddParameterCodeFixProvider()
{
}

public override ImmutableArray<string> FixableDiagnosticIds
=> AddParameterFixableDiagnosticIds;

Expand All @@ -52,14 +46,14 @@ protected override ImmutableArray<string> CannotConvertDiagnosticIds
protected override ITypeSymbol GetArgumentType(SyntaxNode argumentNode, SemanticModel semanticModel, CancellationToken cancellationToken)
=> ((ArgumentSyntax)argumentNode).DetermineParameterType(semanticModel, cancellationToken);

protected override RegisterFixData<ArgumentSyntax> TryGetLanguageSpecificFixInfo(
protected override RegisterFixData<ArgumentSyntax>? TryGetLanguageSpecificFixInfo(
SemanticModel semanticModel,
SyntaxNode node,
CancellationToken cancellationToken)
{
if (node is ConstructorInitializerSyntax constructorInitializer)
{
var constructorDeclaration = constructorInitializer.Parent;
var constructorDeclaration = constructorInitializer.GetRequiredParent();
if (semanticModel.GetDeclaredSymbol(constructorDeclaration, cancellationToken) is IMethodSymbol constructorSymbol)
{
var type = constructorSymbol.ContainingType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,15 @@ namespace Microsoft.CodeAnalysis.CSharp.AliasAmbiguousType;

[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.AliasAmbiguousType), Shared]
[ExtensionOrder(After = PredefinedCodeFixProviderNames.FullyQualify)]
internal class CSharpAliasAmbiguousTypeCodeFixProvider : AbstractAliasAmbiguousTypeCodeFixProvider
[method: ImportingConstructor]
[method: SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
internal sealed class CSharpAliasAmbiguousTypeCodeFixProvider() : AbstractAliasAmbiguousTypeCodeFixProvider
{
/// <summary>
/// 'reference' is an ambiguous reference between 'identifier' and 'identifier'
/// </summary>
private const string CS0104 = nameof(CS0104);

[ImportingConstructor]
[SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
public CSharpAliasAmbiguousTypeCodeFixProvider()
{
}

public override ImmutableArray<string> FixableDiagnosticIds
=> [CS0104];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,10 @@
namespace Microsoft.CodeAnalysis.CSharp.AssignOutParameters;

[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.AssignOutParametersAboveReturn), Shared]
internal class AssignOutParametersAboveReturnCodeFixProvider : AbstractAssignOutParametersCodeFixProvider
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed class AssignOutParametersAboveReturnCodeFixProvider() : AbstractAssignOutParametersCodeFixProvider
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public AssignOutParametersAboveReturnCodeFixProvider()
{
}

protected override void TryRegisterFix(CodeFixContext context, Document document, SyntaxNode container, SyntaxNode location)
{
RegisterCodeFix(context, CSharpCodeFixesResources.Assign_out_parameters, nameof(CSharpCodeFixesResources.Assign_out_parameters));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@
namespace Microsoft.CodeAnalysis.CSharp.AssignOutParameters;

[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.AssignOutParametersAtStart), Shared]
internal class AssignOutParametersAtStartCodeFixProvider : AbstractAssignOutParametersCodeFixProvider
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed class AssignOutParametersAtStartCodeFixProvider() : AbstractAssignOutParametersCodeFixProvider
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public AssignOutParametersAtStartCodeFixProvider()
{
}

protected override void TryRegisterFix(CodeFixContext context, Document document, SyntaxNode container, SyntaxNode location)
{
// Don't offer if we're already the starting statement of the container. This case will
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,12 @@ namespace Microsoft.CodeAnalysis.CSharp.ConditionalExpressionInStringInterpolati
using static CSharpSyntaxTokens;

[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.AddParenthesesAroundConditionalExpressionInInterpolatedString), Shared]
internal class CSharpAddParenthesesAroundConditionalExpressionInInterpolatedStringCodeFixProvider : CodeFixProvider
[method: ImportingConstructor]
[method: SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
internal sealed class CSharpAddParenthesesAroundConditionalExpressionInInterpolatedStringCodeFixProvider() : CodeFixProvider
{
private const string CS8361 = nameof(CS8361); //A conditional expression cannot be used directly in a string interpolation because the ':' ends the interpolation.Parenthesize the conditional expression.

[ImportingConstructor]
[SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
public CSharpAddParenthesesAroundConditionalExpressionInInterpolatedStringCodeFixProvider()
{
}

// CS8361 is a syntax error and it is unlikely that there is more than one CS8361 at a time.
public override FixAllProvider? GetFixAllProvider() => null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@
namespace Microsoft.CodeAnalysis.CSharp.ConflictMarkerResolution;

[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.ConflictMarkerResolution), Shared]
internal class CSharpResolveConflictMarkerCodeFixProvider : AbstractResolveConflictMarkerCodeFixProvider
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed class CSharpResolveConflictMarkerCodeFixProvider()
: AbstractResolveConflictMarkerCodeFixProvider(CSharpSyntaxKinds.Instance, CS8300)
{
private const string CS8300 = nameof(CS8300); // Merge conflict marker encountered

[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public CSharpResolveConflictMarkerCodeFixProvider()
: base(CSharpSyntaxKinds.Instance, CS8300)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,10 @@
namespace Microsoft.CodeAnalysis.CSharp.ConvertNamespace;

[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.ConvertNamespace), Shared]
internal class ConvertNamespaceCodeFixProvider : SyntaxEditorBasedCodeFixProvider
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed class ConvertNamespaceCodeFixProvider() : SyntaxEditorBasedCodeFixProvider
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public ConvertNamespaceCodeFixProvider()
{
}
public override ImmutableArray<string> FixableDiagnosticIds
=> [IDEDiagnosticIds.UseBlockScopedNamespaceDiagnosticId, IDEDiagnosticIds.UseFileScopedNamespaceDiagnosticId];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,10 @@ namespace Microsoft.CodeAnalysis.CSharp.ConvertSwitchStatementToExpression;
using Constants = ConvertSwitchStatementToExpressionConstants;

[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.ConvertSwitchStatementToExpression), Shared]
internal sealed partial class ConvertSwitchStatementToExpressionCodeFixProvider : SyntaxEditorBasedCodeFixProvider
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed partial class ConvertSwitchStatementToExpressionCodeFixProvider() : SyntaxEditorBasedCodeFixProvider
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public ConvertSwitchStatementToExpressionCodeFixProvider()
{
}

public override ImmutableArray<string> FixableDiagnosticIds
=> [IDEDiagnosticIds.ConvertSwitchStatementToExpressionDiagnosticId];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,15 @@
namespace Microsoft.CodeAnalysis.CSharp.CodeFixes.ConvertToAsync;

[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.ConvertToAsync), Shared]
internal class CSharpConvertToAsyncMethodCodeFixProvider : AbstractConvertToAsyncCodeFixProvider
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed class CSharpConvertToAsyncMethodCodeFixProvider() : AbstractConvertToAsyncCodeFixProvider
{
/// <summary>
/// Cannot await void.
/// </summary>
private const string CS4008 = nameof(CS4008);

[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public CSharpConvertToAsyncMethodCodeFixProvider()
{
}

public override ImmutableArray<string> FixableDiagnosticIds => [CS4008];

protected override async Task<string> GetDescriptionAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Collections.Immutable;
using System.Composition;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Host.Mef;
Expand All @@ -15,16 +14,12 @@
namespace Microsoft.CodeAnalysis.CSharp.ConvertToRecord;

[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.ConvertToRecord), Shared]
internal class CSharpConvertToRecordCodeFixProvider : CodeFixProvider
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed class CSharpConvertToRecordCodeFixProvider() : CodeFixProvider
{
private const string CS8865 = nameof(CS8865); // Only records may inherit from records.

[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public CSharpConvertToRecordCodeFixProvider()
{
}

public override FixAllProvider? GetFixAllProvider()
=> null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@
namespace Microsoft.CodeAnalysis.CSharp.ConvertTypeOfToNameOf;

[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.ConvertTypeOfToNameOf), Shared]
internal class CSharpConvertTypeOfToNameOfCodeFixProvider : AbstractConvertTypeOfToNameOfCodeFixProvider<
[method: ImportingConstructor]
[method: SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
internal sealed class CSharpConvertTypeOfToNameOfCodeFixProvider() : AbstractConvertTypeOfToNameOfCodeFixProvider<
MemberAccessExpressionSyntax>
{
[ImportingConstructor]
[SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
public CSharpConvertTypeOfToNameOfCodeFixProvider()
{
}

protected override string GetCodeFixTitle()
=> CSharpCodeFixesResources.Convert_typeof_to_nameof;

Expand Down
Loading

0 comments on commit 7241b85

Please sign in to comment.