Skip to content

Commit

Permalink
Enable nullable reference types (#1198)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt committed Oct 15, 2023
1 parent 2214b4d commit 6c6ee99
Show file tree
Hide file tree
Showing 213 changed files with 1,875 additions and 1,686 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

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

### Changed

Expand Down
3 changes: 3 additions & 0 deletions src/Analyzers/CSharp/Analysis/UseConditionalAccessAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ private static void AnalyzeIfStatement(SyntaxNodeAnalysisContext context)

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

if (!invocationInfo.Success)
return;

ExpressionSyntax expression2 = invocationInfo.Expression;

if (expression2 is null)
Expand Down
1 change: 1 addition & 0 deletions src/CSharp.Workspaces/CSharp.Workspaces.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<Version>$(RoslynatorCoreVersion)</Version>
<AssemblyName>$(RoslynatorDllPrefix)Roslynator.CSharp.Workspaces</AssemblyName>
<RootNamespace>Roslynator</RootNamespace>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand Down
6 changes: 4 additions & 2 deletions src/CSharp.Workspaces/CSharp/CSharpSyntaxFactsService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Composition;
using System.Diagnostics;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Host;
Expand Down Expand Up @@ -59,9 +60,9 @@ public bool AreEquivalent(SyntaxTree oldTree, SyntaxTree newTree)
return SyntaxFactory.AreEquivalent(oldTree, newTree, topLevel: false);
}

public SyntaxNode GetSymbolDeclaration(SyntaxToken identifier)
public SyntaxNode? GetSymbolDeclaration(SyntaxToken identifier)
{
SyntaxNode parent = identifier.Parent;
SyntaxNode? parent = identifier.Parent;

if (!identifier.IsKind(SyntaxKind.IdentifierToken))
return null;
Expand Down Expand Up @@ -109,6 +110,7 @@ public SyntaxNode GetSymbolDeclaration(SyntaxToken identifier)
}

SyntaxDebug.Fail(parent);

return null;
}

Expand Down
14 changes: 7 additions & 7 deletions src/CSharp.Workspaces/CSharp/CSharpTypeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ namespace Roslynator.CSharp;

internal static class CSharpTypeFactory
{
private static TypeSyntax _boolType;
private static TypeSyntax _intType;
private static TypeSyntax _doubleType;
private static TypeSyntax _stringType;
private static TypeSyntax _objectType;
private static TypeSyntax _notImplementedException;
private static TypeSyntax _notSupportedException;
private static TypeSyntax? _boolType;
private static TypeSyntax? _intType;
private static TypeSyntax? _doubleType;
private static TypeSyntax? _stringType;
private static TypeSyntax? _objectType;
private static TypeSyntax? _notImplementedException;
private static TypeSyntax? _notSupportedException;

public static TypeSyntax BoolType()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private void VisitBrace(in SyntaxToken braceToken)

private void VisitBraceTrivia(in SyntaxToken braceToken)
{
SyntaxTree tree = braceToken.SyntaxTree;
SyntaxTree tree = braceToken.SyntaxTree!;

if (AnalyzeLeadingTrivia(braceToken.LeadingTrivia)
&& AnalyzeTrailingTrivia(braceToken.TrailingTrivia))
Expand Down
31 changes: 23 additions & 8 deletions src/CSharp.Workspaces/CSharp/Extensions/WorkspaceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal static bool SupportsLanguageFeature(this Document document, CSharpLangu

internal static bool SupportsLanguageVersion(this Document document, LanguageVersion languageVersion)
{
return ((CSharpParseOptions)document.Project.ParseOptions).LanguageVersion >= languageVersion;
return ((CSharpParseOptions?)document.Project.ParseOptions)?.LanguageVersion >= languageVersion;
}

internal static DefaultSyntaxOptions GetDefaultSyntaxOptions(this Document document, DefaultSyntaxOptions options = DefaultSyntaxOptions.None)
Expand Down Expand Up @@ -83,7 +83,7 @@ internal static DefaultSyntaxOptions GetDefaultSyntaxOptions(this Document docum
if (member is null)
throw new ArgumentNullException(nameof(member));

SyntaxNode parent = member.Parent;
SyntaxNode? parent = member.Parent;

switch (parent?.Kind())
{
Expand Down Expand Up @@ -162,7 +162,10 @@ internal static DefaultSyntaxOptions GetDefaultSyntaxOptions(this Document docum
if (document is null)
throw new ArgumentNullException(nameof(document));

SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
SyntaxNode? root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

if (root is null)
return document;

SyntaxNode newRoot = SyntaxRefactorings.RemoveComments(root, comments)
.WithFormatterAnnotation();
Expand All @@ -186,7 +189,10 @@ internal static DefaultSyntaxOptions GetDefaultSyntaxOptions(this Document docum
if (document is null)
throw new ArgumentNullException(nameof(document));

SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
SyntaxNode? root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

if (root is null)
return document;

SyntaxNode newRoot = SyntaxRefactorings.RemoveComments(root, span, comments)
.WithFormatterAnnotation();
Expand All @@ -208,7 +214,10 @@ internal static DefaultSyntaxOptions GetDefaultSyntaxOptions(this Document docum
if (document is null)
throw new ArgumentNullException(nameof(document));

SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
SyntaxNode? root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

if (root is null)
return document;

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

Expand All @@ -229,7 +238,10 @@ internal static DefaultSyntaxOptions GetDefaultSyntaxOptions(this Document docum
if (document is null)
throw new ArgumentNullException(nameof(document));

SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
SyntaxNode? root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

if (root is null)
return document;

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

Expand All @@ -254,7 +266,10 @@ internal static DefaultSyntaxOptions GetDefaultSyntaxOptions(this Document docum
if (document is null)
throw new ArgumentNullException(nameof(document));

SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
SyntaxNode? root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

if (root is null)
return document;

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

Expand Down Expand Up @@ -400,7 +415,7 @@ bool ShouldRemoveDirective(DirectiveTriviaSyntax directive)
DocumentationCommentTriviaSyntax documentationComment,
CancellationToken cancellationToken = default)
{
SyntaxNode node = documentationComment.ParentTrivia.Token.Parent;
SyntaxNode node = documentationComment.ParentTrivia.Token.Parent!;
SyntaxNode newNode = SyntaxRefactorings.RemoveSingleLineDocumentationComment(node, documentationComment);

return document.ReplaceNodeAsync(node, newNode, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static class WorkspaceSymbolExtensions
public static ExpressionSyntax GetDefaultValueSyntax(
this ITypeSymbol typeSymbol,
DefaultSyntaxOptions options = DefaultSyntaxOptions.None,
SymbolDisplayFormat format = null)
SymbolDisplayFormat? format = null)
{
return GetDefaultValueSyntax(typeSymbol, options, default(TypeSyntax), format);
}
Expand All @@ -42,8 +42,8 @@ public static class WorkspaceSymbolExtensions
private static ExpressionSyntax GetDefaultValueSyntax(
this ITypeSymbol typeSymbol,
DefaultSyntaxOptions options = DefaultSyntaxOptions.None,
TypeSyntax type = null,
SymbolDisplayFormat format = null)
TypeSyntax? type = null,
SymbolDisplayFormat? format = null)
{
if (typeSymbol is null)
throw new ArgumentNullException(nameof(typeSymbol));
Expand All @@ -60,7 +60,7 @@ public static class WorkspaceSymbolExtensions

if (typeSymbol.TypeKind == TypeKind.Enum)
{
IFieldSymbol fieldSymbol = CSharpUtility.FindEnumDefaultField((INamedTypeSymbol)typeSymbol);
IFieldSymbol? fieldSymbol = CSharpUtility.FindEnumDefaultField((INamedTypeSymbol)typeSymbol);

if (fieldSymbol is not null)
return SimpleMemberAccessExpression(GetTypeSyntax(), IdentifierName(fieldSymbol.Name));
Expand Down Expand Up @@ -106,15 +106,15 @@ ExpressionSyntax CreateDefault()

internal static ExpressionSyntax GetDefaultValueSyntax(
this IParameterSymbol parameterSymbol,
SymbolDisplayFormat format = null)
SymbolDisplayFormat? format = null)
{
if (parameterSymbol is null)
throw new ArgumentNullException(nameof(parameterSymbol));

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

object value = parameterSymbol.ExplicitDefaultValue;
object? value = parameterSymbol.ExplicitDefaultValue;

ITypeSymbol typeSymbol = parameterSymbol.Type;

Expand All @@ -123,7 +123,7 @@ ExpressionSyntax CreateDefault()
if (value is null)
return NullLiteralExpression();

IFieldSymbol fieldSymbol = FindFieldWithConstantValue();
IFieldSymbol? fieldSymbol = FindFieldWithConstantValue();

TypeSyntax type = typeSymbol.ToTypeSyntax(format);

Expand All @@ -145,7 +145,7 @@ ExpressionSyntax CreateDefault()

return LiteralExpression(value);

IFieldSymbol FindFieldWithConstantValue()
IFieldSymbol? FindFieldWithConstantValue()
{
foreach (ISymbol symbol in typeSymbol.GetMembers())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal class CSharpFindSymbolService : FindSymbolService
{
public override ISyntaxFactsService SyntaxFacts => CSharpSyntaxFactsService.Instance;

public override SyntaxNode FindDeclaration(SyntaxNode node)
public override SyntaxNode? FindDeclaration(SyntaxNode node)
{
return node.FirstAncestorOrSelf(
n =>
Expand Down
22 changes: 14 additions & 8 deletions src/CSharp.Workspaces/CSharp/FindSymbols/LocalSymbolFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,23 @@ internal static class LocalSymbolFinder
{
var eventDeclaration = (EventDeclarationSyntax)node;

foreach (AccessorDeclarationSyntax accessor in eventDeclaration.AccessorList.Accessors)
walker.Visit(accessor.BodyOrExpressionBody());
if (eventDeclaration.AccessorList is not null)
{
foreach (AccessorDeclarationSyntax accessor in eventDeclaration.AccessorList.Accessors)
walker.Visit(accessor.BodyOrExpressionBody());
}

break;
}
case SyntaxKind.IndexerDeclaration:
{
var indexerDeclaration = (IndexerDeclarationSyntax)node;

foreach (AccessorDeclarationSyntax accessor in indexerDeclaration.AccessorList.Accessors)
walker.Visit(accessor.BodyOrExpressionBody());
if (indexerDeclaration.AccessorList is not null)
{
foreach (AccessorDeclarationSyntax accessor in indexerDeclaration.AccessorList.Accessors)
walker.Visit(accessor.BodyOrExpressionBody());
}

break;
}
Expand All @@ -57,13 +63,13 @@ internal static class LocalSymbolFinder
{
var propertyDeclaration = (PropertyDeclarationSyntax)node;

ArrowExpressionClauseSyntax expressionBody = propertyDeclaration.ExpressionBody;
ArrowExpressionClauseSyntax? expressionBody = propertyDeclaration.ExpressionBody;

if (expressionBody is not null)
{
walker.Visit(expressionBody);
}
else
else if (propertyDeclaration.AccessorList is not null)
{
foreach (AccessorDeclarationSyntax accessor in propertyDeclaration.AccessorList.Accessors)
walker.Visit(accessor.BodyOrExpressionBody());
Expand All @@ -75,7 +81,7 @@ internal static class LocalSymbolFinder
{
var declarator = (VariableDeclaratorSyntax)node;

ExpressionSyntax expression = declarator.Initializer?.Value;
ExpressionSyntax? expression = declarator.Initializer?.Value;

if (expression is not null)
walker.Visit(expression);
Expand Down Expand Up @@ -133,7 +139,7 @@ private class LocalSymbolCollector : LocalWalker

public override void VisitLocal(SyntaxNode node)
{
ISymbol symbol = SemanticModel.GetDeclaredSymbol(node, CancellationToken);
ISymbol? symbol = SemanticModel.GetDeclaredSymbol(node, CancellationToken);

if (symbol is not null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal sealed class CSharpSpellingDiagnostic : SpellingDiagnostic
Diagnostic diagnostic,
string value,
int valueIndex,
string containingValue,
string? containingValue,
int containingValueIndex,
SyntaxToken identifier = default) : base(diagnostic, value, valueIndex, containingValue, containingValueIndex, identifier)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ internal partial class CSharpSpellingService : SpellingService
public override SpellingDiagnostic CreateSpellingDiagnostic(Diagnostic diagnostic)
{
Location location = diagnostic.Location;
SyntaxTree syntaxTree = location.SourceTree;
SyntaxTree syntaxTree = location.SourceTree!;
SyntaxNode root = syntaxTree.GetRoot();
TextSpan span = location.SourceSpan;

string value = diagnostic.Properties["Value"];
string value = diagnostic.Properties["Value"]!;
int index = location.SourceSpan.Start;
string parent = diagnostic.Properties.GetValueOrDefault("Parent");
string? parent = diagnostic.Properties.GetValueOrDefault("Parent");

int parentIndex = (diagnostic.Properties.TryGetValue("ParentIndex", out string parentIndexText))
int parentIndex = (diagnostic.Properties.TryGetValue("ParentIndex", out string? parentIndexText))
? int.Parse(parentIndexText)
: 0;

Expand Down
4 changes: 2 additions & 2 deletions src/CSharp.Workspaces/CSharp/Spelling/CSharpSpellingWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public override void VisitTrivia(SyntaxTrivia trivia)
case SyntaxKind.MultiLineCommentTrivia:
{
if (ShouldVisit(SpellingScopeFilter.NonDocumentationComment))
AnalyzeText(trivia.ToString(), trivia.SyntaxTree, trivia.Span);
AnalyzeText(trivia.ToString(), trivia.SyntaxTree!, trivia.Span);

break;
}
Expand All @@ -85,7 +85,7 @@ public override void VisitTrivia(SyntaxTrivia trivia)
{
Debug.Assert(ShouldVisit(SpellingScopeFilter.Region));

AnalyzeText(trivia.ToString(), trivia.SyntaxTree, trivia.Span);
AnalyzeText(trivia.ToString(), trivia.SyntaxTree!, trivia.Span);
break;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/CSharp.Workspaces/CSharp/SyntaxInverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Roslynator.CSharp;

#nullable disable

/// <summary>
/// Provides static methods for syntax inversion.
/// </summary>
Expand Down

0 comments on commit 6c6ee99

Please sign in to comment.