Skip to content

Commit 6c6ee99

Browse files
authored
Enable nullable reference types (#1198)
1 parent 2214b4d commit 6c6ee99

File tree

213 files changed

+1875
-1686
lines changed

Some content is hidden

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

213 files changed

+1875
-1686
lines changed

ChangeLog.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- Add social card ([#1212](https://github.com/dotnet/roslynator/pull/1212)).
13+
- Add nullable annotation to public API ([#1198](https://github.com/JosefPihrt/Roslynator/pull/1198)).
1314

1415
### Changed
1516

src/Analyzers/CSharp/Analysis/UseConditionalAccessAnalyzer.cs

+3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ private static void AnalyzeIfStatement(SyntaxNodeAnalysisContext context)
6969

7070
SimpleMemberInvocationStatementInfo invocationInfo = SyntaxInfo.SimpleMemberInvocationStatementInfo(ifStatement.SingleNonBlockStatementOrDefault());
7171

72+
if (!invocationInfo.Success)
73+
return;
74+
7275
ExpressionSyntax expression2 = invocationInfo.Expression;
7376

7477
if (expression2 is null)

src/CSharp.Workspaces/CSharp.Workspaces.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<Version>$(RoslynatorCoreVersion)</Version>
99
<AssemblyName>$(RoslynatorDllPrefix)Roslynator.CSharp.Workspaces</AssemblyName>
1010
<RootNamespace>Roslynator</RootNamespace>
11+
<Nullable>enable</Nullable>
1112
</PropertyGroup>
1213

1314
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">

src/CSharp.Workspaces/CSharp/CSharpSyntaxFactsService.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
22

33
using System.Composition;
4+
using System.Diagnostics;
45
using Microsoft.CodeAnalysis;
56
using Microsoft.CodeAnalysis.CSharp;
67
using Microsoft.CodeAnalysis.Host;
@@ -59,9 +60,9 @@ public bool AreEquivalent(SyntaxTree oldTree, SyntaxTree newTree)
5960
return SyntaxFactory.AreEquivalent(oldTree, newTree, topLevel: false);
6061
}
6162

62-
public SyntaxNode GetSymbolDeclaration(SyntaxToken identifier)
63+
public SyntaxNode? GetSymbolDeclaration(SyntaxToken identifier)
6364
{
64-
SyntaxNode parent = identifier.Parent;
65+
SyntaxNode? parent = identifier.Parent;
6566

6667
if (!identifier.IsKind(SyntaxKind.IdentifierToken))
6768
return null;
@@ -109,6 +110,7 @@ public SyntaxNode GetSymbolDeclaration(SyntaxToken identifier)
109110
}
110111

111112
SyntaxDebug.Fail(parent);
113+
112114
return null;
113115
}
114116

src/CSharp.Workspaces/CSharp/CSharpTypeFactory.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ namespace Roslynator.CSharp;
77

88
internal static class CSharpTypeFactory
99
{
10-
private static TypeSyntax _boolType;
11-
private static TypeSyntax _intType;
12-
private static TypeSyntax _doubleType;
13-
private static TypeSyntax _stringType;
14-
private static TypeSyntax _objectType;
15-
private static TypeSyntax _notImplementedException;
16-
private static TypeSyntax _notSupportedException;
10+
private static TypeSyntax? _boolType;
11+
private static TypeSyntax? _intType;
12+
private static TypeSyntax? _doubleType;
13+
private static TypeSyntax? _stringType;
14+
private static TypeSyntax? _objectType;
15+
private static TypeSyntax? _notImplementedException;
16+
private static TypeSyntax? _notSupportedException;
1717

1818
public static TypeSyntax BoolType()
1919
{

src/CSharp.Workspaces/CSharp/CodeMetrics/CSharpPhysicalLinesWalker.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ private void VisitBrace(in SyntaxToken braceToken)
120120

121121
private void VisitBraceTrivia(in SyntaxToken braceToken)
122122
{
123-
SyntaxTree tree = braceToken.SyntaxTree;
123+
SyntaxTree tree = braceToken.SyntaxTree!;
124124

125125
if (AnalyzeLeadingTrivia(braceToken.LeadingTrivia)
126126
&& AnalyzeTrailingTrivia(braceToken.TrailingTrivia))

src/CSharp.Workspaces/CSharp/Extensions/WorkspaceExtensions.cs

+23-8
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ internal static bool SupportsLanguageFeature(this Document document, CSharpLangu
4242

4343
internal static bool SupportsLanguageVersion(this Document document, LanguageVersion languageVersion)
4444
{
45-
return ((CSharpParseOptions)document.Project.ParseOptions).LanguageVersion >= languageVersion;
45+
return ((CSharpParseOptions?)document.Project.ParseOptions)?.LanguageVersion >= languageVersion;
4646
}
4747

4848
internal static DefaultSyntaxOptions GetDefaultSyntaxOptions(this Document document, DefaultSyntaxOptions options = DefaultSyntaxOptions.None)
@@ -83,7 +83,7 @@ internal static Task<Document> RemoveMemberAsync(
8383
if (member is null)
8484
throw new ArgumentNullException(nameof(member));
8585

86-
SyntaxNode parent = member.Parent;
86+
SyntaxNode? parent = member.Parent;
8787

8888
switch (parent?.Kind())
8989
{
@@ -162,7 +162,10 @@ public static async Task<Document> RemoveCommentsAsync(
162162
if (document is null)
163163
throw new ArgumentNullException(nameof(document));
164164

165-
SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
165+
SyntaxNode? root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
166+
167+
if (root is null)
168+
return document;
166169

167170
SyntaxNode newRoot = SyntaxRefactorings.RemoveComments(root, comments)
168171
.WithFormatterAnnotation();
@@ -186,7 +189,10 @@ public static async Task<Document> RemoveCommentsAsync(
186189
if (document is null)
187190
throw new ArgumentNullException(nameof(document));
188191

189-
SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
192+
SyntaxNode? root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
193+
194+
if (root is null)
195+
return document;
190196

191197
SyntaxNode newRoot = SyntaxRefactorings.RemoveComments(root, span, comments)
192198
.WithFormatterAnnotation();
@@ -208,7 +214,10 @@ public static async Task<Document> RemoveTriviaAsync(
208214
if (document is null)
209215
throw new ArgumentNullException(nameof(document));
210216

211-
SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
217+
SyntaxNode? root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
218+
219+
if (root is null)
220+
return document;
212221

213222
SyntaxNode newRoot = SyntaxRefactorings.RemoveTrivia(root, span);
214223

@@ -229,7 +238,10 @@ public static async Task<Document> RemovePreprocessorDirectivesAsync(
229238
if (document is null)
230239
throw new ArgumentNullException(nameof(document));
231240

232-
SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
241+
SyntaxNode? root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
242+
243+
if (root is null)
244+
return document;
233245

234246
SourceText sourceText = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
235247

@@ -254,7 +266,10 @@ public static async Task<Document> RemovePreprocessorDirectivesAsync(
254266
if (document is null)
255267
throw new ArgumentNullException(nameof(document));
256268

257-
SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
269+
SyntaxNode? root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
270+
271+
if (root is null)
272+
return document;
258273

259274
SourceText sourceText = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
260275

@@ -400,7 +415,7 @@ internal static Task<Document> RemoveSingleLineDocumentationComment(
400415
DocumentationCommentTriviaSyntax documentationComment,
401416
CancellationToken cancellationToken = default)
402417
{
403-
SyntaxNode node = documentationComment.ParentTrivia.Token.Parent;
418+
SyntaxNode node = documentationComment.ParentTrivia.Token.Parent!;
404419
SyntaxNode newNode = SyntaxRefactorings.RemoveSingleLineDocumentationComment(node, documentationComment);
405420

406421
return document.ReplaceNodeAsync(node, newNode, cancellationToken);

src/CSharp.Workspaces/CSharp/Extensions/WorkspaceSymbolExtensions.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static class WorkspaceSymbolExtensions
1919
public static ExpressionSyntax GetDefaultValueSyntax(
2020
this ITypeSymbol typeSymbol,
2121
DefaultSyntaxOptions options = DefaultSyntaxOptions.None,
22-
SymbolDisplayFormat format = null)
22+
SymbolDisplayFormat? format = null)
2323
{
2424
return GetDefaultValueSyntax(typeSymbol, options, default(TypeSyntax), format);
2525
}
@@ -42,8 +42,8 @@ public static ExpressionSyntax GetDefaultValueSyntax(
4242
private static ExpressionSyntax GetDefaultValueSyntax(
4343
this ITypeSymbol typeSymbol,
4444
DefaultSyntaxOptions options = DefaultSyntaxOptions.None,
45-
TypeSyntax type = null,
46-
SymbolDisplayFormat format = null)
45+
TypeSyntax? type = null,
46+
SymbolDisplayFormat? format = null)
4747
{
4848
if (typeSymbol is null)
4949
throw new ArgumentNullException(nameof(typeSymbol));
@@ -60,7 +60,7 @@ private static ExpressionSyntax GetDefaultValueSyntax(
6060

6161
if (typeSymbol.TypeKind == TypeKind.Enum)
6262
{
63-
IFieldSymbol fieldSymbol = CSharpUtility.FindEnumDefaultField((INamedTypeSymbol)typeSymbol);
63+
IFieldSymbol? fieldSymbol = CSharpUtility.FindEnumDefaultField((INamedTypeSymbol)typeSymbol);
6464

6565
if (fieldSymbol is not null)
6666
return SimpleMemberAccessExpression(GetTypeSyntax(), IdentifierName(fieldSymbol.Name));
@@ -106,15 +106,15 @@ ExpressionSyntax CreateDefault()
106106

107107
internal static ExpressionSyntax GetDefaultValueSyntax(
108108
this IParameterSymbol parameterSymbol,
109-
SymbolDisplayFormat format = null)
109+
SymbolDisplayFormat? format = null)
110110
{
111111
if (parameterSymbol is null)
112112
throw new ArgumentNullException(nameof(parameterSymbol));
113113

114114
if (!parameterSymbol.HasExplicitDefaultValue)
115115
throw new ArgumentException("Parameter does not specify default value.", nameof(parameterSymbol));
116116

117-
object value = parameterSymbol.ExplicitDefaultValue;
117+
object? value = parameterSymbol.ExplicitDefaultValue;
118118

119119
ITypeSymbol typeSymbol = parameterSymbol.Type;
120120

@@ -123,7 +123,7 @@ internal static ExpressionSyntax GetDefaultValueSyntax(
123123
if (value is null)
124124
return NullLiteralExpression();
125125

126-
IFieldSymbol fieldSymbol = FindFieldWithConstantValue();
126+
IFieldSymbol? fieldSymbol = FindFieldWithConstantValue();
127127

128128
TypeSyntax type = typeSymbol.ToTypeSyntax(format);
129129

@@ -145,7 +145,7 @@ internal static ExpressionSyntax GetDefaultValueSyntax(
145145

146146
return LiteralExpression(value);
147147

148-
IFieldSymbol FindFieldWithConstantValue()
148+
IFieldSymbol? FindFieldWithConstantValue()
149149
{
150150
foreach (ISymbol symbol in typeSymbol.GetMembers())
151151
{

src/CSharp.Workspaces/CSharp/FindSymbols/CSharpFindSymbolService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal class CSharpFindSymbolService : FindSymbolService
1717
{
1818
public override ISyntaxFactsService SyntaxFacts => CSharpSyntaxFactsService.Instance;
1919

20-
public override SyntaxNode FindDeclaration(SyntaxNode node)
20+
public override SyntaxNode? FindDeclaration(SyntaxNode node)
2121
{
2222
return node.FirstAncestorOrSelf(
2323
n =>

src/CSharp.Workspaces/CSharp/FindSymbols/LocalSymbolFinder.cs

+14-8
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,23 @@ public static ImmutableArray<ISymbol> FindLocalSymbols(
3131
{
3232
var eventDeclaration = (EventDeclarationSyntax)node;
3333

34-
foreach (AccessorDeclarationSyntax accessor in eventDeclaration.AccessorList.Accessors)
35-
walker.Visit(accessor.BodyOrExpressionBody());
34+
if (eventDeclaration.AccessorList is not null)
35+
{
36+
foreach (AccessorDeclarationSyntax accessor in eventDeclaration.AccessorList.Accessors)
37+
walker.Visit(accessor.BodyOrExpressionBody());
38+
}
3639

3740
break;
3841
}
3942
case SyntaxKind.IndexerDeclaration:
4043
{
4144
var indexerDeclaration = (IndexerDeclarationSyntax)node;
4245

43-
foreach (AccessorDeclarationSyntax accessor in indexerDeclaration.AccessorList.Accessors)
44-
walker.Visit(accessor.BodyOrExpressionBody());
46+
if (indexerDeclaration.AccessorList is not null)
47+
{
48+
foreach (AccessorDeclarationSyntax accessor in indexerDeclaration.AccessorList.Accessors)
49+
walker.Visit(accessor.BodyOrExpressionBody());
50+
}
4551

4652
break;
4753
}
@@ -57,13 +63,13 @@ public static ImmutableArray<ISymbol> FindLocalSymbols(
5763
{
5864
var propertyDeclaration = (PropertyDeclarationSyntax)node;
5965

60-
ArrowExpressionClauseSyntax expressionBody = propertyDeclaration.ExpressionBody;
66+
ArrowExpressionClauseSyntax? expressionBody = propertyDeclaration.ExpressionBody;
6167

6268
if (expressionBody is not null)
6369
{
6470
walker.Visit(expressionBody);
6571
}
66-
else
72+
else if (propertyDeclaration.AccessorList is not null)
6773
{
6874
foreach (AccessorDeclarationSyntax accessor in propertyDeclaration.AccessorList.Accessors)
6975
walker.Visit(accessor.BodyOrExpressionBody());
@@ -75,7 +81,7 @@ public static ImmutableArray<ISymbol> FindLocalSymbols(
7581
{
7682
var declarator = (VariableDeclaratorSyntax)node;
7783

78-
ExpressionSyntax expression = declarator.Initializer?.Value;
84+
ExpressionSyntax? expression = declarator.Initializer?.Value;
7985

8086
if (expression is not null)
8187
walker.Visit(expression);
@@ -133,7 +139,7 @@ public LocalSymbolCollector(
133139

134140
public override void VisitLocal(SyntaxNode node)
135141
{
136-
ISymbol symbol = SemanticModel.GetDeclaredSymbol(node, CancellationToken);
142+
ISymbol? symbol = SemanticModel.GetDeclaredSymbol(node, CancellationToken);
137143

138144
if (symbol is not null)
139145
{

src/CSharp.Workspaces/CSharp/Spelling/CSharpSpellingDiagnostic.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public CSharpSpellingDiagnostic(
1212
Diagnostic diagnostic,
1313
string value,
1414
int valueIndex,
15-
string containingValue,
15+
string? containingValue,
1616
int containingValueIndex,
1717
SyntaxToken identifier = default) : base(diagnostic, value, valueIndex, containingValue, containingValueIndex, identifier)
1818
{

src/CSharp.Workspaces/CSharp/Spelling/CSharpSpellingService.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ public override ImmutableArray<Diagnostic> AnalyzeSpelling(
5353
public override SpellingDiagnostic CreateSpellingDiagnostic(Diagnostic diagnostic)
5454
{
5555
Location location = diagnostic.Location;
56-
SyntaxTree syntaxTree = location.SourceTree;
56+
SyntaxTree syntaxTree = location.SourceTree!;
5757
SyntaxNode root = syntaxTree.GetRoot();
5858
TextSpan span = location.SourceSpan;
5959

60-
string value = diagnostic.Properties["Value"];
60+
string value = diagnostic.Properties["Value"]!;
6161
int index = location.SourceSpan.Start;
62-
string parent = diagnostic.Properties.GetValueOrDefault("Parent");
62+
string? parent = diagnostic.Properties.GetValueOrDefault("Parent");
6363

64-
int parentIndex = (diagnostic.Properties.TryGetValue("ParentIndex", out string parentIndexText))
64+
int parentIndex = (diagnostic.Properties.TryGetValue("ParentIndex", out string? parentIndexText))
6565
? int.Parse(parentIndexText)
6666
: 0;
6767

src/CSharp.Workspaces/CSharp/Spelling/CSharpSpellingWalker.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public override void VisitTrivia(SyntaxTrivia trivia)
6161
case SyntaxKind.MultiLineCommentTrivia:
6262
{
6363
if (ShouldVisit(SpellingScopeFilter.NonDocumentationComment))
64-
AnalyzeText(trivia.ToString(), trivia.SyntaxTree, trivia.Span);
64+
AnalyzeText(trivia.ToString(), trivia.SyntaxTree!, trivia.Span);
6565

6666
break;
6767
}
@@ -85,7 +85,7 @@ public override void VisitTrivia(SyntaxTrivia trivia)
8585
{
8686
Debug.Assert(ShouldVisit(SpellingScopeFilter.Region));
8787

88-
AnalyzeText(trivia.ToString(), trivia.SyntaxTree, trivia.Span);
88+
AnalyzeText(trivia.ToString(), trivia.SyntaxTree!, trivia.Span);
8989
break;
9090
}
9191
}

src/CSharp.Workspaces/CSharp/SyntaxInverter.cs

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Roslynator.CSharp;
1313

14+
#nullable disable
15+
1416
/// <summary>
1517
/// Provides static methods for syntax inversion.
1618
/// </summary>

0 commit comments

Comments
 (0)