Skip to content

Commit ffa3f81

Browse files
authored
Fix RCS1241 (#1197)
1 parent 0568588 commit ffa3f81

File tree

4 files changed

+70
-10
lines changed

4 files changed

+70
-10
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Fixed
1111

1212
- Fix [RCS1164](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1164) ([#1196](https://github.com/JosefPihrt/Roslynator/pull/1196)).
13+
- Fix [RCS1241](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1241) ([#1197](https://github.com/JosefPihrt/Roslynator/pull/1197)).
1314

1415
## [4.5.0] - 2023-08-27
1516

src/Analyzers.CodeFixes/CSharp/CodeFixes/ImplementNonGenericCounterpartCodeFixProvider.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,18 @@ private static async Task<Document> RefactorAsync(
290290

291291
newTypeDeclaration = classDeclaration.WithBaseList(baseList.WithTypes(baseTypes));
292292
}
293+
else if (kind == SyntaxKind.RecordDeclaration)
294+
{
295+
var recordDeclaration = (RecordDeclarationSyntax)newTypeDeclaration;
296+
297+
BaseListSyntax baseList = recordDeclaration.BaseList;
298+
299+
SeparatedSyntaxList<BaseTypeSyntax> baseTypes = baseList.Types;
300+
301+
baseTypes = AddBaseType(baseTypes, baseType);
302+
303+
newTypeDeclaration = recordDeclaration.WithBaseList(baseList.WithTypes(baseTypes));
304+
}
293305
else if (kind == SyntaxKind.StructDeclaration
294306
|| kind == SyntaxKind.RecordStructDeclaration)
295307
{

src/Analyzers/CSharp/Analysis/NamedTypeSymbolAnalyzer.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// Copyright (c) Josef Pihrt and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
22

3+
using System;
34
using System.Collections.Generic;
45
using System.Collections.Immutable;
56
using System.Linq;
67
using Microsoft.CodeAnalysis;
8+
using Microsoft.CodeAnalysis.CSharp;
79
using Microsoft.CodeAnalysis.CSharp.Syntax;
810
using Microsoft.CodeAnalysis.Diagnostics;
911

@@ -130,19 +132,19 @@ private static void AnalyzeNamedType(SymbolAnalysisContext context)
130132

131133
private static void ReportDiagnostic(SymbolAnalysisContext context, INamedTypeSymbol symbol, string interfaceName, string genericInterfaceName)
132134
{
133-
SyntaxToken identifier = default;
135+
SyntaxNode node = symbol.GetSyntax(context.CancellationToken);
134136

135-
if (symbol.TypeKind == TypeKind.Class)
136-
{
137-
var classDeclaration = (ClassDeclarationSyntax)symbol.GetSyntax(context.CancellationToken);
137+
SyntaxToken identifier = GetIdentifier();
138138

139-
identifier = classDeclaration.Identifier;
140-
}
141-
else if (symbol.TypeKind == TypeKind.Struct)
139+
SyntaxToken GetIdentifier()
142140
{
143-
var structDeclaration = (StructDeclarationSyntax)symbol.GetSyntax(context.CancellationToken);
144-
145-
identifier = structDeclaration.Identifier;
141+
return node switch
142+
{
143+
ClassDeclarationSyntax classDeclaration => classDeclaration.Identifier,
144+
StructDeclarationSyntax structDeclaration => structDeclaration.Identifier,
145+
RecordDeclarationSyntax recordDeclaration => recordDeclaration.Identifier,
146+
_ => throw new InvalidOperationException($"Unknown syntax node kind '{node.Kind()}'."),
147+
};
146148
}
147149

148150
DiagnosticHelpers.ReportDiagnostic(

src/Tests/Analyzers.Tests/RCS1241ImplementNonGenericCounterpartTests.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,51 @@ public int CompareTo(object obj)
6464
", equivalenceKey: EquivalenceKey.Create(Descriptor.Id));
6565
}
6666

67+
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.ImplementNonGenericCounterpart)]
68+
public async Task Test_Record_IComparable()
69+
{
70+
await VerifyDiagnosticAndFixAsync(@"
71+
using System;
72+
using System.Collections.Generic;
73+
74+
public class C
75+
{
76+
}
77+
78+
public abstract record class [|Comparable|] : IComparable<C>
79+
{
80+
public abstract int CompareTo(C other);
81+
}
82+
", @"
83+
using System;
84+
using System.Collections.Generic;
85+
86+
public class C
87+
{
88+
}
89+
90+
public abstract record class Comparable : IComparable<C>, IComparable
91+
{
92+
public abstract int CompareTo(C other);
93+
94+
public int CompareTo(object obj)
95+
{
96+
if (obj == null)
97+
{
98+
return 1;
99+
}
100+
101+
if (obj is C x)
102+
{
103+
return CompareTo(x);
104+
}
105+
106+
throw new ArgumentException("""", nameof(obj));
107+
}
108+
}
109+
", equivalenceKey: EquivalenceKey.Create(Descriptor.Id));
110+
}
111+
67112
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.ImplementNonGenericCounterpart)]
68113
public async Task Test_IComparable_Explicit()
69114
{

0 commit comments

Comments
 (0)