Skip to content

Commit

Permalink
Declare explicit enum values and use '<<' operator (RCS1161)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt committed Feb 2, 2020
1 parent de0f120 commit 6b78496
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,25 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
return;
}

bool isFlags = enumSymbol.HasAttribute(MetadataNames.System_FlagsAttribute);

CodeAction codeAction = CodeAction.Create(
"Declare explicit values",
ct => DeclareExplicitValueAsync(document, enumDeclaration, enumSymbol, values, semanticModel, ct),
ct => DeclareExplicitValueAsync(document, enumDeclaration, enumSymbol, isFlags, useBitShift: false, values, semanticModel, ct),
GetEquivalenceKey(diagnostic));

context.RegisterCodeFix(codeAction, diagnostic);

if (isFlags)
{
CodeAction codeAction2 = CodeAction.Create(
"Declare explicit values (and use '<<' operator)",
ct => DeclareExplicitValueAsync(document, enumDeclaration, enumSymbol, isFlags, useBitShift: true, values, semanticModel, ct),
GetEquivalenceKey(diagnostic, "BitShift"));

context.RegisterCodeFix(codeAction2, diagnostic);
}

break;
}
case DiagnosticIdentifiers.UseBitShiftOperator:
Expand Down Expand Up @@ -195,12 +208,12 @@ private static async Task<Document> DeclareExplicitValueAsync(
Document document,
EnumDeclarationSyntax enumDeclaration,
INamedTypeSymbol enumSymbol,
bool isFlags,
bool useBitShift,
ImmutableArray<ulong> values,
SemanticModel semanticModel,
CancellationToken cancellationToken)
{
bool isFlags = enumSymbol.HasAttribute(MetadataNames.System_FlagsAttribute);

List<ulong> reservedValues = values.ToList();

SeparatedSyntaxList<EnumMemberDeclarationSyntax> members = enumDeclaration.Members;
Expand Down Expand Up @@ -234,7 +247,21 @@ private static async Task<Document> DeclareExplicitValueAsync(
{
reservedValues.Add(value.Value);

EqualsValueClauseSyntax equalsValue = EqualsValueClause(NumericLiteralExpression(value.Value, enumSymbol.EnumUnderlyingType.SpecialType));
ExpressionSyntax expression;

if (useBitShift
&& value.Value > 1)
{
var power = (int)Math.Log(Convert.ToDouble(value.Value), 2);

expression = LeftShiftExpression(NumericLiteralExpression(1), NumericLiteralExpression(power));
}
else
{
expression = NumericLiteralExpression(value.Value, enumSymbol.EnumUnderlyingType.SpecialType);
}

EqualsValueClauseSyntax equalsValue = EqualsValueClause(expression);

EnumMemberDeclarationSyntax newMember = members[i].WithEqualsValue(equalsValue);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,34 @@ enum Foo
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.EnumShouldDeclareExplicitValues)]
public async Task Test_Flags_BitShift()
{
await VerifyDiagnosticAndFixAsync(@"
using System;
[Flags]
enum [|Foo|]
{
A,
B,
C,
D,
}
", @"
using System;
[Flags]
enum Foo
{
A = 0,
B = 1,
C = 1 << 1,
D = 1 << 2,
}
", equivalenceKey: EquivalenceKey.Create(Descriptor.Id, "BitShift"));
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.EnumShouldDeclareExplicitValues)]
public async Task Test_Flags2()
{
Expand Down

0 comments on commit 6b78496

Please sign in to comment.