Skip to content

Commit 931b6b4

Browse files
committed
C#: Exclude interfaces and abstract classes from cs/call-to-object-tostring
1 parent 83e2689 commit 931b6b4

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

csharp/ql/src/Useless code/DefaultToString.ql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ predicate alwaysDefaultToString(ValueOrRefType t) {
5454
not exists(RefType overriding |
5555
overriding.getAMethod() instanceof ToStringMethod and
5656
overriding.getABaseType+() = t
57-
)
57+
) and
58+
not t.isAbstract() and
59+
not t instanceof Interface
5860
}
5961

6062
newtype TDefaultToStringType = TDefaultToStringType0(ValueOrRefType t) { alwaysDefaultToString(t) }

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ void M()
2929

3030
var sb = new StringBuilder();
3131
sb.Append(new char[] { 'a', 'b', 'c' }, 0, 3); // GOOD
32+
33+
IInterface f = null;
34+
Console.WriteLine(f); // GOOD
3235
}
3336

3437
class A
@@ -48,6 +51,10 @@ class D : C
4851
{
4952
override public string ToString() { return "D"; }
5053
}
54+
55+
interface IInterface
56+
{
57+
}
5158
}
5259

5360
// semmle-extractor-options: /r:System.Runtime.Extensions.dll

0 commit comments

Comments
 (0)