未验证 提交 284a67e9 编写于 作者: C CyrusNajmabadi 提交者: GitHub

Merge pull request #42961 from jnm2/simplify_interpolation_ref_structs

Do not offer to simplify ToString on ref structs
......@@ -894,6 +894,45 @@ struct TypeNotImplementingIFormattable
struct TypeNotImplementingIFormattable
{
public string ToString(string format) => ""A"";
}");
}
[Fact, WorkItem(42936, "https://github.com/dotnet/roslyn/issues/42936")]
public async Task ToStringSimplificationIsNotOfferedOnRefStruct()
{
await TestMissingInRegularAndScriptAsync(
@"class C
{
string M(RefStruct someValue) => $""Test: {someValue[||].ToString()}"";
}
ref struct RefStruct
{
public override string ToString() => ""A"";
}");
}
[Fact, WorkItem(42936, "https://github.com/dotnet/roslyn/issues/42936")]
public async Task PadLeftSimplificationIsStillOfferedOnRefStruct()
{
await TestInRegularAndScriptAsync(
@"class C
{
string M(RefStruct someValue) => $""Test: {someValue.ToString()[||].PadLeft(10)}"";
}
ref struct RefStruct
{
public override string ToString() => ""A"";
}",
@"class C
{
string M(RefStruct someValue) => $""Test: {someValue.ToString(),10}"";
}
ref struct RefStruct
{
public override string ToString() => ""A"";
}");
}
}
......
......@@ -73,7 +73,8 @@ private static IOperation Unwrap(IOperation expression)
{
if (expression is IInvocationOperation { TargetMethod: { Name: nameof(ToString) } } invocation &&
HasNonImplicitInstance(invocation) &&
!syntaxFacts.IsBaseExpression(invocation.Instance.Syntax))
!syntaxFacts.IsBaseExpression(invocation.Instance.Syntax) &&
!invocation.Instance.Type.IsRefLikeType)
{
if (invocation.Arguments.Length == 1 &&
invocation.Arguments[0].Value is ILiteralOperation { ConstantValue: { HasValue: true, Value: string value } } literal &&
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册