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

Respect PreferParameterNullChecking user option #59561

Merged
merged 5 commits into from
Feb 16, 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 @@ -29,8 +29,10 @@ public async Task TestEmptyFile()
await VerifyCS.VerifyRefactoringAsync(code, code);
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInitializeParameter)]
public async Task TestSimpleReferenceType()
[Theory, Trait(Traits.Feature, Traits.Features.CodeActionsInitializeParameter)]
[InlineData("csharp_style_prefer_parameter_null_checking = true")]
[InlineData("")]
public async Task TestSimpleReferenceType(string editorConfig)
{
await new VerifyCS.Test
{
Expand All @@ -52,7 +54,46 @@ class C
public C(string s!!)
{
}
}"
}",
EditorConfig = $@"
[*]
{editorConfig}
"
}.RunAsync();
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInitializeParameter)]
public async Task TestDoNotPreferParameterNullChecking()
{
await new VerifyCS.Test
{
LanguageVersion = LanguageVersionExtensions.CSharpNext,
TestCode = @"
using System;
class C
{
public C([||]string s)
{
}
}",
FixedCode = @"
using System;
class C
{
public C(string s)
{
if (s is null)
{
throw new ArgumentNullException(nameof(s));
}
}
}",
EditorConfig = @"
[*]
csharp_style_prefer_parameter_null_checking = false
",
}.RunAsync();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Composition;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Extensions;
Expand Down Expand Up @@ -102,14 +103,18 @@ protected override StatementSyntax CreateParameterCheckIfStatement(DocumentOptio
@else: null);
}

protected override Document? TryAddNullCheckToParameterDeclaration(Document document, ParameterSyntax parameterSyntax, CancellationToken cancellationToken)
protected override async Task<Document?> TryAddNullCheckToParameterDeclarationAsync(Document document, ParameterSyntax parameterSyntax, CancellationToken cancellationToken)
{
var tree = parameterSyntax.SyntaxTree;
if (!tree.Options.LanguageVersion().IsCSharp11OrAbove())
return null;

var options = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);
if (!options.GetOption(CSharpCodeStyleOptions.PreferParameterNullChecking).Value)
return null;

// We expect the syntax tree to already be in memory since we already have a node from the tree
var syntaxRoot = tree.GetRoot(cancellationToken);
var syntaxRoot = await tree.GetRootAsync(cancellationToken).ConfigureAwait(false);
syntaxRoot = syntaxRoot.ReplaceNode(
parameterSyntax,
parameterSyntax.WithExclamationExclamationToken(Token(SyntaxKind.ExclamationExclamationToken)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal abstract partial class AbstractAddParameterCheckCodeRefactoringProvider
protected abstract bool PrefersThrowExpression(DocumentOptionSet options);
protected abstract string EscapeResourceString(string input);
protected abstract TStatementSyntax CreateParameterCheckIfStatement(DocumentOptionSet options, TExpressionSyntax condition, TStatementSyntax ifTrueStatement);
protected abstract Document? TryAddNullCheckToParameterDeclaration(Document document, TParameterSyntax parameterSyntax, CancellationToken cancellationToken);
protected abstract Task<Document?> TryAddNullCheckToParameterDeclarationAsync(Document document, TParameterSyntax parameterSyntax, CancellationToken cancellationToken);

protected override async Task<ImmutableArray<CodeAction>> GetRefactoringsForAllParametersAsync(
Document document,
Expand Down Expand Up @@ -331,7 +331,7 @@ private async Task<Document> AddNullCheckAsync(
CancellationToken cancellationToken)
{
// First see if we can adopt the '!!' parameter null checking syntax.
var modifiedDocument = TryAddNullCheckToParameterDeclaration(document, parameterSyntax, cancellationToken);
var modifiedDocument = await TryAddNullCheckToParameterDeclarationAsync(document, parameterSyntax, cancellationToken).ConfigureAwait(false);
if (modifiedDocument != null)
{
return modifiedDocument;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.InitializeParameter
elseBlock:=Nothing)
End Function

Protected Overrides Function TryAddNullCheckToParameterDeclaration(document As Document, parameterSyntax As ParameterSyntax, cancellationToken As CancellationToken) As Document
Return Nothing
Protected Overrides Function TryAddNullCheckToParameterDeclarationAsync(document As Document, parameterSyntax As ParameterSyntax, cancellationToken As CancellationToken) As Task(Of Document)
Return Task.FromResult(Of Document)(Nothing)
End Function
End Class
End Namespace