Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -45,6 +45,22 @@ protected override StatementSyntax WrapWithBlockIfAppropriate(
return statement;
}

protected override SyntaxNode WrapIfStatementIfNecessary(IConditionalOperation operation)
{
if (operation.Syntax is IfStatementSyntax { Condition: CheckedExpressionSyntax exp })
return exp;

return base.WrapIfStatementIfNecessary(operation);
}

protected override ExpressionSyntax WrapReturnExpressionIfNecessary(ExpressionSyntax returnExpression, IOperation returnOperation)
{
if (returnOperation.Syntax is ReturnStatementSyntax { Expression: CheckedExpressionSyntax exp })
return exp;

return base.WrapReturnExpressionIfNecessary(returnExpression, returnOperation);
}

protected override ExpressionSyntax ConvertToExpression(IThrowOperation throwOperation)
=> CSharpUseConditionalExpressionHelpers.ConvertToExpression(throwOperation);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,146 @@ int M()
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/70748")]
public async Task TestMissingWithCheckedInIf()
{
await TestInRegularAndScriptAsync(
"""
class C
{
int M()
{
int x = 0;
int y = 0;
[||]if (checked(x == y))
{
return 0;
}
else
{
return 1;
}
}
}
""",
"""
class C
{
int M()
{
int x = 0;
int y = 0;
return checked(x == y) ? 0 : 1;
}
}
""", parseOptions: CSharp8);
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/70748")]
public async Task TestMissingWithUncheckedInIf()
{
await TestInRegularAndScriptAsync(
"""
class C
{
int M()
{
int x = 0;
int y = 0;
[||]if (unchecked(x == y))
{
return 0;
}
else
{
return 1;
}
}
}
""",
"""
class C
{
int M()
{
int x = 0;
int y = 0;
return unchecked(x == y) ? 0 : 1;
}
}
""", parseOptions: CSharp8);
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/70748")]
public async Task TestMissingWithCheckedInTrueStatement()
{
await TestInRegularAndScriptAsync(
"""
class C
{
int M()
{
int x = 0;
int y = 0;
[||]if (x == y)
{
return checked(x - y);
}
else
{
return 1;
}
}
}
""",
"""
class C
{
int M()
{
int x = 0;
int y = 0;
return x == y ? checked(x - y) : 1;
}
}
""", parseOptions: CSharp8);
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/70748")]
public async Task TestMissingWithUncheckedInTrueStatement()
{
await TestInRegularAndScriptAsync(
"""
class C
{
int M()
{
int x = 0;
int y = 0;
[||]if (x == y)
{
return unchecked(x - y);
}
else
{
return 1;
}
}
}
""",
"""
class C
{
int M()
{
int x = 0;
int y = 0;
return x == y ? unchecked(x - y) : 1;
}
}
""", parseOptions: CSharp8);
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/70750")]
public async Task TestMissingWithUnchecked()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,23 @@ protected async Task<TExpressionSyntax> CreateConditionalExpressionAsync(
var generatorInternal = document.GetRequiredLanguageService<SyntaxGeneratorInternal>();
var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);

var condition = ifOperation.Condition.Syntax;
var condition = WrapIfStatementIfNecessary(ifOperation);
if (CanSimplify(trueValue, falseValue, isRef, out var negate))
{
return negate
? (TExpressionSyntax)generator.Negate(generatorInternal, condition, semanticModel, cancellationToken).WithoutTrivia()
: (TExpressionSyntax)condition.WithoutTrivia();
}

var trueExpression = MakeRef(generatorInternal, isRef, CastValueIfNecessary(generator, trueStatement, trueValue));
var falseExpression = MakeRef(generatorInternal, isRef, CastValueIfNecessary(generator, falseStatement, falseValue));
trueExpression = WrapReturnExpressionIfNecessary(trueExpression, trueStatement);
falseExpression = WrapReturnExpressionIfNecessary(falseExpression, falseStatement);

var conditionalExpression = (TConditionalExpressionSyntax)generator.ConditionalExpression(
condition.WithoutTrivia(),
MakeRef(generatorInternal, isRef, CastValueIfNecessary(generator, trueStatement, trueValue)),
MakeRef(generatorInternal, isRef, CastValueIfNecessary(generator, falseStatement, falseValue)));
trueExpression,
falseExpression);

conditionalExpression = conditionalExpression.WithAdditionalAnnotations(Simplifier.Annotation);
var makeMultiLine = await MakeMultiLineAsync(
Expand All @@ -130,6 +135,12 @@ protected async Task<TExpressionSyntax> CreateConditionalExpressionAsync(
return MakeRef(generatorInternal, isRef, conditionalExpression);
}

protected virtual SyntaxNode WrapIfStatementIfNecessary(IConditionalOperation operation)
=> operation.Condition.Syntax;

protected virtual TExpressionSyntax WrapReturnExpressionIfNecessary(TExpressionSyntax returnExpression, IOperation returnOperation)
=> returnExpression;

private static TExpressionSyntax MakeRef(SyntaxGeneratorInternal generator, bool isRef, TExpressionSyntax syntaxNode)
=> isRef ? (TExpressionSyntax)generator.RefExpression(syntaxNode) : syntaxNode;

Expand Down