Skip to content

Commit

Permalink
Fix RCS1234 (#1233)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubreznak authored Oct 28, 2023
1 parent bc9b8ac commit facf447
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
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,
}
");
}
}

0 comments on commit facf447

Please sign in to comment.