mirror of
https://github.com/github/codeql.git
synced 2025-12-17 01:03:14 +01:00
Merge pull request #18508 from michaelnebel/csharp/implicitinheritedtostring
C#: Also syntheize calls to inherited `ToString`.
This commit is contained in:
@@ -14,7 +14,12 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
|
||||
/// </summary>
|
||||
private static IMethodSymbol? GetToStringMethod(ITypeSymbol? type)
|
||||
{
|
||||
return type?
|
||||
if (type is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var toString = type
|
||||
.GetMembers()
|
||||
.OfType<IMethodSymbol>()
|
||||
.Where(method =>
|
||||
@@ -22,6 +27,8 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
|
||||
method.Parameters.Length == 0
|
||||
)
|
||||
.FirstOrDefault();
|
||||
|
||||
return toString ?? GetToStringMethod(type.BaseType);
|
||||
}
|
||||
|
||||
private ImplicitToString(ExpressionNodeInfo info, IMethodSymbol toString) : base(new ExpressionInfo(info.Context, AnnotatedTypeSymbol.CreateNotAnnotated(toString.ReturnType), info.Location, ExprKind.METHOD_INVOCATION, info.Parent, info.Child, isCompilerGenerated: true, info.ExprValue))
|
||||
|
||||
@@ -10,6 +10,10 @@ public class TestImplicitToString
|
||||
}
|
||||
}
|
||||
|
||||
public class Container2 : Container { }
|
||||
|
||||
public class Container3 { }
|
||||
|
||||
public class FormattableContainer : IFormattable
|
||||
{
|
||||
public string ToString(string format, IFormatProvider formatProvider)
|
||||
@@ -40,5 +44,11 @@ public class TestImplicitToString
|
||||
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.
|
||||
|
||||
var container2 = new Container2();
|
||||
y = "Hello" + container2; // Implicit Container.ToString call.
|
||||
|
||||
var container3 = new Container3();
|
||||
y = "Hello" + container3; // Implicit Object.ToString call.
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
| 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 |
|
||||
| implicitToString.cs:35:27:35:35 | call to method ToString | Container |
|
||||
| implicitToString.cs:37:22:37:30 | call to method ToString | Container |
|
||||
| implicitToString.cs:39:22:39:30 | call to method ToString | Container |
|
||||
| implicitToString.cs:44:23:44:42 | call to method ToString | FormattableContainer |
|
||||
| implicitToString.cs:49:23:49:32 | call to method ToString | Container |
|
||||
| implicitToString.cs:52:23:52:32 | call to method ToString | Object |
|
||||
|
||||
@@ -2,4 +2,4 @@ import csharp
|
||||
|
||||
from MethodCall c
|
||||
where c.isImplicit()
|
||||
select c
|
||||
select c, c.getTarget().getDeclaringType().toString()
|
||||
|
||||
@@ -908,8 +908,8 @@
|
||||
| D.cs:253:13:253:14 | access to local variable o2 | null | D.cs:249:18:249:38 | ... ? ... : ... | null |
|
||||
| D.cs:266:13:266:27 | ... is ... | true | D.cs:266:13:266:17 | access to local variable other | non-null |
|
||||
| D.cs:310:21:310:26 | ... + ... | non-null | D.cs:310:21:310:22 | "" | non-null |
|
||||
| D.cs:310:21:310:26 | ... + ... | non-null | D.cs:310:26:310:26 | access to parameter a | non-null |
|
||||
| D.cs:310:21:310:26 | ... + ... | null | D.cs:310:26:310:26 | access to parameter a | null |
|
||||
| D.cs:310:21:310:26 | ... + ... | non-null | D.cs:310:26:310:26 | call to method ToString | non-null |
|
||||
| D.cs:310:21:310:26 | ... + ... | null | D.cs:310:26:310:26 | call to method ToString | null |
|
||||
| D.cs:312:17:312:23 | !... | false | D.cs:312:18:312:23 | access to local variable s_null | true |
|
||||
| D.cs:312:17:312:23 | !... | true | D.cs:312:18:312:23 | access to local variable s_null | false |
|
||||
| D.cs:318:16:318:62 | ... && ... | true | D.cs:318:16:318:36 | ... == ... | true |
|
||||
|
||||
Reference in New Issue
Block a user