Skip to content
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

Fix UseNullPropagationCodeFixProvider for parenthesized property access #74316

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1949,6 +1949,46 @@ void M(object o)
""");
}

[Fact, WorkItem("https:/dotnet/roslyn/issues/74273")]
public async Task TestParenthesizedPropertyAccess()
{
await TestInRegularAndScript1Async("""
using System;

class C
{
int? Length(Array array) => [|array == null ? null : (array.Length)|];
}
""", """
using System;

class C
{
int? Length(Array array) => (array?.Length);
}
""");
}

[Fact, WorkItem("https:/dotnet/roslyn/issues/74273")]
public async Task TestReversedParenthesizedPropertyAccess()
{
await TestInRegularAndScript1Async("""
using System;

class C
{
int? Length(Array array) => [|array != null ? (array.Length) : null|];
}
""", """
using System;

class C
{
int? Length(Array array) => (array?.Length);
}
""");
}

[Fact, WorkItem("https:/dotnet/roslyn/issues/49517")]
public async Task TestParenthesizedNull()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,8 @@ private void FixConditionalExpression(
var conditionalPart = root.FindNode(diagnostic.AdditionalLocations[1].SourceSpan, getInnermostNodeForTie: true);
var whenPart = root.FindNode(diagnostic.AdditionalLocations[2].SourceSpan, getInnermostNodeForTie: true);
syntaxFacts.GetPartsOfConditionalExpression(
conditionalExpression, out var condition, out var whenTrue, out var whenFalse);
conditionalExpression, out _, out var whenTrue, out _);
whenTrue = syntaxFacts.WalkDownParentheses(whenTrue);
whenFalse = syntaxFacts.WalkDownParentheses(whenFalse);

var whenPartIsNullable = diagnostic.Properties.ContainsKey(UseNullPropagationConstants.WhenPartIsNullable);
editor.ReplaceNode(
Expand All @@ -116,12 +115,14 @@ private void FixConditionalExpression(

var currentWhenPartToCheck = whenPart == whenTrue ? currentWhenTrue : currentWhenFalse;

var unwrappedCurrentWhenPartToCheck = syntaxFacts.WalkDownParentheses(currentWhenPartToCheck);

var match = AbstractUseNullPropagationDiagnosticAnalyzer<
TSyntaxKind, TExpressionSyntax, TStatementSyntax,
TConditionalExpressionSyntax, TBinaryExpressionSyntax, TInvocationExpressionSyntax,
TConditionalAccessExpressionSyntax, TElementAccessExpressionSyntax, TMemberAccessExpressionSyntax,
TIfStatementSyntax, TExpressionStatementSyntax>.GetWhenPartMatch(
syntaxFacts, semanticModel, (TExpressionSyntax)conditionalPart, (TExpressionSyntax)currentWhenPartToCheck, cancellationToken);
syntaxFacts, semanticModel, (TExpressionSyntax)conditionalPart, (TExpressionSyntax)unwrappedCurrentWhenPartToCheck, cancellationToken);
if (match == null)
{
return conditionalExpression;
Expand Down
Loading