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 S4070 FP: Raised on flagged enum #7991

Closed
muffeldsound opened this issue Sep 12, 2023 · 2 comments · Fixed by #8211
Closed

Fix S4070 FP: Raised on flagged enum #7991

muffeldsound opened this issue Sep 12, 2023 · 2 comments · Fixed by #8211
Assignees
Labels
Area: C# C# rules related issues. Sprint: Hardening Fix FPs/FNs/improvements Type: False Positive Rule IS triggered when it shouldn't be.
Milestone

Comments

@muffeldsound
Copy link

Description

Get the S4070 on flaged enum

Repro steps

[Flags]
public enum Failing
{
	None = 0,
            MyTest = 1 << 31
    }

Expected behavior

No S4070

Actual behavior

Get the S4070 on flaged enum

Related information

  • Visual Studio version
@pavel-mikula-sonarsource pavel-mikula-sonarsource changed the title Fix Sxxxx FP/FN: Issue title Fix S4070 FP: Raised on flagged enum Sep 12, 2023
@cristian-ambrosini-sonarsource
Copy link
Contributor

Hi @muffeldsound!
Thanks for reporting this.
After reviewing the issue, I can confirm that it is a false positive. I will provide a reproducer shortly to demonstrate the problem.
The root cause of this issue lies in the way we retrieve the value from the IFieldSymbol in the GetEnumValueOrDefault method (see here). The method fails with bitwise operations since they are not constant values.

@costin-zaharia-sonarsource
Copy link
Member

costin-zaharia-sonarsource commented Oct 17, 2023

The issue is raised since we consider negative values as noncompliant. We even have a test case for this. See:

[Flags]
public enum Color5 : int // Noncompliant
{
Value = -3, // Secondary
Other = -4 // Secondary
}

In this example 1 << 31 is outside the Int32 range (int is signed), the default base type for enums. Due to this, it overflows.

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. Sprint: Hardening Fix FPs/FNs/improvements Type: False Positive Rule IS triggered when it shouldn't be.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants