提交 7310fba5 编写于 作者: A AlekseyTs 提交者: GitHub

Merge pull request #12227 from AlekseyTs/OutVar_15

Adjust more places outside of compiler to account for the fact that ArgumentSyntax.Expression can be null.
......@@ -518,6 +518,34 @@ public static void M()
{
string result = $""{5}"";
}
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertToInterpolatedString)]
public async Task TestOutVariableDeclaration_01()
{
await TestMissingAsync(
@"using System;
class T
{
void M()
{
var a = [|string.Format(""{0}"", out int x)|];
}
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertToInterpolatedString)]
public async Task TestOutVariableDeclaration_02()
{
await TestMissingAsync(
@"using System;
class T
{
void M()
{
var a = [|string.Format(out string x, 1)|];
}
}");
}
}
......
......@@ -468,5 +468,137 @@ public async Task TupleWithDifferentNames_GetAndSet()
parseOptions: TestOptions.Regular,
withScriptOption: true));
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
public async Task TestOutVarDeclaration_1()
{
await TestAsync(
@"class C
{
// Foo
int [||]GetFoo()
{
}
// SetFoo
void SetFoo(out int i)
{
}
void Test()
{
SetFoo(out int i);
}
}",
@"class C
{
// Foo
int Foo
{
get
{
}
}
// SetFoo
void SetFoo(out int i)
{
}
void Test()
{
SetFoo(out int i);
}
}",
index: 0,
compareTokens: false);
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
public async Task TestOutVarDeclaration_2()
{
await TestAsync(
@"class C
{
// Foo
int [||]GetFoo()
{
}
// SetFoo
void SetFoo(int i)
{
}
void Test()
{
SetFoo(out int i);
}
}",
@"class C
{
// Foo
// SetFoo
int Foo
{
get
{
}
set
{
}
}
void Test()
{
{|Conflict:Foo|}(out int i);
}
}",
index: 1,
compareTokens: false);
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
public async Task TestOutVarDeclaration_3()
{
await TestMissingAsync(
@"class C
{
// Foo
int GetFoo()
{
}
// SetFoo
void [||]SetFoo(out int i)
{
}
void Test()
{
SetFoo(out int i);
}
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
public async Task TestOutVarDeclaration_4()
{
await TestMissingAsync(
@"class C
{
// Foo
int [||]GetFoo(out int i)
{
}
// SetFoo
void SetFoo(out int i, int j)
{
}
void Test()
{
var y = GetFoo(out int i);
}
}");
}
}
}
\ No newline at end of file
......@@ -213,7 +213,8 @@ public override SyntaxNode VisitIdentifierName(IdentifierNameSyntax node)
private static Action<SyntaxEditor, InvocationExpressionSyntax, SimpleNameSyntax, SimpleNameSyntax> s_replaceSetReferenceInvocation =
(editor, invocation, nameNode, newName) =>
{
if (invocation.ArgumentList?.Arguments.Count != 1)
if (invocation.ArgumentList?.Arguments.Count != 1 ||
invocation.ArgumentList.Arguments[0].Declaration != null)
{
var annotation = ConflictAnnotation.Create(FeaturesResources.OnlyMethodsWithASingleArgumentCanBeReplacedWithAProperty);
editor.ReplaceNode(nameNode, newName.WithIdentifier(newName.Identifier.WithAdditionalAnnotations(annotation)));
......
......@@ -1934,7 +1934,7 @@ internal class FeaturesResources {
}
/// <summary>
/// Looks up a localized string similar to Only methods with a single argument can be replaced with a property..
/// Looks up a localized string similar to Only methods with a single argument, which is not an out variable declaration, can be replaced with a property..
/// </summary>
internal static string OnlyMethodsWithASingleArgumentCanBeReplacedWithAProperty {
get {
......
......@@ -871,7 +871,7 @@ Do you want to continue?</value>
<value>Non-invoked method cannot be replaced with property.</value>
</data>
<data name="OnlyMethodsWithASingleArgumentCanBeReplacedWithAProperty" xml:space="preserve">
<value>Only methods with a single argument can be replaced with a property.</value>
<value>Only methods with a single argument, which is not an out variable declaration, can be replaced with a property.</value>
</data>
<data name="ErrorCategory" xml:space="preserve">
<value>Roslyn.HostError</value>
......
......@@ -129,7 +129,9 @@ private static bool IsValidGetMethod(IMethodSymbol getMethod)
private static bool IsValidSetMethod(IMethodSymbol setMethod, IMethodSymbol getMethod)
{
return IsValidSetMethod(setMethod) &&
setMethod.Parameters.Length == 1 && Equals(setMethod.Parameters[0].Type, getMethod.ReturnType) &&
setMethod.Parameters.Length == 1 &&
setMethod.Parameters[0].RefKind == RefKind.None &&
Equals(setMethod.Parameters[0].Type, getMethod.ReturnType) &&
setMethod.IsAbstract == getMethod.IsAbstract;
}
......
......@@ -853,7 +853,7 @@ private AttributeArgumentSyntax AsAttributeArgument(SyntaxNode node)
}
var arg = node as ArgumentSyntax;
if (arg != null)
if (arg != null && arg.Expression != null)
{
return SyntaxFactory.AttributeArgument(default(NameEqualsSyntax), arg.NameColon, arg.Expression);
}
......
......@@ -1510,20 +1510,24 @@ private IEnumerable<ITypeSymbol> InferTypeInNameColon(NameColonSyntax nameColon,
if (invocation.ArgumentList.Arguments.Count > 0)
{
var argumentExpression = invocation.ArgumentList.Arguments[0].Expression;
var argumentTypes = GetTypes(argumentExpression);
var delegateType = argumentTypes.FirstOrDefault().GetDelegateType(this.Compilation);
var typeArg = delegateType?.TypeArguments.Length > 0
? delegateType.TypeArguments[0]
: this.Compilation.ObjectType;
if (IsUnusableType(typeArg) && argumentExpression is LambdaExpressionSyntax)
if (argumentExpression != null)
{
typeArg = InferTypeForFirstParameterOfLambda((LambdaExpressionSyntax)argumentExpression) ??
this.Compilation.ObjectType;
var argumentTypes = GetTypes(argumentExpression);
var delegateType = argumentTypes.FirstOrDefault().GetDelegateType(this.Compilation);
var typeArg = delegateType?.TypeArguments.Length > 0
? delegateType.TypeArguments[0]
: this.Compilation.ObjectType;
if (IsUnusableType(typeArg) && argumentExpression is LambdaExpressionSyntax)
{
typeArg = InferTypeForFirstParameterOfLambda((LambdaExpressionSyntax)argumentExpression) ??
this.Compilation.ObjectType;
}
return SpecializedCollections.SingletonEnumerable(
ienumerableType.Construct(typeArg));
}
return SpecializedCollections.SingletonEnumerable(
ienumerableType.Construct(typeArg));
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册