From 0c5c2a3509b0f06678674d5ae94db42a5e8f8b94 Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Tue, 14 Jan 2025 15:50:04 +0100 Subject: [PATCH] C#: Add implicit to string test. --- .../implicittostring/implicitToString.cs | 44 +++++++++++++++++++ .../implicitToString.expected | 4 ++ .../implicittostring/implicitToString.ql | 5 +++ 3 files changed, 53 insertions(+) create mode 100644 csharp/ql/test/library-tests/implicittostring/implicitToString.cs create mode 100644 csharp/ql/test/library-tests/implicittostring/implicitToString.expected create mode 100644 csharp/ql/test/library-tests/implicittostring/implicitToString.ql diff --git a/csharp/ql/test/library-tests/implicittostring/implicitToString.cs b/csharp/ql/test/library-tests/implicittostring/implicitToString.cs new file mode 100644 index 000000000000..d75044246f8e --- /dev/null +++ b/csharp/ql/test/library-tests/implicittostring/implicitToString.cs @@ -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. + } +} diff --git a/csharp/ql/test/library-tests/implicittostring/implicitToString.expected b/csharp/ql/test/library-tests/implicittostring/implicitToString.expected new file mode 100644 index 000000000000..4878b12a4fdb --- /dev/null +++ b/csharp/ql/test/library-tests/implicittostring/implicitToString.expected @@ -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 | diff --git a/csharp/ql/test/library-tests/implicittostring/implicitToString.ql b/csharp/ql/test/library-tests/implicittostring/implicitToString.ql new file mode 100644 index 000000000000..4eb518d84c39 --- /dev/null +++ b/csharp/ql/test/library-tests/implicittostring/implicitToString.ql @@ -0,0 +1,5 @@ +import csharp + +from MethodCall c +where c.isImplicit() +select c