Skip to content

Commit 426962e

Browse files
committed
C#: Fix FPs in RedundantToStringCall.ql
1 parent 33e9c02 commit 426962e

File tree

4 files changed

+5
-6
lines changed

4 files changed

+5
-6
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: 2 additions & 1 deletion
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
21+
mc.getTarget() instanceof ToStringMethod and
22+
not mc.getQualifier() instanceof BaseAccess
2223
select mc, "Redundant call to 'ToString' on a String object."

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public void M(object o)
1717
var sb = new StringBuilder();
1818
sb.Append(o.ToString()); // $ Alert
1919
sb.Append(o); // GOOD
20-
sb.AppendLine(o.ToString()); // $ SPURIOUS: Alert
20+
sb.AppendLine(o.ToString()); // GOOD
2121

22-
Console.WriteLine($"Hello: {base.ToString()}"); // $ SPURIOUS: Alert
22+
Console.WriteLine($"Hello: {base.ToString()}"); // GOOD
2323
}
2424
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,4 @@
22
| RedundantToStringCall.cs:11:37:11:48 | call to method ToString | Redundant call to 'ToString' on a String object. |
33
| RedundantToStringCall.cs:14:39:14:50 | call to method ToString | Redundant call to 'ToString' on a String object. |
44
| 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. |
75
| RedundantToStringCallBad.cs:7:45:7:56 | call to method ToString | Redundant call to 'ToString' on a String object. |

0 commit comments

Comments
 (0)