Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- 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))
- Fix analyzer [RCS1214](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1214) ([PR](https://github.com/dotnet/roslynator/pull/1500))

## [4.12.4] - 2024-06-01

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private static void AnalyzeInterpolatedStringExpression(SyntaxNodeAnalysisContex

if (ConvertInterpolatedStringToStringLiteralAnalysis.IsFixable(contents))
{
if (IsFormattableString(context))
if (CheckConvertedType(context))
return;

ReportDiagnostic(
Expand All @@ -87,7 +87,7 @@ private static void AnalyzeInterpolatedStringExpression(SyntaxNodeAnalysisContex
if (!IsNonNullStringExpression(expression))
return;

if (IsFormattableString(context))
if (CheckConvertedType(context))
return;

ReportDiagnostic(context, DiagnosticRules.UnnecessaryInterpolatedString, interpolatedString);
Expand All @@ -110,13 +110,15 @@ bool IsNonNullStringExpression(ExpressionSyntax expression)
&& value is not null;
}

static bool IsFormattableString(SyntaxNodeAnalysisContext context)
static bool CheckConvertedType(SyntaxNodeAnalysisContext context)
{
return context
ITypeSymbol convertedType = context
.SemanticModel
.GetTypeInfo(context.Node, context.CancellationToken)
.ConvertedType?
.HasMetadataName(MetadataNames.System_FormattableString) == true;
.ConvertedType;

return convertedType?.HasMetadataName(MetadataNames.System_FormattableString) == true
|| convertedType?.HasMetadataName(MetadataNames.System_MemoryExtensions_TryWriteInterpolatedStringHandler) == true;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/Core/MetadataNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ internal static class MetadataNames
public static readonly MetadataName System_Linq_ImmutableArrayExtensions = MetadataName.Parse("System.Linq.ImmutableArrayExtensions");
public static readonly MetadataName System_Linq_IOrderedEnumerable_T = MetadataName.Parse("System.Linq.IOrderedEnumerable`1");
public static readonly MetadataName System_Linq_IQueryable_T = MetadataName.Parse("System.Linq.IQueryable`1");
public static readonly MetadataName System_MemoryExtensions_TryWriteInterpolatedStringHandler = MetadataName.Parse("System.MemoryExtensions+TryWriteInterpolatedStringHandler");
public static readonly MetadataName System_NonSerializedAttribute = MetadataName.Parse("System.NonSerializedAttribute");
public static readonly MetadataName System_ObsoleteAttribute = MetadataName.Parse("System.ObsoleteAttribute");
public static readonly MetadataName System_ReadOnlySpan_T = MetadataName.Parse("System.ReadOnlySpan`1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,24 @@ string Call(FormattableString s)
}

}
""");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UnnecessaryInterpolatedString)]
public async Task TestNoDiagnostic_TryWriteInterpolatedStringHandler()
{
await VerifyNoDiagnosticAsync("""
using System;
using System.Globalization;

class C
{
public void M()
{
Span<char> bar = stackalloc char[1];
bar.TryWrite(CultureInfo.InvariantCulture, $"foo", out _);
}
}
""");
}
}