Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bail out for primary constructor parameters in IDE0059 analyzer #69748

Merged
merged 1 commit into from
Aug 30, 2023
Merged
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 @@ -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