Skip to content

Commit

Permalink
Respect PreferParameterNullChecking user option (#59561)
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 authored Feb 16, 2022
1 parent f0a81c8 commit 52bdc01
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 9 deletions.
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

0 comments on commit 52bdc01

Please sign in to comment.