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: certains configuration of conditions lead to wrong detection of dead code #2442

Closed
sebastien-lorrain opened this issue Jun 4, 2019 · 1 comment · Fixed by #2533
Assignees
Labels
Area: C# C# rules related issues. Area: CFG/SE CFG and SE related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Milestone

Comments

@sebastien-lorrain
Copy link

sebastien-lorrain commented Jun 4, 2019

Repro steps

/// <summary>
/// A certain combination of condition wrongly considers the else code as dead.
/// </summary>
private static void FalsePositive()
{
	FalsePositive2_Sub(true, false, false);
	FalsePositive2_Sub(true, false, true);

	FalsePositive2_Sub(true, true, true);
	FalsePositive2_Sub(true, true, false);

	FalsePositive2_Sub(false, false, false);
	FalsePositive2_Sub(false, false, true);

	FalsePositive2_Sub(false, true, true);
	FalsePositive2_Sub(false, true, false);

	// Outcome.
	Console.WriteLine((_test1 && _test2 && _test3 && _test4) ? "Went through each test condition" : "Missed at least one test condition");        //NOSONAR.
}

private static bool _test1 = false;
private static bool _test2 = false;
private static bool _test3 = false;
private static bool _test4 = false;

private static void FalsePositive_Sub(bool testCondition1, bool testCondition2, bool testCondition3)
{
	bool condition1 = testCondition1;
	bool condition2 = testCondition2;
	bool condition3 = condition2 && testCondition3;

	if (condition2 && condition3 && condition1)
	{
		_test1 = true;
	}

	else if (!condition2 && !condition1)
	{
		_test2 = true;
	}

	else if (condition2 && condition1 && !condition3)
	{
		_test3 = true;
	}

	else
	{
		_test4 = true;
	}
}

Expected behavior

Rule S2583 should not be triggered

Actual Behavior

The else code should not be considered as dead code, as at least one combination of conditions triggers it.

Related information

Sonar C# version 7.11

@andrei-epure-sonarsource andrei-epure-sonarsource added this to the Support milestone Jun 4, 2019
@andrei-epure-sonarsource
Copy link
Contributor

thanks for the detailed repro, @SebastienLorrain !

@andrei-epure-sonarsource andrei-epure-sonarsource added Area: C# C# rules related issues. Area: CFG/SE CFG and SE related issues. Area: Symbolic Execution Type: False Positive Rule IS triggered when it shouldn't be. labels Jul 22, 2019
@andrei-epure-sonarsource andrei-epure-sonarsource modified the milestones: Support, CFG + Symbolic Execution Jul 22, 2019
@andrei-epure-sonarsource andrei-epure-sonarsource changed the title False positive: certains configuration of conditions lead to wrong detection of dead code S2583 FP: certains configuration of conditions lead to wrong detection of dead code Jul 22, 2019
andrei-epure-sonarsource added a commit that referenced this issue Jul 22, 2019
andrei-epure-sonarsource added a commit that referenced this issue Jul 22, 2019
@AbbasNS AbbasNS modified the milestones: CFG + Symbolic Execution, 7.16 Jul 30, 2019
@AbbasNS AbbasNS self-assigned this Jul 30, 2019
@AbbasNS AbbasNS mentioned this issue Aug 2, 2019
@andrei-epure-sonarsource andrei-epure-sonarsource changed the title S2583 FP: certains configuration of conditions lead to wrong detection of dead code Fix S2583 FP: certains configuration of conditions lead to wrong detection of dead code Aug 12, 2019
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: CFG/SE CFG and SE related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants