-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Use !! in AddParameterCheck refactoring #57934
Use !! in AddParameterCheck refactoring #57934
Conversation
...atures/CSharp/Portable/InitializeParameter/CSharpAddParameterCheckCodeRefactoringProvider.cs
Outdated
Show resolved
Hide resolved
...atures/CSharp/Portable/InitializeParameter/CSharpAddParameterCheckCodeRefactoringProvider.cs
Outdated
Show resolved
Hide resolved
@@ -5775,6 +5775,28 @@ static object F() | |||
await AssertFormatAsync(expectedCode, code); | |||
} | |||
|
|||
[Fact] | |||
[Trait(Traits.Feature, Traits.Features.Formatting)] | |||
public async Task SpacingInNullCheckedParameter() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't forget to also test/handle the syntax normalize (SyntaxNormalizerTests.cs) which generally needs similar fixing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
{ | ||
foreach (var parameterNode in parameterNodes) | ||
{ | ||
var parameter = (IParameterSymbol)semanticModel.GetRequiredDeclaredSymbol(parameterNode, cancellationToken); | ||
if (index == parameter.Ordinal) | ||
{ | ||
return parameter; | ||
return ((TParameterSyntax)parameterNode, parameter); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this worries me. were we blindly casting before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Compiler changes LGTM.
/// <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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's an oversight. Thanks for catching it.
if (options.LanguageVersion < LanguageVersionExtensions.CSharpNext) | ||
{ | ||
return null; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[InlineData(LanguageVersionExtensions.CSharpNext)] | |
[InlineData(LanguageVersion.CSharp8)] | |
[CombinatorialData] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[InlineData(LanguageVersionExtensions.CSharpNext)] | |
[InlineData(LanguageVersion.CSharp8)] | |
[CombinatorialData] |
Related to #36024
@CyrusNajmabadi for review