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

CA1508: false positive on sub-sequential more strict condition #6868

Open
MaceWindu opened this issue Aug 19, 2023 · 1 comment · May be fixed by #6876
Open

CA1508: false positive on sub-sequential more strict condition #6868

MaceWindu opened this issue Aug 19, 2023 · 1 comment · May be fixed by #6876
Assignees
Labels
Bug The product is not behaving according to its current intended design DataFlow help wanted The issue is up-for-grabs, and can be claimed by commenting
Milestone

Comments

@MaceWindu
Copy link

Analyzer

Diagnostic ID: CA1508: Avoid dead conditional code

Analyzer source

NuGet Package: Microsoft.CodeAnalysis.NetAnalyzers

Version: 8.0.0-preview.23364.2 (Latest)

Describe the bug

When variable checked to have one of two values and next checked to have one of those values specifically, CA1508 generated.
For specific code it is possible to workaround (with less code as a bonus) by inverting condition to check value doesn't have remaining possible values.

Steps To Reproduce

public class Test
{
	public static void Method(string parameter)
	{
		var flag = GetFlag();

		// triggers CA1508 below
		if (flag == null || flag == false)
		// workaround: works
		//if (flag != true)
		{
			// CA1508  'flag == false' is always 'false'.Remove or refactor the condition(s) to avoid dead code.
			if (flag == false)
			{
			}
		}
	}

	public static bool? GetFlag() => false;
}

Expected behavior

No errors reported

Actual behavior

Error reported

Additional context

@Youssef1313
Copy link
Member

@mavasani I have been investigating this for a while today.

We incorrectly hit this condition:

image

So we are not merging the results of flag being null and false.

This is because the equality implementation relies on hash codes. The hash code of false is zero, and we also consider null to be zero.

@Youssef1313 Youssef1313 self-assigned this Aug 20, 2023
@Youssef1313 Youssef1313 added Bug The product is not behaving according to its current intended design DataFlow labels Aug 20, 2023
@mavasani mavasani added this to the Unknown milestone Oct 11, 2023
@mavasani mavasani added the help wanted The issue is up-for-grabs, and can be claimed by commenting label Oct 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug The product is not behaving according to its current intended design DataFlow help wanted The issue is up-for-grabs, and can be claimed by commenting
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants