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

S1186: also inspect empty set and init and empty local functions #8584

Merged
merged 4 commits into from
Jan 26, 2024

Conversation

antonioaversa
Copy link
Contributor

@antonioaversa antonioaversa commented Jan 23, 2024

Fixes #3753

@antonioaversa antonioaversa force-pushed the antonio/S1186-improvement-empty-set-and-init branch 5 times, most recently from f508ae5 to 64b148a Compare January 23, 2024 17:12
@antonioaversa antonioaversa changed the title S1186: also inspect empty set and init and empty local functions S1186: also inspect empty set and init Jan 23, 2024
@antonioaversa antonioaversa self-assigned this Jan 23, 2024
@antonioaversa antonioaversa force-pushed the antonio/S1186-improvement-empty-set-and-init branch 3 times, most recently from 6dbbfe2 to 63d629d Compare January 24, 2024 08:42
class Inherited : Base
{
protected override int VirtualEmptyInitProp {
init
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note for the reviewer: in the presence of accessors with empty bodies, I have decided to go only one level up w.r.t. the empty body, when formatting the code fix, instead of formatting at the property level.
That is because the user may also have other accessors defined in the property, that are not involved in the code fix.

In that scenario, a code fix on the body of an accessor would end up formatting a potentially large portion of user code, that may not be what the user wants.

int AProperty
{
    get { /* ... */ } // Get accessor with code, that the user may not want to be reformatted
    init { } 
}

Let me know if you think that it would be better, in your opinion, to take the risk of reformatting unrelated user code, and have better formatting overall, and I will make the change.

@antonioaversa
Copy link
Contributor Author

antonioaversa commented Jan 24, 2024

Uncovered lines in ReturnEmptyCollectionInsteadOfNull.cs and SyntaxNodeExtensions.cs are not new, only refactored.
Uncovered lines in EmptyMethodCodeFix.cs are also not new, and related to the await instruction. It's unclear to me what is not covered on those lines: I have tried to see how lowered code looks like here and understand if the await can generate uncovered branches somehow, but I can't reproduce such a scenario: the generated stateMachine is always branch-less.

@antonioaversa antonioaversa marked this pull request as ready for review January 24, 2024 12:02
@antonioaversa antonioaversa added Sprint: Hardening Fix FPs/FNs/improvements and removed Sprint: Hardening Fix FPs/FNs/improvements labels Jan 24, 2024
@antonioaversa antonioaversa changed the title S1186: also inspect empty set and init S1186: also inspect empty set and init and empty local functions Jan 24, 2024
Copy link
Contributor

@sebastien-marichal sebastien-marichal left a comment

Choose a reason for hiding this comment

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

LGTM!

@@ -68,41 +68,32 @@ void ReportIfAny(List<Location> nullOrDefaultLiterals)
}
}

private static BlockSyntax GetBody(SyntaxNode node) =>

Choose a reason for hiding this comment

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

Can't we remove this method and use the SyntaxExtensions?

Copy link
Member

Choose a reason for hiding this comment

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

Discussed offline, this always returns the body of the get accessor. We would have to return an array instead of a single BlockSyntax.

{
var symbol = context.SemanticModel.GetDeclaredSymbol(context.Node);
return symbol is IPropertySymbol property ? property.Type : ((IMethodSymbol)symbol).ReturnType;
}

private static ArrowExpressionClauseSyntax GetExpressionBody(SyntaxNode node) =>

Choose a reason for hiding this comment

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

Wouldn't be better to move this to SyntaxNodeExtensions?

Choose a reason for hiding this comment

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

Similar with above.

@antonioaversa antonioaversa force-pushed the antonio/S1186-improvement-empty-set-and-init branch from a0eb27b to 1ae9e62 Compare January 25, 2024 16:02
Copy link

Quality Gate Passed Quality Gate passed

Kudos, no new issues were introduced!

0 New issues
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

Copy link

};

private static AccessorDeclarationSyntax GetAccessor(PropertyDeclarationSyntax property) =>
property.AccessorList?.Accessors.FirstOrDefault(a => a.IsKind(SyntaxKind.GetAccessorDeclaration));
property.AccessorList.Accessors.FirstOrDefault(x => x.IsKind(SyntaxKind.GetAccessorDeclaration));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

While the grammar defines the accessor_list production rule as optional, and an alternative to ((arrow_expression_clause | equals_value_clause) ';')), the AccessorList results non-null even on arrow expressions.
Since I can't produce any example of a property where AccessorList ends up being null (not even with an incomplete code example), I am removing the null propagation operator.

@antonioaversa
Copy link
Contributor Author

antonioaversa commented Jan 25, 2024

After the changes to the testing framework, coverage figures are not comparable with the ones we used to have.
Lack of coverage for GetAccessor has disappeared. However, other issues have appeared, on code that seems to be considered "new" now, but was not new before (as, for example, the entire SyntaxNodeExtensions file, for which now "Coverage on New Code" is reported to be at 76.5%).

Copy link
Member

@costin-zaharia-sonarsource costin-zaharia-sonarsource left a comment

Choose a reason for hiding this comment

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

LGTM! I've only reviewed the coverage-related changes.

@antonioaversa antonioaversa merged commit 3ca63c7 into master Jan 26, 2024
29 checks passed
@antonioaversa antonioaversa deleted the antonio/S1186-improvement-empty-set-and-init branch January 26, 2024 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

S1186: also inspect empty set and init and empty local functions
3 participants