Skip to content

Commit

Permalink
Merge pull request #69748 from mavasani/Issue69643
Browse files Browse the repository at this point in the history
Bail out for primary constructor parameters in IDE0059 analyzer
  • Loading branch information
mavasani authored Aug 30, 2023
2 parents 94b37d1 + 32f597c commit e4a0d5b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9202,5 +9202,29 @@ void M(string[] ss)
ReferenceAssemblies = ReferenceAssemblies.Net.Net70,
}.RunAsync();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/69643")]
public async Task TestPrimaryConstructorParameterAssignment()
{
var source = """
class C(string str) {
public void Reset() {
str = string.Empty;
}
}
""";

await new VerifyCS.Test
{
TestCode = source,
FixedCode = source,
Options =
{
{ CSharpCodeStyleOptions.UnusedValueAssignment, UnusedValuePreference.DiscardVariable },
},
LanguageVersion = LanguageVersion.CSharp12,
ReferenceAssemblies = ReferenceAssemblies.Net.Net80,
}.RunAsync();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,15 @@ public override void VisitLocalReference(ILocalReferenceOperation operation)
}

public override void VisitParameterReference(IParameterReferenceOperation operation)
=> OnReferenceFound(operation.Parameter, operation);
{
if (operation.Parameter.IsPrimaryConstructor(_cancellationToken))
{
// Bail out for primary constructor parameters.
return;
}

OnReferenceFound(operation.Parameter, operation);
}

public override void VisitVariableDeclarator(IVariableDeclaratorOperation operation)
{
Expand Down

0 comments on commit e4a0d5b

Please sign in to comment.