Skip to content

Commit

Permalink
Merge pull request #57934 from RikkiGibson/pnc-refactoring
Browse files Browse the repository at this point in the history
Use !! in AddParameterCheck refactoring
  • Loading branch information
RikkiGibson authored Dec 8, 2021
1 parent 7854604 commit ed3de52
Show file tree
Hide file tree
Showing 13 changed files with 276 additions and 41 deletions.
3 changes: 3 additions & 0 deletions src/Compilers/CSharp/Portable/LanguageVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ public static class LanguageVersionFacts
/// Usages of TestOptions.RegularNext and LanguageVersionFacts.CSharpNext
/// will be replaced with TestOptions.RegularN and LanguageVersion.CSharpN when language version N is introduced.
/// </summary>
/// <remarks>
/// Corresponds to Microsoft.CodeAnalysis.CSharp.Shared.Extensions.LanguageVersionExtensions.CSharpNext.
/// </remarks>
internal const LanguageVersion CSharpNext = LanguageVersion.Preview;

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion src/Compilers/Core/Portable/Symbols/IParameterSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ public interface IParameterSymbol : ISymbol
/// source or metadata.
/// </summary>
new IParameterSymbol OriginalDefinition { get; }

/// <summary>
/// True if the compiler will synthesize a null check for this parameter (the parameter is declared in source with a '!' following the parameter name).
/// True if the compiler will synthesize a null check for this parameter (the parameter is declared in source with a <c>!!</c> following the parameter name).
/// </summary>
bool IsNullChecked { get; }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

Expand All @@ -8,6 +8,7 @@
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Shared.Extensions;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.CodeAnalysis.Testing;
using Roslyn.Test.Utilities;
Expand All @@ -30,6 +31,77 @@ public async Task TestEmptyFile()

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInitializeParameter)]
public async Task TestSimpleReferenceType()
{
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!!)
{
}
}"
}.RunAsync();
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInitializeParameter)]
public async Task TestSimpleReferenceType_AlreadyNullChecked1()
{
var testCode = @"
using System;
class C
{
public C([||]string s!!)
{
}
}";
await new VerifyCS.Test
{
LanguageVersion = LanguageVersionExtensions.CSharpNext,
TestCode = testCode,
FixedCode = testCode
}.RunAsync();
}

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

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInitializeParameter)]
public async Task TestSimpleReferenceType_CSharp8()
{
await VerifyCS.VerifyRefactoringAsync(
@"
Expand Down Expand Up @@ -204,8 +276,11 @@ class C
await VerifyCS.VerifyRefactoringAsync(code, code);
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInitializeParameter)]
public async Task TestNotOnPartialMethodDefinition1()
[Theory]
[Trait(Traits.Feature, Traits.Features.CodeActionsInitializeParameter)]
[InlineData(LanguageVersionExtensions.CSharpNext)]
[InlineData(LanguageVersion.CSharp8)]
public async Task TestNotOnPartialMethodDefinition1(LanguageVersion languageVersion)
{
var code = @"
using System;
Expand All @@ -218,7 +293,12 @@ partial void M(string s)
{
}
}";
await VerifyCS.VerifyRefactoringAsync(code, code);
await new VerifyCS.Test
{
LanguageVersion = languageVersion,
TestCode = code,
FixedCode = code
}.RunAsync();
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInitializeParameter)]
Expand All @@ -243,8 +323,11 @@ public partial void M(string s)
}.RunAsync();
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInitializeParameter)]
public async Task TestNotOnPartialMethodDefinition2()
[Theory]
[Trait(Traits.Feature, Traits.Features.CodeActionsInitializeParameter)]
[InlineData(LanguageVersionExtensions.CSharpNext)]
[InlineData(LanguageVersion.CSharp8)]
public async Task TestNotOnPartialMethodDefinition2(LanguageVersion languageVersion)
{
var code = @"
using System;
Expand All @@ -257,7 +340,12 @@ partial void M(string s)
partial void M([||]string s);
}";
await VerifyCS.VerifyRefactoringAsync(code, code);
await new VerifyCS.Test
{
LanguageVersion = languageVersion,
TestCode = code,
FixedCode = code
}.RunAsync();
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInitializeParameter)]
Expand All @@ -284,6 +372,37 @@ public partial void M(string s)

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInitializeParameter)]
public async Task TestOnPartialMethodImplementation1()
{
await new VerifyCS.Test
{
LanguageVersion = LanguageVersionExtensions.CSharpNext,
TestCode = @"
using System;
partial class C
{
partial void M(string s);
partial void M([||]string s)
{
}
}",
FixedCode = @"
using System;
partial class C
{
partial void M(string s);
partial void M(string s!!)
{
}
}"
}.RunAsync();
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInitializeParameter)]
public async Task TestOnPartialMethodImplementation1_CSharp8()
{
await VerifyCS.VerifyRefactoringAsync(
@"
Expand Down Expand Up @@ -351,6 +470,37 @@ public partial void M(string s)

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInitializeParameter)]
public async Task TestOnPartialMethodImplementation2()
{
await new VerifyCS.Test
{
LanguageVersion = LanguageVersionExtensions.CSharpNext,
TestCode = @"
using System;
partial class C
{
partial void M([||]string s)
{
}
partial void M(string s);
}",
FixedCode = @"
using System;
partial class C
{
partial void M(string s!!)
{
}
partial void M(string s);
}"
}.RunAsync();
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInitializeParameter)]
public async Task TestOnPartialMethodImplementation2_CSharp9()
{
await VerifyCS.VerifyRefactoringAsync(
@"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System;
using System.Composition;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using System.Threading;
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Shared.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.InitializeParameter;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;

namespace Microsoft.CodeAnalysis.CSharp.InitializeParameter
Expand All @@ -40,8 +41,8 @@ protected override bool IsFunctionDeclaration(SyntaxNode node)
protected override SyntaxNode GetBody(SyntaxNode functionDeclaration)
=> InitializeParameterHelpers.GetBody(functionDeclaration);

protected override void InsertStatement(SyntaxEditor editor, SyntaxNode functionDeclaration, bool returnsVoid, SyntaxNode statementToAddAfterOpt, StatementSyntax statement)
=> InitializeParameterHelpers.InsertStatement(editor, functionDeclaration, returnsVoid, statementToAddAfterOpt, statement);
protected override void InsertStatement(SyntaxEditor editor, SyntaxNode functionDeclaration, bool returnsVoid, SyntaxNode? statementToAddAfter, StatementSyntax statement)
=> InitializeParameterHelpers.InsertStatement(editor, functionDeclaration, returnsVoid, statementToAddAfter, statement);

protected override bool IsImplicitConversion(Compilation compilation, ITypeSymbol source, ITypeSymbol destination)
=> InitializeParameterHelpers.IsImplicitConversion(compilation, source, destination);
Expand Down Expand Up @@ -92,5 +93,22 @@ protected override StatementSyntax CreateParameterCheckIfStatement(DocumentOptio
statement: ifTrueStatement,
@else: null);
}

protected override Document? TryAddNullCheckToParameterDeclaration(Document document, ParameterSyntax parameterSyntax, CancellationToken cancellationToken)
{
var tree = parameterSyntax.SyntaxTree;
var options = (CSharpParseOptions)tree.Options;
if (options.LanguageVersion < LanguageVersionExtensions.CSharpNext)
{
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);
syntaxRoot = syntaxRoot.ReplaceNode(
parameterSyntax,
parameterSyntax.WithExclamationExclamationToken(Token(SyntaxKind.ExclamationExclamationToken)));
return document.WithSyntaxRoot(syntaxRoot);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System.Composition;
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeRefactorings;
Expand Down Expand Up @@ -33,11 +31,11 @@ public CSharpInitializeMemberFromParameterCodeRefactoringProvider()
protected override bool IsFunctionDeclaration(SyntaxNode node)
=> InitializeParameterHelpers.IsFunctionDeclaration(node);

protected override SyntaxNode TryGetLastStatement(IBlockOperation blockStatementOpt)
=> InitializeParameterHelpers.TryGetLastStatement(blockStatementOpt);
protected override SyntaxNode? TryGetLastStatement(IBlockOperation? blockStatement)
=> InitializeParameterHelpers.TryGetLastStatement(blockStatement);

protected override void InsertStatement(SyntaxEditor editor, SyntaxNode functionDeclaration, bool returnsVoid, SyntaxNode statementToAddAfterOpt, StatementSyntax statement)
=> InitializeParameterHelpers.InsertStatement(editor, functionDeclaration, returnsVoid, statementToAddAfterOpt, statement);
protected override void InsertStatement(SyntaxEditor editor, SyntaxNode functionDeclaration, bool returnsVoid, SyntaxNode? statementToAddAfter, StatementSyntax statement)
=> InitializeParameterHelpers.InsertStatement(editor, functionDeclaration, returnsVoid, statementToAddAfter, statement);

protected override bool IsImplicitConversion(Compilation compilation, ITypeSymbol source, ITypeSymbol destination)
=> InitializeParameterHelpers.IsImplicitConversion(compilation, source, destination);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ public static SyntaxNode GetBody(SyntaxNode functionDeclaration)
public static bool IsImplicitConversion(Compilation compilation, ITypeSymbol source, ITypeSymbol destination)
=> compilation.ClassifyConversion(source: source, destination: destination).IsImplicit;

public static SyntaxNode? TryGetLastStatement(IBlockOperation blockStatementOpt)
=> blockStatementOpt?.Syntax is BlockSyntax block
public static SyntaxNode? TryGetLastStatement(IBlockOperation? blockStatement)
=> blockStatement?.Syntax is BlockSyntax block
? block.Statements.LastOrDefault()
: blockStatementOpt?.Syntax;
: blockStatement?.Syntax;

public static void InsertStatement(
SyntaxEditor editor,
SyntaxNode functionDeclaration,
bool returnsVoid,
SyntaxNode statementToAddAfterOpt,
SyntaxNode? statementToAddAfterOpt,
StatementSyntax statement)
{
var body = GetBody(functionDeclaration);
Expand Down
Loading

0 comments on commit ed3de52

Please sign in to comment.