"Add null check" inserts single line statements if preferred#52462
"Add null check" inserts single line statements if preferred#52462sharwell merged 13 commits intodotnet:mainfrom
Conversation
|
It seems, roslyn/src/Workspaces/Core/Portable/CodeActions/CodeAction.cs Lines 283 to 297 in 9dad013 Afterwards the document looks like this ( using System;
class C
{
public C(object o)
{
if(oisnull)thrownew ArgumentNullException(nameof(o)); }
}The next change happens in using System;
class C
{
public C(object o)
{
if (o is null)
throw new ArgumentNullException(nameof(o));
}
}It seems The propable cause is the It does not respect @CyrusNajmabadi Am I on the right track here. Should IndentBlockFormattingRule be changed to respect |
'Allow' does not mean 'require'. It just means it is on if the user does that. The fix is to check for both of these and not have elastic trivia after the close paren or before the throw keyword. Thanks! |
But shouldn't
I tried to find the |
|
Okay. I found the |
No. Because the general pattern in the ecosystem is to still place them on multiple lines. This is jsut a way for clients to say if it's should be considered illegal to be on the same line. Not that it is preferred that they be on the same line. They're very different concepts. |
Yes. i would not include the trivia between those two tokens. :) I wouldn't remove them after the fact, i woudl just not add them in the first place. |
…etween ) and throw
I finally got this approach working. 0f157af shows that the close brace and the throw keyword need both trivia handling to avoid the line break/indentation. I think the way forward here is to move away from |
src/Workspaces/Core/Portable/Shared/Extensions/SyntaxGeneratorExtensions.cs
Outdated
Show resolved
Hide resolved
This seems reasonable. |
src/EditorFeatures/CSharpTest/InitializeParameter/AddParameterCheckTests.cs
Outdated
Show resolved
Hide resolved
src/EditorFeatures/CSharpTest/InitializeParameter/AddParameterCheckTests.cs
Outdated
Show resolved
Hide resolved
src/Workspaces/Core/Portable/Shared/Extensions/SyntaxGeneratorExtensions.cs
Outdated
Show resolved
Hide resolved
src/Workspaces/Core/Portable/Shared/Extensions/SyntaxGeneratorExtensions.cs
Outdated
Show resolved
Hide resolved
src/Workspaces/Core/Portable/Shared/Extensions/SyntaxGeneratorExtensions.cs
Outdated
Show resolved
Hide resolved
|
@CyrusNajmabadi and @sharwell Ready for review. |
sharwell
left a comment
There was a problem hiding this comment.
The implementation seems fine. I would like to see new tests added for the "Fix All" case before merging.
src/EditorFeatures/CSharpTest/InitializeParameter/AddParameterCheckTests.cs
Outdated
Show resolved
Hide resolved
| { | ||
| { CSharpCodeStyleOptions.PreferBraces, new CodeStyleOption2<PreferBracesPreference>((PreferBracesPreference)preferBraces, NotificationOption2.Silent) } | ||
| { CSharpCodeStyleOptions.PreferBraces, new CodeStyleOption2<PreferBracesPreference>((PreferBracesPreference)preferBraces, NotificationOption2.Silent) }, | ||
| { CSharpCodeStyleOptions.AllowEmbeddedStatementsOnSameLine, false }, |
There was a problem hiding this comment.
💭 Considering this test was previously showing the behavior for a defaulted value of AllowEmbeddedStatementsOnSameLine, it might be preferable to change FixedCode instead of changing this line.
It makes me wonder if we should have used FalseWithSilentEnforcement as the default for AllowEmbeddedStatementsOnSameLine, but if that changes it can be a separate change.
|
|
||
| [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInitializeParameter)] | ||
| [WorkItem(52385, "https://github.com/dotnet/roslyn/issues/52385")] | ||
| public async Task SingleLineStatement_NullCheck_BracesNone_SameLineFalse() |
There was a problem hiding this comment.
💡 Can you also add some tests for using code action equivalence key Add_null_checks_for_all_parameters (e.g. based on TestMultiNullableObjects)
| End Function | ||
|
|
||
| Protected Overrides Function CreateParameterCheckIfStatement(options As DocumentOptionSet, condition As ExpressionSyntax, ifTrueStatement As StatementSyntax) As StatementSyntax | ||
| Return SyntaxFactory.MultiLineIfBlock( |
There was a problem hiding this comment.
💭 Technically we could do the same thing here, but I'm not sure it's common enough to need to. I'd be fine leaving it as a follow-up, especially considering AllowEmbeddedStatementsOnSameLine would need to be moved to the shared options first.
There was a problem hiding this comment.
agreed.
fwiw, we don't check AllowEmbeddedStatementsOnSameLine in vb at all. it was really intended for the C# case (and may just be a c# option currently). we have very little use of this pattern in vb.
…CheckTests.cs Co-authored-by: Sam Harwell <sam@tunnelvisionlabs.com>
| SyntaxFactory.IfStatement(SyntaxFactory.Token(SyntaxKind.IfKeyword), condition, SyntaxFactory.Token(SyntaxKind.ThenKeyword)), | ||
| New SyntaxList(Of StatementSyntax)(ifTrueStatement), | ||
| Nothing, | ||
| Nothing) |
There was a problem hiding this comment.
nit: named parameters plz.
CyrusNajmabadi
left a comment
There was a problem hiding this comment.
Thanks! @jnm2 will appreciate this a lot :)
Fixes #52385
csharp_prefer_bracescsharp_style_allow_embedded_statements_on_same_line_experimental