Skip to content

Commit

Permalink
Fix RCS1241 (#1197)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt authored Sep 2, 2023
1 parent 0568588 commit ffa3f81
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 10 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

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

## [4.5.0] - 2023-08-27

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,18 @@ private static async Task<Document> RefactorAsync(

newTypeDeclaration = classDeclaration.WithBaseList(baseList.WithTypes(baseTypes));
}
else if (kind == SyntaxKind.RecordDeclaration)
{
var recordDeclaration = (RecordDeclarationSyntax)newTypeDeclaration;

BaseListSyntax baseList = recordDeclaration.BaseList;

SeparatedSyntaxList<BaseTypeSyntax> baseTypes = baseList.Types;

baseTypes = AddBaseType(baseTypes, baseType);

newTypeDeclaration = recordDeclaration.WithBaseList(baseList.WithTypes(baseTypes));
}
else if (kind == SyntaxKind.StructDeclaration
|| kind == SyntaxKind.RecordStructDeclaration)
{
Expand Down
22 changes: 12 additions & 10 deletions src/Analyzers/CSharp/Analysis/NamedTypeSymbolAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,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.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;

Expand Down Expand Up @@ -130,19 +132,19 @@ private static void AnalyzeNamedType(SymbolAnalysisContext context)

private static void ReportDiagnostic(SymbolAnalysisContext context, INamedTypeSymbol symbol, string interfaceName, string genericInterfaceName)
{
SyntaxToken identifier = default;
SyntaxNode node = symbol.GetSyntax(context.CancellationToken);

if (symbol.TypeKind == TypeKind.Class)
{
var classDeclaration = (ClassDeclarationSyntax)symbol.GetSyntax(context.CancellationToken);
SyntaxToken identifier = GetIdentifier();

identifier = classDeclaration.Identifier;
}
else if (symbol.TypeKind == TypeKind.Struct)
SyntaxToken GetIdentifier()
{
var structDeclaration = (StructDeclarationSyntax)symbol.GetSyntax(context.CancellationToken);

identifier = structDeclaration.Identifier;
return node switch
{
ClassDeclarationSyntax classDeclaration => classDeclaration.Identifier,
StructDeclarationSyntax structDeclaration => structDeclaration.Identifier,
RecordDeclarationSyntax recordDeclaration => recordDeclaration.Identifier,
_ => throw new InvalidOperationException($"Unknown syntax node kind '{node.Kind()}'."),
};
}

DiagnosticHelpers.ReportDiagnostic(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,51 @@ public int CompareTo(object obj)
", equivalenceKey: EquivalenceKey.Create(Descriptor.Id));
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.ImplementNonGenericCounterpart)]
public async Task Test_Record_IComparable()
{
await VerifyDiagnosticAndFixAsync(@"
using System;
using System.Collections.Generic;
public class C
{
}
public abstract record class [|Comparable|] : IComparable<C>
{
public abstract int CompareTo(C other);
}
", @"
using System;
using System.Collections.Generic;
public class C
{
}
public abstract record class Comparable : IComparable<C>, IComparable
{
public abstract int CompareTo(C other);
public int CompareTo(object obj)
{
if (obj == null)
{
return 1;
}
if (obj is C x)
{
return CompareTo(x);
}
throw new ArgumentException("""", nameof(obj));
}
}
", equivalenceKey: EquivalenceKey.Create(Descriptor.Id));
}

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

0 comments on commit ffa3f81

Please sign in to comment.