提交 27ca9c15 编写于 作者: C CyrusNajmabadi

Give conflicts in ref/out situations.

上级 892c2027
......@@ -58,6 +58,38 @@ public async Task TestAnonyousType2()
@"class C {
public int GetProp() { return 0; }
public void M() { var v = new { Prop = this.GetProp() } }
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplacePropertyWithMethods)]
public async Task TestPassedToRef1()
{
await TestAsync(
@"class C {
public int [||]Prop { get { return 0; } }
public void RefM(ref int i) { }
public void M() { RefM(ref this.Prop); }
}",
@"class C {
public int GetProp() { return 0; }
public void RefM(ref int i) { }
public void M() { RefM(ref {|Conflict:this.GetProp()|}); }
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplacePropertyWithMethods)]
public async Task TestPassedToOut1()
{
await TestAsync(
@"class C {
public int [||]Prop { get { return 0; } }
public void OutM(out int i) { }
public void M() { OutM(out this.Prop); }
}",
@"class C {
public int GetProp() { return 0; }
public void OutM(out int i) { }
public void M() { OutM(out {|Conflict:this.GetProp()|}); }
}");
}
}
......
......@@ -646,6 +646,15 @@ internal class CSharpFeaturesResources {
}
}
/// <summary>
/// Looks up a localized string similar to Property cannot safely be replaced with a method call.
/// </summary>
internal static string Property_cannot_safely_be_replaced_with_a_method_call {
get {
return ResourceManager.GetString("Property_cannot_safely_be_replaced_with_a_method_call", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to property getter.
/// </summary>
......
......@@ -455,4 +455,7 @@
<data name="UseImplicitTypeDiagnosticTitle" xml:space="preserve">
<value>Use implicit type</value>
</data>
</root>
<data name="Property_cannot_safely_be_replaced_with_a_method_call" xml:space="preserve">
<value>Property cannot safely be replaced with a method call</value>
</data>
</root>
\ No newline at end of file
......@@ -4,6 +4,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;
......@@ -104,11 +105,16 @@ public void ReplaceReference(SyntaxEditor editor, SyntaxToken nameToken)
expression = expression.Parent as ExpressionSyntax;
}
if (identifierName.IsOnlyWrittenTo())
if (expression.IsInOutContext() || expression.IsInRefContext())
{
// Code wasn't legal (you can't reference a property in an out/ref position in C#).
// Just replace this with a simple GetCall, but mark it so it's clear there's an error.
ReplaceWithGetInvocation(editor, nameToken, CSharpFeaturesResources.Property_cannot_safely_be_replaced_with_a_method_call);
}
else if (expression.IsOnlyWrittenTo())
{
// We're only being written to here. This is safe to replace with a call to the
// setter.
}
else if (identifierName.IsWrittenTo())
{
......@@ -124,9 +130,7 @@ public void ReplaceReference(SyntaxEditor editor, SyntaxToken nameToken)
if (declarator.NameEquals != null)
{
editor.ReplaceNode(
expression,
GetGetInvocationExpression(nameToken));
ReplaceWithGetInvocation(editor, nameToken);
}
else
{
......@@ -139,10 +143,27 @@ public void ReplaceReference(SyntaxEditor editor, SyntaxToken nameToken)
else
{
// No writes. Replace this with a call to the getter.
editor.ReplaceNode(
expression,
GetGetInvocationExpression(nameToken));
ReplaceWithGetInvocation(editor, nameToken);
}
}
private static void ReplaceWithGetInvocation(
SyntaxEditor editor, SyntaxToken nameToken, string conflictMessage = null)
{
var identifierName = (IdentifierNameSyntax)nameToken.Parent;
var expression = (ExpressionSyntax)identifierName;
if (expression.IsRightSideOfDotOrArrow())
{
expression = expression.Parent as ExpressionSyntax;
}
var invocation = GetGetInvocationExpression(nameToken);
if (conflictMessage != null)
{
invocation = invocation.WithAdditionalAnnotations(ConflictAnnotation.Create(conflictMessage));
}
editor.ReplaceNode(expression, invocation);
}
private static ExpressionSyntax GetGetInvocationExpression(SyntaxToken nameToken)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册