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

Rule S4070: Use xor operator to verify flags #814

Merged
merged 1 commit into from
Oct 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public sealed class DoNotMarkEnumsWithFlags : SonarDiagnosticAnalyzer

private static readonly DiagnosticDescriptor rule =
DiagnosticDescriptorBuilder.GetDescriptor(DiagnosticId, MessageFormat, RspecStrings.ResourceManager);

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(rule);

protected override void Initialize(SonarAnalysisContext context)
Expand Down Expand Up @@ -114,20 +115,19 @@ private static bool IsCombinationOfOtherValues(ulong value, List<ulong> otherVal
}

var newValue = value;
foreach (var otherValue in otherValues)
foreach (var otherValue in otherValues.SkipWhile(v => value <= v))
{
if (newValue <= 0)
{
return true;
}

if (otherValue < value)
if (otherValue <= newValue)
{
newValue -= otherValue;
newValue ^= otherValue;
if (newValue == 0)
{
return true;
}
}
}

return newValue == 0;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,24 @@ public enum InvalidStringEnum : string // Noncompliant
{
MyValue = "toto" // Secondary
}

[Flags]
enum EnumFoo3
{
N1 = 1,
N2 = 2,
N3 = N1 | N2,
N4 = 4,
N5 = N4 | N1,
N6 = N4 | N2
}

[Flags]
enum EnumFoo4 // Noncompliant
{
N1 = 1,
N2 = 2,
N3 = N1 | N2,
N5 = 5 // Secondary
}
}