Skip to content

Commit 8409642

Browse files
authored
Fix analyzer RCS1198 (#1501)
1 parent 5428aab commit 8409642

File tree

3 files changed

+8
-61
lines changed

3 files changed

+8
-61
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 analyzer [RCS1182](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1182) ([PR](https://github.com/dotnet/roslynator/pull/1502))
13+
- Fix analyzer [RCS1198](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1198) ([PR](https://github.com/dotnet/roslynator/pull/1501))
1314

1415
## [4.12.4] - 2024-06-01
1516

src/Analyzers/CSharp/Analysis/AvoidBoxingOfValueTypeAnalyzer.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ public override void Initialize(AnalysisContext context)
2828
{
2929
base.Initialize(context);
3030

31-
context.RegisterSyntaxNodeAction(f => AnalyzeInterpolation(f), SyntaxKind.Interpolation);
31+
context.RegisterCompilationStartAction(startContext =>
32+
{
33+
INamedTypeSymbol exceptionSymbol = startContext.Compilation.GetTypeByMetadataName("System.Runtime.CompilerServices.DefaultInterpolatedStringHandler");
34+
35+
if (exceptionSymbol is null)
36+
startContext.RegisterSyntaxNodeAction(f => AnalyzeInterpolation(f), SyntaxKind.Interpolation);
37+
});
3238
}
3339

3440
private static void AnalyzeInterpolation(SyntaxNodeAnalysisContext context)

src/Tests/Analyzers.Tests/RCS1198AvoidBoxingOfValueTypeTests.cs

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -12,66 +12,6 @@ public class RCS1198AvoidBoxingOfValueTypeTests : AbstractCSharpDiagnosticVerifi
1212
{
1313
public override DiagnosticDescriptor Descriptor { get; } = DiagnosticRules.AvoidBoxingOfValueType;
1414

15-
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.AvoidBoxingOfValueType)]
16-
public async Task Test_Interpolation()
17-
{
18-
await VerifyDiagnosticAndFixAsync("""
19-
using System;
20-
21-
class C
22-
{
23-
public TimeSpan P { get; }
24-
25-
void M()
26-
{
27-
var c = new C();
28-
29-
var x = $"{[|c?.P.TotalMilliseconds|]}";
30-
}
31-
}
32-
""", """
33-
using System;
34-
35-
class C
36-
{
37-
public TimeSpan P { get; }
38-
39-
void M()
40-
{
41-
var c = new C();
42-
43-
var x = $"{(c?.P.TotalMilliseconds).ToString()}";
44-
}
45-
}
46-
""");
47-
}
48-
49-
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.AvoidBoxingOfValueType)]
50-
public async Task Test_Interpolation_NullableType()
51-
{
52-
await VerifyDiagnosticAndFixAsync("""
53-
class C
54-
{
55-
void M()
56-
{
57-
int? i = null;
58-
59-
string s = $"{[|i|]}";
60-
}
61-
}
62-
""", """
63-
class C
64-
{
65-
void M()
66-
{
67-
int? i = null;
68-
69-
string s = $"{i?.ToString()}";
70-
}
71-
}
72-
""");
73-
}
74-
7515
// https://github.com/dotnet/roslyn/pull/35006
7616
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.AvoidBoxingOfValueType)]
7717
public async Task TestNoDiagnostic_StringConcatenation()

0 commit comments

Comments
 (0)