Skip to content

Commit

Permalink
C#: Add implicit to string test.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelnebel committed Jan 14, 2025
1 parent 6a406b2 commit 0c5c2a3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
44 changes: 44 additions & 0 deletions csharp/ql/test/library-tests/implicittostring/implicitToString.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;

public class TestImplicitToString
{
public class Container
{
public override string ToString()
{
return "Container";
}
}

public class FormattableContainer : IFormattable
{
public string ToString(string format, IFormatProvider formatProvider)
{
return "Formatted container";
}


public override string ToString()
{
return "Container";
}
}

public void M()
{
var container = new Container();

var y = "Hello" + container; // Implicit ToString call
y = "Hello" + container.ToString();
y = $"Hello {container}"; // Implicit ToString() call
y = $"Hello {container.ToString()}";
y = $"Hello {container:D}"; // Implicit ToString() call.

var z = "Hello" + y; // No implicit ToString call as `y` is already a string.

var formattableContainer = new FormattableContainer();
y = "Hello" + formattableContainer; // Implicit call to ToString().
y = $"Hello {formattableContainer}"; // Implicit call to ToString(string, IFormatProvider). We don't handle this.
y = $"Hello {formattableContainer:D}"; // Implicit call to ToString(string, IFormatProvider). We don't handle this.
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
| implicitToString.cs:31:27:31:35 | call to method ToString |
| implicitToString.cs:33:22:33:30 | call to method ToString |
| implicitToString.cs:35:22:35:30 | call to method ToString |
| implicitToString.cs:40:23:40:42 | call to method ToString |
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import csharp

from MethodCall c
where c.isImplicit()
select c

0 comments on commit 0c5c2a3

Please sign in to comment.