Skip to content

Primary Constructors - Don't report unused parameters#67167

Merged
akhera99 merged 8 commits intodotnet:mainfrom
akhera99:primary_constructors_completion_tests
Mar 10, 2023
Merged

Primary Constructors - Don't report unused parameters#67167
akhera99 merged 8 commits intodotnet:mainfrom
akhera99:primary_constructors_completion_tests

Conversation

@akhera99
Copy link
Member

@akhera99 akhera99 commented Mar 3, 2023

Fixes: #67013

Falls under the same issue as #47830

@akhera99 akhera99 requested a review from mavasani March 3, 2023 17:24
@akhera99 akhera99 requested a review from a team as a code owner March 3, 2023 17:24
@ghost ghost added the Area-IDE label Mar 3, 2023
var methodSyntax = method.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax();
if (method.IsConstructor() &&
_compilationAnalyzer.IsRecordDeclaration(methodSyntax))
(_compilationAnalyzer.IsRecordDeclaration(methodSyntax) || _compilationAnalyzer.IsClassDeclaration(methodSyntax)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do we make sure this is a primary constructor and not a regular class constructor? Can you add tests to ensure that unused parameters are still flagged for a regular user written constructor in a regular class declaration?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see how this will work. Constructor symbol with its syntax node as a class declaration syntax guarantees it being a primary constructor. Would still be good to add a test for unused parameters of a regular constructor (if one doesn’t already exist)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't love that we're testing this way. I'd prefer it be done similar to the extension we have for GetAssociatedSynthesizedRecordProperty. Can you make a similar extension for this question (i.e. 'IsPrimaryConstructor').

End Function

Protected Overrides Function IsClassDeclaration(node As SyntaxNode) As Boolean
Return False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels weird, I believe we should check for node being a ClassDecarationSyntax here. Obviously no constructor in VB will have ClassDeclarationSyntax associated with it as VB doesn’t support primary constructors, so the modified if condition in base type will always be false for VB.

Copy link
Contributor

@mavasani mavasani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Minor suggestion for additional tests, and update to VB override.

var methodSyntax = method.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax();
if (method.IsConstructor() &&
_compilationAnalyzer.IsRecordDeclaration(methodSyntax))
(_compilationAnalyzer.IsRecordDeclaration(methodSyntax) || _compilationAnalyzer.IsClassDeclaration(methodSyntax)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this just be _compilationAnalyzer.IsTypeDeclaration(methodSyntax) and replace both methods with a single IsTypeDeclaration method?

@@ -231,7 +231,7 @@ parameter.ContainingSymbol is not IMethodSymbol method ||
// TODO: Remove this when implicit operations are synthesised: https://github.com/dotnet/roslyn/issues/47829
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider removing this comment. The issue was closed as by-design.

{
MethodKind: MethodKind.Constructor,
DeclaringSyntaxReferences.Length: > 0,
ContainingType: { IsType: true } containingType,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for the IsType: true check.

ContainingSymbol: IMethodSymbol
{
MethodKind: MethodKind.Constructor,
DeclaringSyntaxReferences.Length: > 0,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
DeclaringSyntaxReferences.Length: > 0,
DeclaringSyntaxReferences.Length: [var constructorReference, ..],

can then use that below.

{
if (parameter is
{
DeclaringSyntaxReferences.Length: > 0,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this check needed?

akhera99 and others added 2 commits March 8, 2023 11:10
…nsions/IParameterSymbolExtensions.cs

Co-authored-by: Youssef Victor <youssefvictor00@gmail.com>
@akhera99 akhera99 merged commit 4a2586c into dotnet:main Mar 10, 2023
@ghost ghost added this to the Next milestone Mar 10, 2023
@allisonchou allisonchou modified the milestones: Next, 17.6 P3 Mar 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unexpected "IDE0060 Remove unused parameter" suggestion for a Primary Constructor parameter

5 participants