Skip to content

Commit 14bdb62

Browse files
authored
Merge pull request #21726 from hvitved/csharp/useless-to-string-fps
C#: Fix FPs in `RedundantToStringCall.ql`
2 parents 3073c1c + 7bfdfbe commit 14bdb62

File tree

7 files changed

+31
-12
lines changed

7 files changed

+31
-12
lines changed

csharp/ql/lib/semmle/code/csharp/commons/Strings.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class ImplicitToStringExpr extends Expr {
2929
m = p.getCallable()
3030
|
3131
m = any(SystemTextStringBuilderClass c).getAMethod() and
32-
m.getName().regexpMatch("Append(Line)?") and
32+
m.getName() = "Append" and
3333
not p.getType() instanceof ArrayType
3434
or
3535
p instanceof StringFormatItemParameter and

csharp/ql/src/Useless code/RedundantToStringCall.ql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ import semmle.code.csharp.frameworks.System
1818
from MethodCall mc
1919
where
2020
mc instanceof ImplicitToStringExpr and
21-
mc.getTarget() instanceof ToStringMethod
22-
select mc, "Redundant call to 'ToString' on a String object."
21+
mc.getTarget() instanceof ToStringMethod and
22+
not mc.getQualifier() instanceof BaseAccess
23+
select mc, "Redundant call to 'ToString'."
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The query `cs/useless-tostring-call` has been updated to avoid false
5+
positive results in calls to `StringBuilder.AppendLine` and calls of
6+
the form `base.ToString()`. Moreover, the alert message has been
7+
made more precise.
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()); // GOOD
21+
22+
Console.WriteLine($"Hello: {base.ToString()}"); // GOOD
1523
}
1624
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
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. |
4-
| RedundantToStringCallBad.cs:7:45:7:56 | 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'. |
2+
| RedundantToStringCall.cs:11:37:11:48 | call to method ToString | Redundant call to 'ToString'. |
3+
| RedundantToStringCall.cs:14:39:14:50 | call to method ToString | Redundant call to 'ToString'. |
4+
| RedundantToStringCall.cs:18:19:18:30 | call to method ToString | Redundant call to 'ToString'. |
5+
| RedundantToStringCallBad.cs:7:45:7:56 | call to method ToString | Redundant call to 'ToString'. |
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)