Skip to content

Commit 33e9c02

Browse files
committed
C#: Add more tests for RedundantToStringCall.ql
1 parent ee34e33 commit 33e9c02

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
using System;
2+
using System.Text;
23

34
class RedundantToString
45
{
56
public void M(object o)
67
{
7-
Console.WriteLine(o.ToString()); // BAD
8+
Console.WriteLine(o.ToString()); // $ Alert
89
Console.WriteLine(o); // GOOD
910

10-
Console.WriteLine($"Hello: {o.ToString()}"); // BAD
11+
Console.WriteLine($"Hello: {o.ToString()}"); // $ Alert
1112
Console.WriteLine($"Hello: {o}"); // GOOD
1213

13-
Console.WriteLine("Hello: " + o.ToString()); // BAD
14+
Console.WriteLine("Hello: " + o.ToString()); // $ Alert
1415
Console.WriteLine("Hello: " + o); // GOOD
16+
17+
var sb = new StringBuilder();
18+
sb.Append(o.ToString()); // $ Alert
19+
sb.Append(o); // GOOD
20+
sb.AppendLine(o.ToString()); // $ SPURIOUS: Alert
21+
22+
Console.WriteLine($"Hello: {base.ToString()}"); // $ SPURIOUS: Alert
1523
}
1624
}
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
| RedundantToStringCall.cs:7:27:7:38 | call to method ToString | Redundant call to 'ToString' on a String object. |
2-
| RedundantToStringCall.cs:10:37:10:48 | call to method ToString | Redundant call to 'ToString' on a String object. |
3-
| RedundantToStringCall.cs:13:39:13:50 | call to method ToString | Redundant call to 'ToString' on a String object. |
1+
| RedundantToStringCall.cs:8:27:8:38 | call to method ToString | Redundant call to 'ToString' on a String object. |
2+
| RedundantToStringCall.cs:11:37:11:48 | call to method ToString | Redundant call to 'ToString' on a String object. |
3+
| RedundantToStringCall.cs:14:39:14:50 | call to method ToString | Redundant call to 'ToString' on a String object. |
4+
| RedundantToStringCall.cs:18:19:18:30 | call to method ToString | Redundant call to 'ToString' on a String object. |
5+
| RedundantToStringCall.cs:20:23:20:34 | call to method ToString | Redundant call to 'ToString' on a String object. |
6+
| RedundantToStringCall.cs:22:37:22:51 | call to method ToString | Redundant call to 'ToString' on a String object. |
47
| RedundantToStringCallBad.cs:7:45:7:56 | call to method ToString | Redundant call to 'ToString' on a String object. |
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
Useless code/RedundantToStringCall.ql
1+
query: Useless code/RedundantToStringCall.ql
2+
postprocess:
3+
- utils/test/InlineExpectationsTestQuery.ql

csharp/ql/test/query-tests/Useless Code/RedundantToStringCall/RedundantToStringCallBad.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ class Bad
44
{
55
static string Hello(object o)
66
{
7-
return string.Format("Hello, {0}!", o.ToString());
7+
return string.Format("Hello, {0}!", o.ToString()); // $ Alert
88
}
99
}

0 commit comments

Comments
 (0)