Skip to content

Commit

Permalink
Fix analyzer RCS1198 (#1501)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt authored Aug 17, 2024
1 parent 5428aab commit 8409642
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 61 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 analyzer [RCS1182](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1182) ([PR](https://github.com/dotnet/roslynator/pull/1502))
- Fix analyzer [RCS1198](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1198) ([PR](https://github.com/dotnet/roslynator/pull/1501))

## [4.12.4] - 2024-06-01

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ public override void Initialize(AnalysisContext context)
{
base.Initialize(context);

context.RegisterSyntaxNodeAction(f => AnalyzeInterpolation(f), SyntaxKind.Interpolation);
context.RegisterCompilationStartAction(startContext =>
{
INamedTypeSymbol exceptionSymbol = startContext.Compilation.GetTypeByMetadataName("System.Runtime.CompilerServices.DefaultInterpolatedStringHandler");
if (exceptionSymbol is null)
startContext.RegisterSyntaxNodeAction(f => AnalyzeInterpolation(f), SyntaxKind.Interpolation);
});
}

private static void AnalyzeInterpolation(SyntaxNodeAnalysisContext context)
Expand Down
60 changes: 0 additions & 60 deletions src/Tests/Analyzers.Tests/RCS1198AvoidBoxingOfValueTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,66 +12,6 @@ public class RCS1198AvoidBoxingOfValueTypeTests : AbstractCSharpDiagnosticVerifi
{
public override DiagnosticDescriptor Descriptor { get; } = DiagnosticRules.AvoidBoxingOfValueType;

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

class C
{
public TimeSpan P { get; }

void M()
{
var c = new C();

var x = $"{[|c?.P.TotalMilliseconds|]}";
}
}
""", """
using System;

class C
{
public TimeSpan P { get; }

void M()
{
var c = new C();

var x = $"{(c?.P.TotalMilliseconds).ToString()}";
}
}
""");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.AvoidBoxingOfValueType)]
public async Task Test_Interpolation_NullableType()
{
await VerifyDiagnosticAndFixAsync("""
class C
{
void M()
{
int? i = null;

string s = $"{[|i|]}";
}
}
""", """
class C
{
void M()
{
int? i = null;

string s = $"{i?.ToString()}";
}
}
""");
}

// https://github.com/dotnet/roslyn/pull/35006
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.AvoidBoxingOfValueType)]
public async Task TestNoDiagnostic_StringConcatenation()
Expand Down

0 comments on commit 8409642

Please sign in to comment.