Skip to content

Commit 4a77333

Browse files
Nullable enable IDE. (#77842)
2 parents 2bad1a5 + 25aba1a commit 4a77333

File tree

44 files changed

+204
-265
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+204
-265
lines changed

src/Analyzers/CSharp/Analyzers/RemoveUnusedParametersAndValues/CSharpRemoveUnusedParametersAndValuesDiagnosticAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected override bool MethodHasHandlesClause(IMethodSymbol method)
3535
protected override bool IsIfConditionalDirective(SyntaxNode node)
3636
=> node is IfDirectiveTriviaSyntax;
3737

38-
protected override bool ReturnsThrow(SyntaxNode node)
38+
protected override bool ReturnsThrow(SyntaxNode? node)
3939
{
4040
if (node is not BaseMethodDeclarationSyntax methodSyntax)
4141
{

src/Analyzers/CSharp/Analyzers/UseExpressionBody/Helpers/UseExpressionBodyForIndexersHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
namespace Microsoft.CodeAnalysis.CSharp.UseExpressionBody;
1616

17-
internal class UseExpressionBodyForIndexersHelper :
17+
internal sealed class UseExpressionBodyForIndexersHelper :
1818
UseExpressionBodyHelper<IndexerDeclarationSyntax>
1919
{
2020
public static readonly UseExpressionBodyForIndexersHelper Instance = new();

src/Analyzers/CSharp/CodeFixes/ConvertToAsync/CSharpConvertToAsyncMethodCodeFixProvider.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ internal sealed class CSharpConvertToAsyncMethodCodeFixProvider() : AbstractConv
2525
/// </summary>
2626
private const string CS4008 = nameof(CS4008);
2727

28+
public override FixAllProvider? GetFixAllProvider() => base.GetFixAllProvider();
29+
2830
public override ImmutableArray<string> FixableDiagnosticIds => [CS4008];
2931

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

43-
protected override async Task<Tuple<SyntaxTree, SyntaxNode>?> GetRootInOtherSyntaxTreeAsync(
45+
protected override async Task<(SyntaxTree syntaxTree, SyntaxNode root)?> GetRootInOtherSyntaxTreeAsync(
4446
SyntaxNode node,
4547
SemanticModel semanticModel,
4648
Diagnostic diagnostic,
4749
CancellationToken cancellationToken)
4850
{
4951
var methodDeclaration = await GetMethodDeclarationAsync(node, semanticModel, cancellationToken).ConfigureAwait(false);
5052
if (methodDeclaration == null)
51-
{
5253
return null;
53-
}
5454

5555
var oldRoot = await methodDeclaration.SyntaxTree.GetRootAsync(cancellationToken).ConfigureAwait(false);
5656
var newRoot = oldRoot.ReplaceNode(methodDeclaration, ConvertToAsyncFunction(methodDeclaration));
57-
return Tuple.Create(oldRoot.SyntaxTree, newRoot);
57+
return (oldRoot.SyntaxTree, newRoot);
5858
}
5959

6060
private static async Task<MethodDeclarationSyntax?> GetMethodDeclarationAsync(

src/Analyzers/CSharp/CodeFixes/GenerateConstructor/CSharpGenerateConstructorService.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
#nullable disable
6-
75
using System;
86
using System.Collections.Immutable;
97
using System.Composition;
8+
using System.Diagnostics.CodeAnalysis;
109
using System.Linq;
1110
using System.Threading;
1211
using Microsoft.CodeAnalysis.CodeGeneration;
@@ -44,7 +43,7 @@ protected override bool TryInitializeConstructorInitializerGeneration(
4443
CancellationToken cancellationToken,
4544
out SyntaxToken token,
4645
out ImmutableArray<Argument<ExpressionSyntax>> arguments,
47-
out INamedTypeSymbol typeToGenerateIn)
46+
[NotNullWhen(true)] out INamedTypeSymbol? typeToGenerateIn)
4847
{
4948
var constructorInitializer = (ConstructorInitializerSyntax)node;
5049

@@ -57,7 +56,7 @@ protected override bool TryInitializeConstructorInitializerGeneration(
5756
var currentType = semanticModel.GetEnclosingNamedType(constructorInitializer.SpanStart, cancellationToken);
5857
typeToGenerateIn = constructorInitializer.IsKind(SyntaxKind.ThisConstructorInitializer)
5958
? currentType
60-
: currentType.BaseType.OriginalDefinition;
59+
: currentType?.BaseType?.OriginalDefinition;
6160
return typeToGenerateIn != null;
6261
}
6362

@@ -82,11 +81,11 @@ protected override bool TryInitializeSimpleNameGenerationState(
8281
CancellationToken cancellationToken,
8382
out SyntaxToken token,
8483
out ImmutableArray<Argument<ExpressionSyntax>> arguments,
85-
out INamedTypeSymbol typeToGenerateIn)
84+
[NotNullWhen(true)] out INamedTypeSymbol? typeToGenerateIn)
8685
{
8786
var simpleName = (SimpleNameSyntax)node;
8887
var fullName = simpleName.IsRightSideOfQualifiedName()
89-
? (NameSyntax)simpleName.Parent
88+
? (NameSyntax)simpleName.GetRequiredParent()
9089
: simpleName;
9190

9291
if (fullName.Parent is ObjectCreationExpressionSyntax objectCreationExpression)
@@ -114,11 +113,11 @@ protected override bool TryInitializeSimpleAttributeNameGenerationState(
114113
CancellationToken cancellationToken,
115114
out SyntaxToken token,
116115
out ImmutableArray<Argument<ExpressionSyntax>> arguments,
117-
out INamedTypeSymbol typeToGenerateIn)
116+
[NotNullWhen(true)] out INamedTypeSymbol? typeToGenerateIn)
118117
{
119118
var simpleName = (SimpleNameSyntax)node;
120119
var fullName = simpleName.IsRightSideOfQualifiedName()
121-
? (NameSyntax)simpleName.Parent
120+
? (NameSyntax)simpleName.GetRequiredParent()
122121
: simpleName;
123122

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

135-
typeToGenerateIn = symbolInfo.CandidateSymbols.FirstOrDefault().ContainingSymbol as INamedTypeSymbol;
134+
typeToGenerateIn = symbolInfo.CandidateSymbols.FirstOrDefault()?.ContainingSymbol as INamedTypeSymbol;
136135
return typeToGenerateIn != null;
137136
}
138137
}
@@ -149,7 +148,7 @@ protected override bool TryInitializeImplicitObjectCreation(SemanticDocument doc
149148
CancellationToken cancellationToken,
150149
out SyntaxToken token,
151150
out ImmutableArray<Argument<ExpressionSyntax>> arguments,
152-
out INamedTypeSymbol typeToGenerateIn)
151+
[NotNullWhen(true)] out INamedTypeSymbol? typeToGenerateIn)
153152
{
154153
var implicitObjectCreation = (ImplicitObjectCreationExpressionSyntax)node;
155154
if (implicitObjectCreation.ArgumentList != null &&
@@ -175,15 +174,15 @@ protected override string GenerateNameForExpression(SemanticModel semanticModel,
175174
=> semanticModel.GenerateNameForExpression(expression, capitalize: false, cancellationToken: cancellationToken);
176175

177176
protected override ITypeSymbol GetArgumentType(SemanticModel semanticModel, Argument<ExpressionSyntax> argument, CancellationToken cancellationToken)
178-
=> InternalExtensions.DetermineParameterType(argument.Expression, semanticModel, cancellationToken);
177+
=> InternalExtensions.DetermineParameterType(argument.Expression!, semanticModel, cancellationToken);
179178

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

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

186-
protected override IMethodSymbol GetDelegatedConstructor(SemanticModel semanticModel, IMethodSymbol constructor, CancellationToken cancellationToken)
185+
protected override IMethodSymbol? GetDelegatedConstructor(SemanticModel semanticModel, IMethodSymbol constructor, CancellationToken cancellationToken)
187186
{
188187
if (constructor.DeclaringSyntaxReferences[0].GetSyntax(cancellationToken) is ConstructorDeclarationSyntax constructorDeclarationSyntax &&
189188
constructorDeclarationSyntax.Initializer.IsKind(SyntaxKind.ThisConstructorInitializer))

src/Analyzers/CSharp/CodeFixes/GenerateConstructor/GenerateConstructorCodeFixProvider.cs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
#nullable disable
6-
75
using System.Collections.Immutable;
86
using System.Composition;
97
using System.Diagnostics.CodeAnalysis;
@@ -42,27 +40,18 @@ internal sealed class GenerateConstructorCodeFixProvider() : AbstractGenerateMem
4240
protected override Task<ImmutableArray<CodeAction>> GetCodeActionsAsync(
4341
Document document, SyntaxNode node, CancellationToken cancellationToken)
4442
{
45-
var service = document.GetLanguageService<IGenerateConstructorService>();
43+
var service = document.GetRequiredLanguageService<IGenerateConstructorService>();
4644
return service.GenerateConstructorAsync(document, node, cancellationToken);
4745
}
4846

4947
protected override bool IsCandidate(SyntaxNode node, SyntaxToken token, Diagnostic diagnostic)
50-
{
51-
return node is BaseObjectCreationExpressionSyntax or
52-
ConstructorInitializerSyntax or
53-
AttributeSyntax;
54-
}
48+
=> node is BaseObjectCreationExpressionSyntax or ConstructorInitializerSyntax or AttributeSyntax;
5549

56-
protected override SyntaxNode GetTargetNode(SyntaxNode node)
57-
{
58-
switch (node)
50+
protected override SyntaxNode? GetTargetNode(SyntaxNode node)
51+
=> node switch
5952
{
60-
case ObjectCreationExpressionSyntax objectCreationNode:
61-
return objectCreationNode.Type.GetRightmostName();
62-
case AttributeSyntax attributeNode:
63-
return attributeNode.Name;
64-
}
65-
66-
return node;
67-
}
53+
ObjectCreationExpressionSyntax objectCreationNode => objectCreationNode.Type.GetRightmostName(),
54+
AttributeSyntax attributeNode => attributeNode.Name,
55+
_ => node,
56+
};
6857
}

src/Analyzers/CSharp/CodeFixes/GenerateEnumMember/CSharpGenerateEnumMemberService.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
#nullable disable
6-
75
using System;
86
using System.Composition;
7+
using System.Diagnostics.CodeAnalysis;
98
using System.Threading;
109
using Microsoft.CodeAnalysis.CSharp.Syntax;
1110
using Microsoft.CodeAnalysis.GenerateMember.GenerateEnumMember;
1211
using Microsoft.CodeAnalysis.Host.Mef;
12+
using Microsoft.CodeAnalysis.Shared.Extensions;
1313

1414
namespace Microsoft.CodeAnalysis.CSharp.GenerateMember.GenerateEnumMember;
1515

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

2525
protected override bool TryInitializeIdentifierNameState(
2626
SemanticDocument document, SimpleNameSyntax identifierName, CancellationToken cancellationToken,
27-
out SyntaxToken identifierToken, out ExpressionSyntax simpleNameOrMemberAccessExpression)
27+
out SyntaxToken identifierToken,
28+
[NotNullWhen(true)] out ExpressionSyntax? simpleNameOrMemberAccessExpression)
2829
{
2930
identifierToken = identifierName.Identifier;
3031
if (identifierToken.ValueText != string.Empty &&
@@ -37,7 +38,7 @@ protected override bool TryInitializeIdentifierNameState(
3738
// If we're being invoked, then don't offer this, offer generate method instead.
3839
// Note: we could offer to generate a field with a delegate type. However, that's
3940
// very esoteric and probably not what most users want.
40-
if (simpleNameOrMemberAccessExpression.Parent.Kind()
41+
if (simpleNameOrMemberAccessExpression.GetRequiredParent().Kind()
4142
is SyntaxKind.InvocationExpression
4243
or SyntaxKind.ObjectCreationExpression
4344
or SyntaxKind.GotoStatement

src/Analyzers/CSharp/CodeFixes/GenerateMethod/GenerateDeconstructMethodCodeFixProvider.cs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
#nullable disable
6-
75
using System.Collections.Immutable;
86
using System.Composition;
97
using System.Diagnostics;
@@ -20,24 +18,16 @@ namespace Microsoft.CodeAnalysis.CSharp.CodeFixes.GenerateDeconstructMethod;
2018

2119
[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.GenerateDeconstructMethod), Shared]
2220
[ExtensionOrder(After = PredefinedCodeFixProviderNames.GenerateEnumMember)]
23-
internal class GenerateDeconstructMethodCodeFixProvider : CodeFixProvider
21+
[method: ImportingConstructor]
22+
[method: SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
23+
internal sealed class GenerateDeconstructMethodCodeFixProvider() : CodeFixProvider
2424
{
2525
private const string CS8129 = nameof(CS8129); // No suitable Deconstruct instance or extension method was found...
2626

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

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

35-
public override FixAllProvider GetFixAllProvider()
36-
{
37-
// Fix All is not supported by this code fix
38-
return null;
39-
}
40-
4131
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
4232
{
4333
// Not supported in REPL
@@ -48,7 +38,7 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
4838

4939
var document = context.Document;
5040
var cancellationToken = context.CancellationToken;
51-
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
41+
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
5242
var span = context.Span;
5343
var token = root.FindToken(span.Start);
5444

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

64-
var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
54+
var model = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);
6555

6656
DeconstructionInfo info;
67-
ITypeSymbol type;
57+
ITypeSymbol? type;
6858
SyntaxNode target;
6959
switch (deconstruction)
7060
{
@@ -80,7 +70,7 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
8070
break;
8171
case PositionalPatternClauseSyntax positionalPattern:
8272
info = default;
83-
type = model.GetTypeInfo(deconstruction.Parent).Type;
73+
type = model.GetTypeInfo(deconstruction.GetRequiredParent()).Type;
8474
target = deconstruction;
8575
break;
8676
default:
@@ -105,7 +95,7 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
10595
return;
10696
}
10797

108-
var service = document.GetLanguageService<IGenerateDeconstructMemberService>();
98+
var service = document.GetRequiredLanguageService<IGenerateDeconstructMemberService>();
10999
var codeActions = await service.GenerateDeconstructMethodAsync(document, target, (INamedTypeSymbol)type, cancellationToken).ConfigureAwait(false);
110100

111101
Debug.Assert(!codeActions.IsDefault);

src/Analyzers/CSharp/CodeFixes/GenerateParameterizedMember/CSharpGenerateConversionService.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
#nullable disable
6-
75
using System;
86
using System.Collections.Generic;
97
using System.Composition;
8+
using System.Diagnostics.CodeAnalysis;
109
using System.Threading;
1110
using Microsoft.CodeAnalysis.CodeGeneration;
1211
using Microsoft.CodeAnalysis.CSharp.Extensions;
@@ -57,8 +56,8 @@ protected override bool TryInitializeImplicitConversionState(
5756
ISet<TypeKind> classInterfaceModuleStructTypes,
5857
CancellationToken cancellationToken,
5958
out SyntaxToken identifierToken,
60-
out IMethodSymbol methodSymbol,
61-
out INamedTypeSymbol typeToGenerateIn)
59+
[NotNullWhen(true)] out IMethodSymbol? methodSymbol,
60+
[NotNullWhen(true)] out INamedTypeSymbol? typeToGenerateIn)
6261
{
6362
if (TryGetConversionMethodAndTypeToGenerateIn(document, expression, classInterfaceModuleStructTypes, cancellationToken, out methodSymbol, out typeToGenerateIn))
6463
{
@@ -83,8 +82,8 @@ protected override bool TryInitializeExplicitConversionState(
8382
ISet<TypeKind> classInterfaceModuleStructTypes,
8483
CancellationToken cancellationToken,
8584
out SyntaxToken identifierToken,
86-
out IMethodSymbol methodSymbol,
87-
out INamedTypeSymbol typeToGenerateIn)
85+
[NotNullWhen(true)] out IMethodSymbol? methodSymbol,
86+
[NotNullWhen(true)] out INamedTypeSymbol? typeToGenerateIn)
8887
{
8988
if (TryGetConversionMethodAndTypeToGenerateIn(document, expression, classInterfaceModuleStructTypes, cancellationToken, out methodSymbol, out typeToGenerateIn))
9089
{
@@ -108,8 +107,8 @@ private static bool TryGetConversionMethodAndTypeToGenerateIn(
108107
SyntaxNode expression,
109108
ISet<TypeKind> classInterfaceModuleStructTypes,
110109
CancellationToken cancellationToken,
111-
out IMethodSymbol methodSymbol,
112-
out INamedTypeSymbol typeToGenerateIn)
110+
[NotNullWhen(true)] out IMethodSymbol? methodSymbol,
111+
[NotNullWhen(true)] out INamedTypeSymbol? typeToGenerateIn)
113112
{
114113
if (expression is CastExpressionSyntax castExpression)
115114
{
@@ -136,8 +135,8 @@ private static bool TryGetExplicitConversionMethodAndTypeToGenerateIn(
136135
CastExpressionSyntax castExpression,
137136
ISet<TypeKind> classInterfaceModuleStructTypes,
138137
CancellationToken cancellationToken,
139-
out IMethodSymbol methodSymbol,
140-
out INamedTypeSymbol typeToGenerateIn)
138+
[NotNullWhen(true)] out IMethodSymbol? methodSymbol,
139+
[NotNullWhen(true)] out INamedTypeSymbol? typeToGenerateIn)
141140
{
142141
methodSymbol = null;
143142
typeToGenerateIn = document.SemanticModel.GetTypeInfo(castExpression.Type, cancellationToken).Type as INamedTypeSymbol;
@@ -167,8 +166,8 @@ private static bool TryGetImplicitConversionMethodAndTypeToGenerateIn(
167166
SyntaxNode expression,
168167
ISet<TypeKind> classInterfaceModuleStructTypes,
169168
CancellationToken cancellationToken,
170-
out IMethodSymbol methodSymbol,
171-
out INamedTypeSymbol typeToGenerateIn)
169+
[NotNullWhen(true)] out IMethodSymbol? methodSymbol,
170+
[NotNullWhen(true)] out INamedTypeSymbol? typeToGenerateIn)
172171
{
173172
methodSymbol = null;
174173
typeToGenerateIn = document.SemanticModel.GetTypeInfo(expression, cancellationToken).ConvertedType as INamedTypeSymbol;

0 commit comments

Comments
 (0)