Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #49 from edespong/Issue42_Interpolation
Browse files Browse the repository at this point in the history
Warn if value type is used in string interpolation
  • Loading branch information
Mukul Sabharwal authored Jul 10, 2018
2 parents 2c2aada + 8bf702a commit 9afb157
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,27 @@ public static implicit operator string(AStruct astruct)
Assert.AreEqual(0, info1.Allocations.Count);
}

[TestMethod]
public void TypeConversionAllocation_InterpolatedStringWithInt_BoxingWarning() {
var sampleProgram = @"string s = $""{1}"";";

var analyser = new TypeConversionAllocationAnalyzer();
var info = ProcessCode(analyser, sampleProgram, ImmutableArray.Create(SyntaxKind.Interpolation));

Assert.AreEqual(1, info.Allocations.Count);
AssertEx.ContainsDiagnostic(info.Allocations, id: TypeConversionAllocationAnalyzer.ValueTypeToReferenceTypeConversionRule.Id, line: 1, character: 15);
}

[TestMethod]
public void TypeConversionAllocation_InterpolatedStringWithString_NoWarning() {
var sampleProgram = @"string s = $""{1.ToString()}"";";

var analyser = new TypeConversionAllocationAnalyzer();
var info = ProcessCode(analyser, sampleProgram, ImmutableArray.Create(SyntaxKind.Interpolation));

Assert.AreEqual(0, info.Allocations.Count);
}

[TestMethod]
public void TypeConversionAllocation_DelegateAssignmentToReadonly_DoNotWarn()
{
Expand Down
17 changes: 17 additions & 0 deletions ClrHeapAllocationsAnalyzer/TypeConversionAllocationAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public override void Initialize(AnalysisContext context)
SyntaxKind.ConditionalExpression,
SyntaxKind.ForEachStatement,
SyntaxKind.EqualsValueClause,
SyntaxKind.Interpolation,
SyntaxKind.Argument,
SyntaxKind.ArrowExpressionClause
};
Expand Down Expand Up @@ -97,6 +98,12 @@ private static void AnalyzeNode(SyntaxNodeAnalysisContext context)
return;
}

// string a = $"{1}";
if (node is InterpolationSyntax) {
InterpolationCheck(node, semanticModel, reportDiagnostic, filePath, cancellationToken);
return;
}

// var f = (object)
if (node is CastExpressionSyntax)
{
Expand Down Expand Up @@ -173,6 +180,16 @@ private static void BinaryExpressionCheck(SyntaxNode node, SemanticModel semanti
}
}

private static void InterpolationCheck(SyntaxNode node, SemanticModel semanticModel, Action<Diagnostic> reportDiagnostic, string filePath, CancellationToken cancellationToken)
{
var interpolation = node as InterpolationSyntax;
var typeInfo = semanticModel.GetTypeInfo(interpolation.Expression, cancellationToken);
if (typeInfo.Type?.IsValueType == true) {
reportDiagnostic(Diagnostic.Create(ValueTypeToReferenceTypeConversionRule, interpolation.Expression.GetLocation(), EmptyMessageArgs));
HeapAllocationAnalyzerEventSource.Logger.BoxingAllocation(filePath);
}
}

private static void CastExpressionCheck(SyntaxNode node, SemanticModel semanticModel, Action<Diagnostic> reportDiagnostic, string filePath, CancellationToken cancellationToken)
{
var castExpression = node as CastExpressionSyntax;
Expand Down

0 comments on commit 9afb157

Please sign in to comment.