Skip to content

Commit

Permalink
Don't warn on unused record primary constructor parameters (#47830)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwengier authored Sep 19, 2020
1 parent 0457da1 commit 6e4a781
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public CSharpRemoveUnusedParametersAndValuesDiagnosticAnalyzer()
{
}

protected override bool IsRecordDeclaration(SyntaxNode node)
=> node is RecordDeclarationSyntax;

protected override bool SupportsDiscard(SyntaxTree tree)
=> ((CSharpParseOptions)tree.Options).LanguageVersion >= LanguageVersion.CSharp7;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1530,5 +1530,49 @@ private void Goo(int [|i|])
}",
Diagnostic(IDEDiagnosticIds.UnusedParameterDiagnosticId));
}

[WorkItem(47142, "https://github.com/dotnet/roslyn/issues/47142")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedParameters)]
public async Task Record_PrimaryConstructorParameter()
{
await TestMissingAsync(
@"record A(int [|X|]);"
);
}

[WorkItem(47142, "https://github.com/dotnet/roslyn/issues/47142")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedParameters)]
public async Task Record_NonPrimaryConstructorParameter()
{
await TestDiagnosticsAsync(
@"record A
{
public A(int [|X|])
{
}
}
",
Diagnostic(IDEDiagnosticIds.UnusedParameterDiagnosticId));
}

[WorkItem(47142, "https://github.com/dotnet/roslyn/issues/47142")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedParameters)]
public async Task Record_DelegatingPrimaryConstructorParameter()
{
await TestDiagnosticMissingAsync(
@"record A(int X);
record B(int X, int [|Y|]) : A(X);
");
}

[WorkItem(47174, "https://github.com/dotnet/roslyn/issues/47174")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedParameters)]
public async Task RecordPrimaryConstructorParameter_PublicRecord()
{
await TestDiagnosticMissingAsync(
@"public record Base(int I) { }
public record Derived(string [|S|]) : Base(42) { }
");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,14 @@ private bool IsUnusedParameterCandidate(IParameterSymbol parameter)
return false;
}

// Ignore parameters of record primary constructors since they map to public properties
// TODO: Remove this when implicit operations are synthesised: https://github.com/dotnet/roslyn/issues/47829
if (method.IsConstructor() &&
_compilationAnalyzer.IsRecordDeclaration(method.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax()))
{
return false;
}

// Ignore event handler methods "Handler(object, MyEventArgs)"
// as event handlers are required to match this signature
// regardless of whether or not the parameters are used.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ protected AbstractRemoveUnusedParametersAndValuesDiagnosticAnalyzer(
UnusedValueAssignmentOption = unusedValueAssignmentOption;
}

protected abstract bool IsRecordDeclaration(SyntaxNode node);
protected abstract Location GetDefinitionLocationToFade(IOperation unusedDefinition);
protected abstract bool SupportsDiscard(SyntaxTree tree);
protected abstract bool MethodHasHandlesClause(IMethodSymbol method);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.RemoveUnusedParametersAndValues
LanguageNames.VisualBasic)
End Sub

Protected Overrides Function IsRecordDeclaration(node As SyntaxNode) As Boolean
Return False
End Function

Protected Overrides Function SupportsDiscard(tree As SyntaxTree) As Boolean
Return False
End Function
Expand Down

0 comments on commit 6e4a781

Please sign in to comment.