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 CA1027 fires for simple aliases #7120

Merged
merged 1 commit into from
Jan 2, 2024
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
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Collections.Immutable;
Expand Down Expand Up @@ -124,7 +124,7 @@ private static bool IsContiguous(IList<ulong> list)

bool first = true;
ulong previous = 0;
foreach (ulong element in list.OrderBy(t => t))
foreach (ulong element in list.Distinct().OrderBy(t => t))
{
if (first)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using System.Globalization;
using System.Threading.Tasks;
Expand Down Expand Up @@ -566,6 +566,47 @@ await VerifyVB.VerifyAnalyzerAsync(codeWithFlags,
GetCA2217BasicResultAt(22, 13, "LabelsClass", "2"));
}

[Fact, WorkItem(6982, "https://github.com/dotnet/roslyn-analyzers/issues/6982")]
public async Task CSharp_EnumWithFlagsAttributes_MembersShareValueAsync()
{
var code = @"{0}
public enum MembersShareValueEnumClass
{{
One,
Tow,
Three,
Two = Tow
}}
";
// Verify no CA1027: Mark enums with FlagsAttribute
string codeWithoutFlags = GetCSharpCode_EnumWithFlagsAttributes(code, hasFlags: false);
await VerifyCS.VerifyAnalyzerAsync(codeWithoutFlags);

// Verify no CA2217: Do not mark enums with FlagsAttribute
string codeWithFlags = GetCSharpCode_EnumWithFlagsAttributes(code, hasFlags: true);
await VerifyCS.VerifyAnalyzerAsync(codeWithFlags);
}

[Fact, WorkItem(6982, "https://github.com/dotnet/roslyn-analyzers/issues/6982")]
public async Task VisualBasic_EnumWithFlagsAttributes_MembersShareValueAsync()
{
string code = @"{0}
Public Enum MembersShareValueEnumClass
One
Tow
Three
Two = Tow
End Enum
";
// Verify no CA1027: Mark enums with FlagsAttribute
string codeWithoutFlags = GetBasicCode_EnumWithFlagsAttributes(code, hasFlags: false);
await VerifyVB.VerifyAnalyzerAsync(codeWithoutFlags);

// Verify no CA2217: Do not mark enums with FlagsAttribute
string codeWithFlags = GetBasicCode_EnumWithFlagsAttributes(code, hasFlags: true);
await VerifyVB.VerifyAnalyzerAsync(codeWithFlags);
}

private static DiagnosticResult GetCA1027CSharpResultAt(int line, int column, string enumTypeName)
#pragma warning disable RS0030 // Do not use banned APIs
=> VerifyCS.Diagnostic(EnumWithFlagsAttributeAnalyzer.Rule1027)
Expand Down