mirror of
https://github.com/github/codeql.git
synced 2026-04-29 02:35:15 +02:00
C#: Do not insert a synthetic ToString call in interpolation expressions, if the type implements IFormattable.
This commit is contained in:
@@ -532,6 +532,12 @@ namespace Semmle.Extraction.CSharp
|
||||
return isInline;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if this type implements `System.IFormattable`.
|
||||
/// </summary>
|
||||
public static bool ImplementsIFormattable(this ITypeSymbol type) =>
|
||||
type.AllInterfaces.Any(i => i.Name == "IFormattable" && i.ContainingNamespace.ToString() == "System");
|
||||
|
||||
/// <summary>
|
||||
/// Holds if this type is of the form <code>System.ReadOnlySpan<byte></code>.
|
||||
/// </summary>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.IO;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Semmle.Extraction.Kinds;
|
||||
@@ -20,7 +21,15 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
|
||||
{
|
||||
case SyntaxKind.Interpolation:
|
||||
var interpolation = (InterpolationSyntax)c;
|
||||
ImplicitToString.Create(Context, interpolation.Expression, this, child++);
|
||||
var exp = interpolation.Expression;
|
||||
if (Context.GetTypeInfo(exp).Type is ITypeSymbol type && !type.ImplementsIFormattable())
|
||||
{
|
||||
ImplicitToString.Create(Context, exp, this, child++);
|
||||
}
|
||||
else
|
||||
{
|
||||
Create(Context, exp, this, child++);
|
||||
}
|
||||
break;
|
||||
case SyntaxKind.InterpolatedStringText:
|
||||
// Create a string literal
|
||||
|
||||
Reference in New Issue
Block a user