Skip to content

Commit a35510d

Browse files
authored
Merge pull request #18849 from michaelnebel/csharp/tupledefaulttostring
C#: Proper handling of value tuples in `cs/call-to-object-tostring`.
2 parents 74a2495 + e8f86e4 commit a35510d

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

csharp/ql/src/Useless code/DefaultToStringQuery.qll

+6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ private predicate alwaysInvokesToString(ParameterRead pr) {
4646
* method from `System.Object` or `System.ValueType`.
4747
*/
4848
predicate alwaysDefaultToString(ValueOrRefType t) {
49+
not t instanceof TupleType and
4950
exists(ToStringMethod m | t.hasMethod(m) |
5051
m.getDeclaringType() instanceof SystemObjectClass or
5152
m.getDeclaringType() instanceof SystemValueTypeClass
@@ -55,6 +56,11 @@ predicate alwaysDefaultToString(ValueOrRefType t) {
5556
overriding.getABaseType+() = t
5657
) and
5758
((t.isAbstract() or t instanceof Interface) implies not t.isEffectivelyPublic())
59+
or
60+
exists(ValueOrRefType elem |
61+
elem = t.(TupleType).getElementType(_) and
62+
alwaysDefaultToString(elem)
63+
)
5864
}
5965

6066
class DefaultToStringType extends ValueOrRefType {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* C#: Improve precision of the query `cs/call-to-object-tostring` for value tuples.

csharp/ql/test/query-tests/Useless Code/DefaultToString/DefaultToString.cs

+10
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ void M()
3535

3636
IPublic g = null;
3737
Console.WriteLine(g); // GOOD
38+
39+
Console.WriteLine(new ValueTuple<int, int>(1, 2)); // GOOD
40+
41+
Console.WriteLine((1, 2)); // GOOD
42+
43+
var t1 = new ValueTuple<int, DefaultToString>(1, new DefaultToString());
44+
Console.WriteLine(t1); // BAD
45+
46+
var t2 = new ValueTuple<A, D>(new A(), new D());
47+
Console.WriteLine(t2); // GOOD
3848
}
3949

4050
class A

csharp/ql/test/query-tests/Useless Code/DefaultToString/DefaultToString.expected

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
| DefaultToString.cs:10:28:10:28 | access to local variable d | Default 'ToString()': $@ inherits 'ToString()' from 'Object', and so is not suitable for printing. | DefaultToString.cs:4:14:4:28 | DefaultToString | DefaultToString |
33
| DefaultToString.cs:16:27:16:30 | access to local variable ints | Default 'ToString()': $@ inherits 'ToString()' from 'Object', and so is not suitable for printing. | file://:0:0:0:0 | Int32[] | Int32[] |
44
| DefaultToString.cs:19:24:19:27 | access to local variable ints | Default 'ToString()': $@ inherits 'ToString()' from 'Object', and so is not suitable for printing. | file://:0:0:0:0 | Int32[] | Int32[] |
5-
| DefaultToString.cs:34:27:34:27 | access to local variable f | Default 'ToString()': $@ inherits 'ToString()' from 'Object', and so is not suitable for printing. | DefaultToString.cs:62:23:62:30 | IPrivate | IPrivate |
5+
| DefaultToString.cs:34:27:34:27 | access to local variable f | Default 'ToString()': $@ inherits 'ToString()' from 'Object', and so is not suitable for printing. | DefaultToString.cs:72:23:72:30 | IPrivate | IPrivate |
6+
| DefaultToString.cs:44:27:44:28 | (...) ... | Default 'ToString()': $@ inherits 'ToString()' from 'Object', and so is not suitable for printing. | file://:0:0:0:0 | (Int32,DefaultToString) | (Int32,DefaultToString) |
67
| DefaultToStringBad.cs:8:35:8:35 | access to local variable p | Default 'ToString()': $@ inherits 'ToString()' from 'Object', and so is not suitable for printing. | DefaultToStringBad.cs:14:11:14:16 | Person | Person |
78
| DefaultToStringBad.cs:11:38:11:41 | access to local variable ints | Default 'ToString()': $@ inherits 'ToString()' from 'Object', and so is not suitable for printing. | file://:0:0:0:0 | Int32[] | Int32[] |
89
| WriteLineArray.cs:7:23:7:26 | access to parameter args | Default 'ToString()': $@ inherits 'ToString()' from 'Object', and so is not suitable for printing. | file://:0:0:0:0 | String[] | String[] |

0 commit comments

Comments
 (0)