Use !! in AddParameterCheck refactoring#57934
Use !! in AddParameterCheck refactoring#57934RikkiGibson merged 7 commits intodotnet:features/param-nullcheckingfrom
Conversation
...atures/CSharp/Portable/InitializeParameter/CSharpAddParameterCheckCodeRefactoringProvider.cs
Outdated
Show resolved
Hide resolved
...atures/CSharp/Portable/InitializeParameter/CSharpAddParameterCheckCodeRefactoringProvider.cs
Outdated
Show resolved
Hide resolved
|
|
||
| [Fact] | ||
| [Trait(Traits.Feature, Traits.Features.Formatting)] | ||
| public async Task SpacingInNullCheckedParameter() |
There was a problem hiding this comment.
Don't forget to also test/handle the syntax normalize (SyntaxNormalizerTests.cs) which generally needs similar fixing.
There was a problem hiding this comment.
I didn't see a similar test in SyntaxNormalizerTests for the ! operator. Didn't we have to do something similar with that for formatting?
There was a problem hiding this comment.
It wasn't clear to me what needs to be done here.
src/EditorFeatures/CSharpTest/InitializeParameter/AddParameterCheckTests.cs
Show resolved
Hide resolved
...atures/CSharp/Portable/InitializeParameter/CSharpAddParameterCheckCodeRefactoringProvider.cs
Outdated
Show resolved
Hide resolved
| currentToken.Parent.IsKind(SyntaxKind.Parameter)) | ||
| { | ||
| return CreateAdjustSpacesOperation(0, AdjustSpacesOption.ForceSpacesIfOnSingleLine); | ||
| } |
There was a problem hiding this comment.
Consider also adding a test to SyntacticClassifierTests.cs and SemanticQuickInfoSourceTests.cs. This way, the PR covers all IDE basics (formatting, classifier, quick info, since completion is N/A).
...atures/CSharp/Portable/InitializeParameter/CSharpAddParameterCheckCodeRefactoringProvider.cs
Outdated
Show resolved
Hide resolved
...es/SharedUtilitiesAndExtensions/Compiler/CSharp/Formatting/Rules/TokenBasedFormattingRule.cs
Outdated
Show resolved
Hide resolved
...atures/Core/Portable/InitializeParameter/AbstractAddParameterCheckCodeRefactoringProvider.cs
Outdated
Show resolved
Hide resolved
| }"; | ||
| await new VerifyCS.Test | ||
| { | ||
| LanguageVersion = LanguageVersion.Preview, |
There was a problem hiding this comment.
For the cases where the test is the same other than changing language version, consider using a xUnit theory instead of a fact to avoid all the extra duplication.
jasonmalinowski
left a comment
There was a problem hiding this comment.
Some comments from me, but wait until @CyrusNajmabadi signs off.
...atures/CSharp/Portable/InitializeParameter/CSharpAddParameterCheckCodeRefactoringProvider.cs
Outdated
Show resolved
Hide resolved
...atures/Core/Portable/InitializeParameter/AbstractAddParameterCheckCodeRefactoringProvider.cs
Outdated
Show resolved
Hide resolved
jcouv
left a comment
There was a problem hiding this comment.
Compiler-side (comments-only change) LGTM Thanks (iteration 4)
...atures/CSharp/Portable/InitializeParameter/CSharpAddParameterCheckCodeRefactoringProvider.cs
Outdated
Show resolved
Hide resolved
src/EditorFeatures/CSharpTest/InitializeParameter/AddParameterCheckTests.cs
Outdated
Show resolved
Hide resolved
src/EditorFeatures/CSharpTest/InitializeParameter/AddParameterCheckTests.cs
Outdated
Show resolved
Hide resolved
...atures/CSharp/Portable/InitializeParameter/CSharpAddParameterCheckCodeRefactoringProvider.cs
Outdated
Show resolved
Hide resolved
| if (index == parameter.Ordinal) | ||
| { | ||
| return parameter; | ||
| return ((TParameterSyntax)parameterNode, parameter); |
There was a problem hiding this comment.
this worries me. were we blindly casting before?
There was a problem hiding this comment.
I think we were not casting the items in parameterNodes before this PR.
It appears parameterNodes contains a SeparatedSyntaxList<ParameterNode> which was converted to an IReadOnlyList<SyntaxNode> within SyntaxGenerator.GetParameters.
I would be open to any refactoring/additional helpers which would make this code safer/more maintainable.
There was a problem hiding this comment.
I changed to simply cast the list of parameter nodes which I think will make it more clear what exactly our assumption is.
|
Could I please get a second review on the relatively small (I think only comments) changes on the @dotnet/roslyn-compiler side? |
|
|
||
| /// <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 '!!' following the parameter name). |
There was a problem hiding this comment.
nit: I don't know whether this should be <c>!!</c> or not. This will render differently on docs.microsoft.com.
There was a problem hiding this comment.
Perhaps it would make sense to have a tracking issue to verify the rendering/appearance of documentation comments, and perform adjustments like the one you've suggested.
| { | ||
| await new VerifyCS.Test | ||
| { | ||
| LanguageVersion = LanguageVersion.Preview, |
There was a problem hiding this comment.
Why is this Preview and not the new CSharpNext? I understand both are equivalent, but CSharpNext should help updating this to CSharp11 when released.
There was a problem hiding this comment.
It's an oversight. Thanks for catching it.
| if (options.LanguageVersion < LanguageVersionExtensions.CSharpNext) | ||
| { | ||
| return null; | ||
| } |
There was a problem hiding this comment.
I was a bit surprised to see this. My expectation is that we would let the operation proceed without syntax checks and let later diagnostics catch that. Is there any other place where we have syntax based ops that depend on /langVersion?
There was a problem hiding this comment.
this is very normal on the IDE side. remember that the user is just saying they want to add a "null check", not that they want to add !!. The IDE normally takes a stance with these types of operations on choosing the most idiomatic form that is legal for the version of the language the user is on.
That way users on C#10 will get the best option they can use, wihle those on 11 will get !!.
If users explicitly write out the C#11 version, then our 'upgrade project' analyzer kicks in and will help them upgrade if they want.
| [InlineData(LanguageVersionExtensions.CSharpNext)] | ||
| [InlineData(LanguageVersion.CSharp8)] |
There was a problem hiding this comment.
| [InlineData(LanguageVersionExtensions.CSharpNext)] | |
| [InlineData(LanguageVersion.CSharp8)] | |
| [CombinatorialData] |
There was a problem hiding this comment.
This ends up giving us unexpected diagnostics about use of partial methods in C# 2.
| [InlineData(LanguageVersionExtensions.CSharpNext)] | ||
| [InlineData(LanguageVersion.CSharp8)] |
There was a problem hiding this comment.
| [InlineData(LanguageVersionExtensions.CSharpNext)] | |
| [InlineData(LanguageVersion.CSharp8)] | |
| [CombinatorialData] |
Related to #36024
@CyrusNajmabadi for review