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

Fix S2190 FN: Support indexer, conversion operator and event declaration #8464

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

sagi1623
Copy link
Contributor

Fixes #6642
Fixes #6643
Fixes #6644

@pavel-mikula-sonarsource pavel-mikula-sonarsource changed the title FN S2190: No issues raised if recursion is inside indexer, conversion operator or event declaration Fix S2190 FN: Support indexer, conversion operator and event declaration Dec 14, 2023
@cristian-ambrosini-sonarsource
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@cristian-ambrosini-sonarsource
Copy link
Contributor

Can you please increase the coverage and fix the 3 issues?

@sagi1623
Copy link
Contributor Author

I fixed the code smells and opened an issue for the rest of the FPs that remain in the file. Regarding the coverage I could not come up with any valid or invalid code snippet that would cover the cases, however I would rather leave it there as defensive coding than to create AD0001s if anything changes in Roslyn.

@cristian-ambrosini-sonarsource
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copy link

sonarcloud bot commented Jan 15, 2024

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

sonarcloud bot commented Jan 15, 2024

Quality Gate Failed Quality Gate failed

Failed conditions

85.5% Coverage on New Code (required ≥ 95%)

See analysis details on SonarCloud

Copy link
Contributor

Choose a reason for hiding this comment

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

Left few ideas to bump the coverage

IIncrementOrDecrementOperationWrapper.FromOperation(operation).OperatorMethod,
OperationKindEx.Unary =>
IUnaryOperationWrapper.FromOperation(operation).OperatorMethod,
OperationKindEx.Conversion=>

Choose a reason for hiding this comment

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

Suggested change
OperationKindEx.Conversion=>
OperationKindEx.Conversion =>

public void CheckForNoExitIndexer(SonarSyntaxNodeReportingContext c, IndexerDeclarationSyntax indexer, IPropertySymbol propertySymbol)
{
// SonarCFG is out of support
}

Choose a reason for hiding this comment

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

I think these two methods can/should be tested by adding two compliant event/indexers UTs in InfiniteRecursion.SonarCfg test file

OperationKindEx.Binary =>
IBinaryOperationWrapper.FromOperation(operation).OperatorMethod,
OperationKindEx.Decrement =>
IIncrementOrDecrementOperationWrapper.FromOperation(operation).OperatorMethod,

Choose a reason for hiding this comment

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

It's weird because this line is tested in InfiniteRecursion.CSharp11.cs through:

public class Decrement : IDecrementOperators<Decrement>
{
    public static Decrement operator --(Decrement val) => --val; // Noncompliant
}

same for Unary:

public class BitWise : IBitwiseOperators<BitWise, BitWise, BitWise>
{
    public static BitWise operator ~(BitWise value) => ~value; // Noncompliant
    public static BitWise operator &(BitWise left, BitWise right) => left & right; // Noncompliant
    public static BitWise operator |(BitWise left, BitWise right) => left | right; // Noncompliant
    public static BitWise operator ^(BitWise left, BitWise right) => left ^ right; // Noncompliant
}

but the code coverage tool is not detecting those lines as covered.

c =>
{
var indexer = (IndexerDeclarationSyntax)c.Node;
if (c.SemanticModel.GetDeclaredSymbol(indexer) is { } indexerSymbol)

Choose a reason for hiding this comment

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

We are registering for a declaration syntax node, looking at the documentation, I don't see how c.SemanticModel.GetDeclaredSymbol(indexer) might return null. I think we can avoid this check entirely and simply run:
checker.CheckForNoExitIndexer(c, indexer, c.SemanticModel.GetDeclaredSymbol(indexer));

c =>
{
var eventDeclaration = (EventDeclarationSyntax)c.Node;
if (c.SemanticModel.GetDeclaredSymbol(eventDeclaration) is { } eventSymbol)

Choose a reason for hiding this comment

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

Same as above

Comment on lines +33 to +35
property.ExpressionBody,
property.AccessorList,
property.Identifier.GetLocation(),

Choose a reason for hiding this comment

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

What about reducing the number of parameters of CheckForNoExit by passing directly the MemberDeclarationSyntax, and then retrieving all those properties within the method?

private static void CheckForNoExit(SonarSyntaxNodeReportingContext c, MemberDeclarationSyntax declarationSyntax, string arrowExpressionMessageArg, string accessorMessageArg)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants