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 RCS1234 #1233

Merged
merged 3 commits into from
Oct 28, 2023
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
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Fix [RCS1234](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1234) ([PR](https://github.com/dotnet/roslynator/pull/1233))

## [4.6.1] - 2023-10-23

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ private static EnumMemberDeclarationSyntax FindMemberByValue(
{
IFieldSymbol fieldSymbol = semanticModel.GetDeclaredSymbol(enumMember, cancellationToken);

if (CSharpUtility.IsSymbolObsolete(fieldSymbol))
continue;

if (!SymbolEqualityComparer.Default.Equals(fieldSymbolInfo.Symbol, fieldSymbol))
{
EnumFieldSymbolInfo fieldSymbolInfo2 = EnumFieldSymbolInfo.Create(fieldSymbol);
Expand Down
4 changes: 4 additions & 0 deletions src/Analyzers/CSharp/Analysis/EnumSymbolAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ private static void AnalyzeNamedType(SymbolAnalysisContext context)
continue;
}

if (CSharpUtility.IsSymbolObsolete(symbolInfo1.Symbol)
|| CSharpUtility.IsSymbolObsolete(symbolInfo2.Symbol))
continue;

var enumMember1 = (EnumMemberDeclarationSyntax)symbolInfo1.Symbol.GetSyntax(context.CancellationToken);

if (enumMember1 is null)
Expand Down
6 changes: 5 additions & 1 deletion src/CSharp/CSharp/CSharpUtility.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
Expand Down Expand Up @@ -681,4 +680,9 @@ public static SeparatedSyntaxList<TypeParameterSyntax> GetTypeParameters(SyntaxN
return null;
}
}

public static bool IsSymbolObsolete(ISymbol symbol)
{
return symbol.HasAttribute(MetadataNames.System_ObsoleteAttribute);
}
}
44 changes: 44 additions & 0 deletions src/Tests/Analyzers.Tests/RCS1234DuplicateEnumValueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,34 @@ enum E
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.DuplicateEnumValue)]
public async Task Test6()
{
await VerifyDiagnosticAndFixAsync(@"
using System;

enum E
{
[Obsolete]
A = 1,

B = 1,
C = [|1|]
}
", @"
using System;

enum E
{
[Obsolete]
A = 1,

B = 1,
C = B
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.DuplicateEnumValue)]
public async Task TestNoDiagnostic()
{
Expand All @@ -124,4 +152,20 @@ enum E
B = A
}");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.DuplicateEnumValue)]
public async Task TestNoDiagnostic2()
{
await VerifyNoDiagnosticAsync(@"
using System;

enum E
{
[Obsolete]
A = 1,

B = 1,
}
");
}
}