Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C#: Also syntheize calls to inherited ToString. #18508

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@ internal sealed class ImplicitToString : Expression
/// </summary>
private static IMethodSymbol? GetToStringMethod(ITypeSymbol? type)
{
return type?
if (type is null)
{
return null;
}

var toString = type
.GetMembers()
.OfType<IMethodSymbol>()
.Where(method =>
method.GetName() == "ToString" &&
method.Parameters.Length == 0
)
.FirstOrDefault();

return toString ?? GetToStringMethod(type.BaseType);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's odd that we have no helper for getting the base types already. Maybe adding this to SymbolExtensions would make sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the base types are used in all sorts of different ways and I didn't find any overlapping use cases for recursively going through the type hierarchy to find a method.

}

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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ public override string ToString()
}
}

public class Container2 : Container { }

public class Container3 { }

public class FormattableContainer : IFormattable
{
public string ToString(string format, IFormatProvider formatProvider)
Expand Down Expand Up @@ -40,5 +44,11 @@ public void M()
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.
}
}
Original file line number Diff line number Diff line change
@@ -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 |
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import csharp

from MethodCall c
where c.isImplicit()
select c
select c, c.getTarget().getDeclaringType().toString()
4 changes: 2 additions & 2 deletions csharp/ql/test/query-tests/Nullness/Implications.expected
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down