Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
b00a86f
Nullable enable file
CyrusNajmabadi Mar 26, 2025
7f194dc
Nullable enable
CyrusNajmabadi Mar 26, 2025
69abb8a
Nullable enable
CyrusNajmabadi Mar 26, 2025
d0d29fb
Nullable enable
CyrusNajmabadi Mar 26, 2025
f15634d
Nullable enable
CyrusNajmabadi Mar 26, 2025
5996721
Nullable enable
CyrusNajmabadi Mar 26, 2025
6e906d4
Nullable enable
CyrusNajmabadi Mar 26, 2025
abf0df0
Nullable enable
CyrusNajmabadi Mar 26, 2025
95cead1
Nullable enable
CyrusNajmabadi Mar 26, 2025
2d7be78
Nullable enable
CyrusNajmabadi Mar 26, 2025
1e50bbe
Nullable enable
CyrusNajmabadi Mar 26, 2025
c094b17
Nullable enable
CyrusNajmabadi Mar 26, 2025
f0e40f2
Nullable enable
CyrusNajmabadi Mar 26, 2025
627d0ff
Nullable enable
CyrusNajmabadi Mar 26, 2025
319e4ae
Nullable enable
CyrusNajmabadi Mar 26, 2025
7f3ea7b
Nullable enable
CyrusNajmabadi Mar 26, 2025
ef73895
Nullable enable
CyrusNajmabadi Mar 26, 2025
de80a11
Nullable enable
CyrusNajmabadi Mar 26, 2025
56a0e5f
Nullable enable
CyrusNajmabadi Mar 26, 2025
402c4bd
Nullable enable
CyrusNajmabadi Mar 26, 2025
575294c
Nullable enable
CyrusNajmabadi Mar 26, 2025
929e5cb
Nullable enable
CyrusNajmabadi Mar 26, 2025
8c3cbd3
Nullable enable
CyrusNajmabadi Mar 26, 2025
707e61d
Nullable enable
CyrusNajmabadi Mar 26, 2025
00d1cf0
Nullable enable
CyrusNajmabadi Mar 26, 2025
e2afb0b
Nullable enable
CyrusNajmabadi Mar 26, 2025
dab4671
Nullable enable
CyrusNajmabadi Mar 26, 2025
bdb03bf
Nullable enable
CyrusNajmabadi Mar 26, 2025
25aba1a
Link
CyrusNajmabadi Mar 26, 2025
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 @@ -35,7 +35,7 @@ protected override bool MethodHasHandlesClause(IMethodSymbol method)
protected override bool IsIfConditionalDirective(SyntaxNode node)
=> node is IfDirectiveTriviaSyntax;

protected override bool ReturnsThrow(SyntaxNode node)
protected override bool ReturnsThrow(SyntaxNode? node)
{
if (node is not BaseMethodDeclarationSyntax methodSyntax)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace Microsoft.CodeAnalysis.CSharp.UseExpressionBody;

internal class UseExpressionBodyForIndexersHelper :
internal sealed class UseExpressionBodyForIndexersHelper :
UseExpressionBodyHelper<IndexerDeclarationSyntax>
{
public static readonly UseExpressionBodyForIndexersHelper Instance = new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ internal sealed class CSharpConvertToAsyncMethodCodeFixProvider() : AbstractConv
/// </summary>
private const string CS4008 = nameof(CS4008);

public override FixAllProvider? GetFixAllProvider() => base.GetFixAllProvider();

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

protected override async Task<string> GetDescriptionAsync(
Expand All @@ -40,21 +42,19 @@ protected override async Task<string> GetDescriptionAsync(
return string.Format(CSharpCodeFixesResources.Make_0_return_Task_instead_of_void, methodNode!.WithBody(null));
}

protected override async Task<Tuple<SyntaxTree, SyntaxNode>?> GetRootInOtherSyntaxTreeAsync(
protected override async Task<(SyntaxTree syntaxTree, SyntaxNode root)?> GetRootInOtherSyntaxTreeAsync(
SyntaxNode node,
SemanticModel semanticModel,
Diagnostic diagnostic,
CancellationToken cancellationToken)
{
var methodDeclaration = await GetMethodDeclarationAsync(node, semanticModel, cancellationToken).ConfigureAwait(false);
if (methodDeclaration == null)
{
return null;
}

var oldRoot = await methodDeclaration.SyntaxTree.GetRootAsync(cancellationToken).ConfigureAwait(false);
var newRoot = oldRoot.ReplaceNode(methodDeclaration, ConvertToAsyncFunction(methodDeclaration));
return Tuple.Create(oldRoot.SyntaxTree, newRoot);
return (oldRoot.SyntaxTree, newRoot);
}

private static async Task<MethodDeclarationSyntax?> GetMethodDeclarationAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
// 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;
using System.Collections.Immutable;
using System.Composition;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.CodeGeneration;
Expand Down Expand Up @@ -44,7 +43,7 @@ protected override bool TryInitializeConstructorInitializerGeneration(
CancellationToken cancellationToken,
out SyntaxToken token,
out ImmutableArray<Argument<ExpressionSyntax>> arguments,
out INamedTypeSymbol typeToGenerateIn)
[NotNullWhen(true)] out INamedTypeSymbol? typeToGenerateIn)
{
var constructorInitializer = (ConstructorInitializerSyntax)node;

Expand All @@ -57,7 +56,7 @@ protected override bool TryInitializeConstructorInitializerGeneration(
var currentType = semanticModel.GetEnclosingNamedType(constructorInitializer.SpanStart, cancellationToken);
typeToGenerateIn = constructorInitializer.IsKind(SyntaxKind.ThisConstructorInitializer)
? currentType
: currentType.BaseType.OriginalDefinition;
: currentType?.BaseType?.OriginalDefinition;
return typeToGenerateIn != null;
}

Expand All @@ -82,11 +81,11 @@ protected override bool TryInitializeSimpleNameGenerationState(
CancellationToken cancellationToken,
out SyntaxToken token,
out ImmutableArray<Argument<ExpressionSyntax>> arguments,
out INamedTypeSymbol typeToGenerateIn)
[NotNullWhen(true)] out INamedTypeSymbol? typeToGenerateIn)
{
var simpleName = (SimpleNameSyntax)node;
var fullName = simpleName.IsRightSideOfQualifiedName()
? (NameSyntax)simpleName.Parent
? (NameSyntax)simpleName.GetRequiredParent()
: simpleName;

if (fullName.Parent is ObjectCreationExpressionSyntax objectCreationExpression)
Expand Down Expand Up @@ -114,11 +113,11 @@ protected override bool TryInitializeSimpleAttributeNameGenerationState(
CancellationToken cancellationToken,
out SyntaxToken token,
out ImmutableArray<Argument<ExpressionSyntax>> arguments,
out INamedTypeSymbol typeToGenerateIn)
[NotNullWhen(true)] out INamedTypeSymbol? typeToGenerateIn)
{
var simpleName = (SimpleNameSyntax)node;
var fullName = simpleName.IsRightSideOfQualifiedName()
? (NameSyntax)simpleName.Parent
? (NameSyntax)simpleName.GetRequiredParent()
: simpleName;

if (fullName.Parent is AttributeSyntax attribute)
Expand All @@ -132,7 +131,7 @@ protected override bool TryInitializeSimpleAttributeNameGenerationState(
token = simpleName.Identifier;
arguments = GetArguments(attribute.ArgumentList.Arguments);

typeToGenerateIn = symbolInfo.CandidateSymbols.FirstOrDefault().ContainingSymbol as INamedTypeSymbol;
typeToGenerateIn = symbolInfo.CandidateSymbols.FirstOrDefault()?.ContainingSymbol as INamedTypeSymbol;
return typeToGenerateIn != null;
}
}
Expand All @@ -149,7 +148,7 @@ protected override bool TryInitializeImplicitObjectCreation(SemanticDocument doc
CancellationToken cancellationToken,
out SyntaxToken token,
out ImmutableArray<Argument<ExpressionSyntax>> arguments,
out INamedTypeSymbol typeToGenerateIn)
[NotNullWhen(true)] out INamedTypeSymbol? typeToGenerateIn)
{
var implicitObjectCreation = (ImplicitObjectCreationExpressionSyntax)node;
if (implicitObjectCreation.ArgumentList != null &&
Expand All @@ -175,15 +174,15 @@ protected override string GenerateNameForExpression(SemanticModel semanticModel,
=> semanticModel.GenerateNameForExpression(expression, capitalize: false, cancellationToken: cancellationToken);

protected override ITypeSymbol GetArgumentType(SemanticModel semanticModel, Argument<ExpressionSyntax> argument, CancellationToken cancellationToken)
=> InternalExtensions.DetermineParameterType(argument.Expression, semanticModel, cancellationToken);
=> InternalExtensions.DetermineParameterType(argument.Expression!, semanticModel, cancellationToken);

protected override bool IsConversionImplicit(Compilation compilation, ITypeSymbol sourceType, ITypeSymbol targetType)
=> compilation.ClassifyConversion(sourceType, targetType).IsImplicit;

protected override IMethodSymbol GetCurrentConstructor(SemanticModel semanticModel, SyntaxToken token, CancellationToken cancellationToken)
protected override IMethodSymbol? GetCurrentConstructor(SemanticModel semanticModel, SyntaxToken token, CancellationToken cancellationToken)
=> token.GetAncestor<ConstructorDeclarationSyntax>() is { } constructor ? semanticModel.GetDeclaredSymbol(constructor, cancellationToken) : null;

protected override IMethodSymbol GetDelegatedConstructor(SemanticModel semanticModel, IMethodSymbol constructor, CancellationToken cancellationToken)
protected override IMethodSymbol? GetDelegatedConstructor(SemanticModel semanticModel, IMethodSymbol constructor, CancellationToken cancellationToken)
{
if (constructor.DeclaringSyntaxReferences[0].GetSyntax(cancellationToken) is ConstructorDeclarationSyntax constructorDeclarationSyntax &&
constructorDeclarationSyntax.Initializer.IsKind(SyntaxKind.ThisConstructorInitializer))
Expand Down
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 Down Expand Up @@ -42,27 +40,18 @@ internal sealed class GenerateConstructorCodeFixProvider() : AbstractGenerateMem
protected override Task<ImmutableArray<CodeAction>> GetCodeActionsAsync(
Document document, SyntaxNode node, CancellationToken cancellationToken)
{
var service = document.GetLanguageService<IGenerateConstructorService>();
var service = document.GetRequiredLanguageService<IGenerateConstructorService>();
return service.GenerateConstructorAsync(document, node, cancellationToken);
}

protected override bool IsCandidate(SyntaxNode node, SyntaxToken token, Diagnostic diagnostic)
{
return node is BaseObjectCreationExpressionSyntax or
ConstructorInitializerSyntax or
AttributeSyntax;
}
=> node is BaseObjectCreationExpressionSyntax or ConstructorInitializerSyntax or AttributeSyntax;

protected override SyntaxNode GetTargetNode(SyntaxNode node)
{
switch (node)
protected override SyntaxNode? GetTargetNode(SyntaxNode node)
=> node switch
{
case ObjectCreationExpressionSyntax objectCreationNode:
return objectCreationNode.Type.GetRightmostName();
case AttributeSyntax attributeNode:
return attributeNode.Name;
}

return node;
}
ObjectCreationExpressionSyntax objectCreationNode => objectCreationNode.Type.GetRightmostName(),
AttributeSyntax attributeNode => attributeNode.Name,
_ => node,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
// 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;
using System.Composition;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.GenerateMember.GenerateEnumMember;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Shared.Extensions;

namespace Microsoft.CodeAnalysis.CSharp.GenerateMember.GenerateEnumMember;

Expand All @@ -24,7 +24,8 @@ protected override bool IsIdentifierNameGeneration(SyntaxNode node)

protected override bool TryInitializeIdentifierNameState(
SemanticDocument document, SimpleNameSyntax identifierName, CancellationToken cancellationToken,
out SyntaxToken identifierToken, out ExpressionSyntax simpleNameOrMemberAccessExpression)
out SyntaxToken identifierToken,
[NotNullWhen(true)] out ExpressionSyntax? simpleNameOrMemberAccessExpression)
{
identifierToken = identifierName.Identifier;
if (identifierToken.ValueText != string.Empty &&
Expand All @@ -37,7 +38,7 @@ protected override bool TryInitializeIdentifierNameState(
// If we're being invoked, then don't offer this, offer generate method instead.
// Note: we could offer to generate a field with a delegate type. However, that's
// very esoteric and probably not what most users want.
if (simpleNameOrMemberAccessExpression.Parent.Kind()
if (simpleNameOrMemberAccessExpression.GetRequiredParent().Kind()
is SyntaxKind.InvocationExpression
or SyntaxKind.ObjectCreationExpression
or SyntaxKind.GotoStatement
Expand Down
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;
Expand All @@ -20,24 +18,16 @@ namespace Microsoft.CodeAnalysis.CSharp.CodeFixes.GenerateDeconstructMethod;

[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.GenerateDeconstructMethod), Shared]
[ExtensionOrder(After = PredefinedCodeFixProviderNames.GenerateEnumMember)]
internal class GenerateDeconstructMethodCodeFixProvider : 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 GenerateDeconstructMethodCodeFixProvider() : CodeFixProvider
{
private const string CS8129 = nameof(CS8129); // No suitable Deconstruct instance or extension method was found...

[ImportingConstructor]
[SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
public GenerateDeconstructMethodCodeFixProvider()
{
}
public override FixAllProvider? GetFixAllProvider() => base.GetFixAllProvider();

public sealed override ImmutableArray<string> FixableDiagnosticIds => [CS8129];

public override FixAllProvider GetFixAllProvider()
{
// Fix All is not supported by this code fix
return null;
}

public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
// Not supported in REPL
Expand All @@ -48,7 +38,7 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)

var document = context.Document;
var cancellationToken = context.CancellationToken;
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var span = context.Span;
var token = root.FindToken(span.Start);

Expand All @@ -61,10 +51,10 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
return;
}

var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var model = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);

DeconstructionInfo info;
ITypeSymbol type;
ITypeSymbol? type;
SyntaxNode target;
switch (deconstruction)
{
Expand All @@ -80,7 +70,7 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
break;
case PositionalPatternClauseSyntax positionalPattern:
info = default;
type = model.GetTypeInfo(deconstruction.Parent).Type;
type = model.GetTypeInfo(deconstruction.GetRequiredParent()).Type;
target = deconstruction;
break;
default:
Expand All @@ -105,7 +95,7 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
return;
}

var service = document.GetLanguageService<IGenerateDeconstructMemberService>();
var service = document.GetRequiredLanguageService<IGenerateDeconstructMemberService>();
var codeActions = await service.GenerateDeconstructMethodAsync(document, target, (INamedTypeSymbol)type, cancellationToken).ConfigureAwait(false);

Debug.Assert(!codeActions.IsDefault);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
// 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;
using System.Collections.Generic;
using System.Composition;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using Microsoft.CodeAnalysis.CodeGeneration;
using Microsoft.CodeAnalysis.CSharp.Extensions;
Expand Down Expand Up @@ -57,8 +56,8 @@ protected override bool TryInitializeImplicitConversionState(
ISet<TypeKind> classInterfaceModuleStructTypes,
CancellationToken cancellationToken,
out SyntaxToken identifierToken,
out IMethodSymbol methodSymbol,
out INamedTypeSymbol typeToGenerateIn)
[NotNullWhen(true)] out IMethodSymbol? methodSymbol,
[NotNullWhen(true)] out INamedTypeSymbol? typeToGenerateIn)
{
if (TryGetConversionMethodAndTypeToGenerateIn(document, expression, classInterfaceModuleStructTypes, cancellationToken, out methodSymbol, out typeToGenerateIn))
{
Expand All @@ -83,8 +82,8 @@ protected override bool TryInitializeExplicitConversionState(
ISet<TypeKind> classInterfaceModuleStructTypes,
CancellationToken cancellationToken,
out SyntaxToken identifierToken,
out IMethodSymbol methodSymbol,
out INamedTypeSymbol typeToGenerateIn)
[NotNullWhen(true)] out IMethodSymbol? methodSymbol,
[NotNullWhen(true)] out INamedTypeSymbol? typeToGenerateIn)
{
if (TryGetConversionMethodAndTypeToGenerateIn(document, expression, classInterfaceModuleStructTypes, cancellationToken, out methodSymbol, out typeToGenerateIn))
{
Expand All @@ -108,8 +107,8 @@ private static bool TryGetConversionMethodAndTypeToGenerateIn(
SyntaxNode expression,
ISet<TypeKind> classInterfaceModuleStructTypes,
CancellationToken cancellationToken,
out IMethodSymbol methodSymbol,
out INamedTypeSymbol typeToGenerateIn)
[NotNullWhen(true)] out IMethodSymbol? methodSymbol,
[NotNullWhen(true)] out INamedTypeSymbol? typeToGenerateIn)
{
if (expression is CastExpressionSyntax castExpression)
{
Expand All @@ -136,8 +135,8 @@ private static bool TryGetExplicitConversionMethodAndTypeToGenerateIn(
CastExpressionSyntax castExpression,
ISet<TypeKind> classInterfaceModuleStructTypes,
CancellationToken cancellationToken,
out IMethodSymbol methodSymbol,
out INamedTypeSymbol typeToGenerateIn)
[NotNullWhen(true)] out IMethodSymbol? methodSymbol,
[NotNullWhen(true)] out INamedTypeSymbol? typeToGenerateIn)
{
methodSymbol = null;
typeToGenerateIn = document.SemanticModel.GetTypeInfo(castExpression.Type, cancellationToken).Type as INamedTypeSymbol;
Expand Down Expand Up @@ -167,8 +166,8 @@ private static bool TryGetImplicitConversionMethodAndTypeToGenerateIn(
SyntaxNode expression,
ISet<TypeKind> classInterfaceModuleStructTypes,
CancellationToken cancellationToken,
out IMethodSymbol methodSymbol,
out INamedTypeSymbol typeToGenerateIn)
[NotNullWhen(true)] out IMethodSymbol? methodSymbol,
[NotNullWhen(true)] out INamedTypeSymbol? typeToGenerateIn)
{
methodSymbol = null;
typeToGenerateIn = document.SemanticModel.GetTypeInfo(expression, cancellationToken).ConvertedType as INamedTypeSymbol;
Expand Down
Loading
Loading