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 S2583 FP/FN: Conditionally executed code should be reachable #9580

Open
gabenovotny opened this issue Jul 30, 2024 · 1 comment
Open
Labels
Area: C# C# rules related issues. Area: VB.NET VB.NET rules related issues. Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules.

Comments

@gabenovotny
Copy link

Description

Block of code with an if statement at the end that allows control to flow when a collection count is equal to zero is being flagged as "unreachable code" for rule S2583

image

Repro steps

// This is of type "MergePublicationCollection" from the Microsoft.SqlServer.Rmo nuget package
var mergePublications = replicationServer.ReplicationDatabases[databaseName].MergePublications;
// This is of type "MergePublication" from the Microsoft.SqlServer.Rmo nuget package
var mergePublication = mergePublications[publication];

mergePublication?.Remove(true);

mergePublications.Refresh();

if (mergePublications.Count == 0)
{
     DisablePublishing(replicationServer, databaseName);
}

Expected behavior

The above collection could be empty, so it is likely that the block within the above if statement would be executed.

Actual behavior

S2583 rule is flagged as a warning in the pipeline running the analysis.

Known workarounds

We could suppress the warning in code or in our project configuration, but we want the warning for other instances of this issue. This appears to be a false-positive.

Related information

  • C#/VB.NET Plugins version - 7.3.0.77872
  • Visual Studio version - 17.4.1
  • MSBuild / dotnet version - using Azure Pipeline task - VSBuild@1
  • Windows Server 2019
@zsolt-kolbay-sonarsource
Copy link
Contributor

zsolt-kolbay-sonarsource commented Jul 31, 2024

Hi @gabenovotny. Thank you for reporting this issue. Confirmed as FP.
There are 2 reasons why this is happening:

  1. Our rule engine assumes that the indexer will throw an exception if the collection does not contain the key. Therefore it thinks by the time it reaches the mergePublications.Count == 0 condition that the collection has at least one element. This is not the case with the class behind mergePublications, it simply returns null when an element is not found. I created a reproducer for this. We'll check out how much effort it will take to fix this.
  2. We do not track elements deleting themselves from a collection (I assume mergePublication?.Remove(true); is doing exactly that before the call to Refresh()). A general solution for that would be very difficult to implement, so it's unlikely we will add a fix for this part of the problem. Maybe we can create a list of collection types that should be ignored and add ReplicationBaseCollection to it?

@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource added Area: C# C# rules related issues. Area: VB.NET VB.NET rules related issues. Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules. labels Jul 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Area: VB.NET VB.NET rules related issues. Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules.
Projects
None yet
Development

No branches or pull requests

2 participants