Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cover additional cases where nullable directives can be removed #62043

Merged
merged 1 commit into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -9,6 +9,7 @@
using Microsoft.CodeAnalysis.CSharp.LanguageServices;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Shared.Collections;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;

namespace Microsoft.CodeAnalysis.CSharp.Analyzers.RemoveUnnecessaryNullableDirective
Expand Down Expand Up @@ -66,6 +67,60 @@ private bool IsIgnored(SyntaxNode node)
return false;
}

private static bool IsLanguageRestrictedToNonNullForm(TypeSyntax node)
{
// Simplify syntax checks by walking up qualified names to an equivalent parent node.
node = WalkUpCurrentQualifiedName(node);

if (node.IsParentKind(SyntaxKind.QualifiedName, out QualifiedNameSyntax? qualifiedName)
&& qualifiedName.Left == node)
{
// Cannot dot off a nullable reference type
return true;
}

if (node.IsParentKind(SyntaxKind.UsingDirective))
{
// Using directives cannot directly reference a nullable reference type
return true;
}

if (node.IsParentKind(SyntaxKind.SimpleBaseType))
{
// Cannot derive directly from a nullable reference type
return true;
}

if (node.IsParentKind(SyntaxKind.NamespaceDeclaration, SyntaxKind.FileScopedNamespaceDeclaration))
{
// Namespace names cannot be nullable reference types
return true;
}

if (node.IsParentKind(SyntaxKind.NameEquals) && node.Parent.IsParentKind(SyntaxKind.UsingDirective))
{
// This is the alias or the target type of a using alias directive, neither of which can be nullable
//
// using CustomException = System.Exception;
// ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^
return true;
}

return false;

// If this is Y in X.Y, walk up to X.Y
static TypeSyntax WalkUpCurrentQualifiedName(TypeSyntax node)
{
while (node.IsParentKind(SyntaxKind.QualifiedName, out QualifiedNameSyntax? qualifiedName)
&& qualifiedName.Right == node)
{
node = qualifiedName;
}

return node;
}
}

public void Dispose()
{
}
Expand All @@ -75,7 +130,8 @@ public override void DefaultVisit(SyntaxNode node)
if (IsIgnored(node))
return;

if (node is TypeSyntax typeSyntax)
if (node is TypeSyntax typeSyntax
&& !IsLanguageRestrictedToNonNullForm(typeSyntax))
{
if (typeSyntax.IsVar)
return;
Expand All @@ -93,7 +149,12 @@ public override void DefaultVisit(SyntaxNode node)
}

var symbolInfo = _semanticModel.GetSymbolInfo(typeSyntax, _cancellationToken);
if (symbolInfo.Symbol is INamedTypeSymbol { IsValueType: true, IsGenericType: false })
if (symbolInfo.Symbol.IsKind(SymbolKind.Namespace))
{
// Namespaces cannot be nullable
return;
}
else if (symbolInfo.Symbol is INamedTypeSymbol { IsValueType: true, IsGenericType: false })
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,36 @@ class Program
""");
}

[Fact]
public async Task TestRedundantDirectiveWithNamespaceAndDerivedType()
{
await VerifyCodeFixAsync(
NullableContextOptions.Enable,
"""
[|#nullable enable|]

using System;

namespace X.Y
{
class ProgramException : Exception
{
}
}
""",
"""

using System;

namespace X.Y
{
class ProgramException : Exception
{
}
}
""");
}

private static string GetDisableDirectiveContext(NullableContextOptions options)
{
return options switch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,84 @@ enum EnumName
""");
}

[Fact]
public async Task TestUnnecessaryDirectiveWithNamespaceAndDerivedType()
{
await VerifyCodeFixAsync(
NullableContextOptions.Enable,
"""
[|#nullable disable|]

using System;

namespace X.Y
{
class ProgramException : Exception
{
}
}
""",
"""

using System;

namespace X.Y
{
class ProgramException : Exception
{
}
}
""");
}

[Fact]
public async Task TestUnnecessaryDirectiveWithNamespaceAndDerivedFromQualifiedBaseType()
{
await VerifyCodeFixAsync(
NullableContextOptions.Enable,
"""
[|#nullable disable|]

namespace X.Y
{
class ProgramException : System.Exception
{
}
}
""",
"""

namespace X.Y
{
class ProgramException : System.Exception
{
}
}
""");
}

[Fact]
public async Task TestUnnecessaryDirectiveWithQualifiedUsingDirectives()
{
await VerifyCodeFixAsync(
NullableContextOptions.Enable,
"""
[|#nullable disable|]

using System;
using System.Runtime.InteropServices;
using CustomException = System.Exception;
using static System.String;
""",
"""

using System;
using System.Runtime.InteropServices;
using CustomException = System.Exception;
using static System.String;
""");
}

[Theory]
[InlineData("disable")]
[InlineData("restore")]
Expand Down