Skip to content
Closed
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 @@ -6,6 +6,7 @@
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Diagnostics;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.MakeFieldReadonly
Expand Down Expand Up @@ -779,5 +780,51 @@ partial struct MyClass { }",

partial struct MyClass { }");
}

[WorkItem(26262, "https://github.com/dotnet/roslyn/issues/26262")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeFieldReadonly)]
public async Task FieldAssignedInCtor_InParens()
{
await TestInRegularAndScriptAsync(
@"class MyClass
{
private int [|_goo|];
MyClass()
{
(_goo) = 0;
}
}",
@"class MyClass
{
private readonly int _goo;
MyClass()
{
(_goo) = 0;
}
}");
}

[WorkItem(26262, "https://github.com/dotnet/roslyn/issues/26262")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeFieldReadonly)]
public async Task FieldAssignedInCtor_QualifiedWithThis_InParens()
{
await TestInRegularAndScriptAsync(
@"class MyClass
{
private int [|_goo|];
MyClass()
{
(this._goo) = 0;
}
}",
@"class MyClass
{
private readonly int _goo;
MyClass()
{
(this._goo) = 0;
}
}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,30 @@ End Class")
End Class")
End Function

<WorkItem(26262, "https://github.com/dotnet/roslyn/issues/26262")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeFieldReadonly)>
Public Async Function CopyPassedAsByRefParameter() As Task
Await TestInRegularAndScriptAsync(
"Class C
Private [|_goo|] As Integer = 0
Sub Goo()
' Note: the parens cause a copy, so this is not an actual write into _goo
Bar((_goo))
End Sub
Sub Bar(ByRef value As Integer)
End Sub
End Class",
"Class C
Private ReadOnly _goo As Integer = 0
Sub Goo()
' Note: the parens cause a copy, so this is not an actual write into _goo
Bar((_goo))
End Sub
Sub Bar(ByRef value As Integer)
End Sub
End Class")
End Function

<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeFieldReadonly)>
Public Async Function PassedAsByRefParameterInCtor() As Task
Await TestInRegularAndScriptAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,13 +325,22 @@ public static bool IsInRefContext(this ExpressionSyntax expression)
public static bool IsInInContext(this ExpressionSyntax expression)
=> (expression?.Parent as ArgumentSyntax)?.RefKindKeyword.Kind() == SyntaxKind.InKeyword;

public static bool IsOnlyWrittenTo(this ExpressionSyntax expression)
private static ExpressionSyntax GetExpressionToAnalyzeForWrites(ExpressionSyntax expression)
{
if (expression.IsRightSideOfDotOrArrow())
{
expression = expression.Parent as ExpressionSyntax;
}

expression = expression.WalkUpParentheses();

return expression;
}

public static bool IsOnlyWrittenTo(this ExpressionSyntax expression)
{
expression = GetExpressionToAnalyzeForWrites(expression);

if (expression != null)
{
if (expression.IsInOutContext())
Expand All @@ -356,24 +365,15 @@ public static bool IsOnlyWrittenTo(this ExpressionSyntax expression)
return false;
}

public static bool IsAttributeNamedArgumentIdentifier(this ExpressionSyntax expression)
{
var nameEquals = expression?.Parent as NameEqualsSyntax;
return nameEquals.IsParentKind(SyntaxKind.AttributeArgument);
}

public static bool IsWrittenTo(this ExpressionSyntax expression)
{
expression = GetExpressionToAnalyzeForWrites(expression);

if (expression.IsOnlyWrittenTo())
{
return true;
}

if (expression.IsRightSideOfDotOrArrow())
{
expression = expression.Parent as ExpressionSyntax;
}

if (expression.IsInRefContext())
{
return true;
Expand All @@ -393,6 +393,12 @@ public static bool IsWrittenTo(this ExpressionSyntax expression)
return false;
}

public static bool IsAttributeNamedArgumentIdentifier(this ExpressionSyntax expression)
{
var nameEquals = expression?.Parent as NameEqualsSyntax;
return nameEquals.IsParentKind(SyntaxKind.AttributeArgument);
}

public static bool IsOperandOfIncrementOrDecrementExpression(this ExpressionSyntax expression)
{
if (expression != null)
Expand Down