Skip to content

Commit 5726d49

Browse files
committed
Fix RCS1258 for partially shared flags enums
1 parent a2c9d3b commit 5726d49

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/Analyzers/CSharp/Analysis/UnnecessaryEnumFlagAnalyzer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ private static void AnalyzeBitwiseOrExpression(SyntaxNodeAnalysisContext context
6262
for (int i = values.Count - 1; i >= 0; i--)
6363
{
6464
(ExpressionSyntax expression2, ulong value2) = values[i];
65+
ulong sharedBits = value & value2;
6566

66-
if ((value & value2) != 0)
67+
if (sharedBits == value2 || sharedBits == value)
6768
{
6869
if (value <= value2)
6970
{

src/Tests/Analyzers.Tests/RCS1258UnnecessaryEnumFlagTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,28 @@ void M()
7979
}
8080
");
8181
}
82+
83+
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UnnecessaryEnumFlag)]
84+
public async Task TestNoDiagnostic_PartiallySame()
85+
{
86+
await VerifyNoDiagnosticAsync(
87+
@"
88+
[System.Flags]
89+
enum FooBar
90+
{
91+
Shared = 1,
92+
Foo = Shared | 2,
93+
Bar = Shared | 4,
94+
}
95+
96+
class C
97+
{
98+
public static bool M(FooBar x)
99+
{
100+
return x == (FooBar.Foo | FooBar.Bar);
101+
}
102+
}
103+
"
104+
);
105+
}
82106
}

0 commit comments

Comments
 (0)