C#: Fix FPs in RedundantToStringCall.ql#21726
Merged
hvitved merged 4 commits intogithub:mainfrom Apr 17, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the C# RedundantToStringCall query and its tests to avoid false positives in cases where an explicit ToString() call is not actually redundant (notably base.ToString() and StringBuilder.AppendLine(...)).
Changes:
- Extend the query to exclude
base.ToString()from being reported as redundant. - Refine the
ImplicitToStringExprmodeling forStringBuilderto coverAppend(...)but notAppendLine(...). - Migrate/expand the query test to inline expectations (
// $ Alert) and update expected results accordingly.
Show a summary per file
| File | Description |
|---|---|
| csharp/ql/test/query-tests/Useless Code/RedundantToStringCall/RedundantToStringCallBad.cs | Adds inline expectation marker for the “bad” string.Format(... o.ToString()) case. |
| csharp/ql/test/query-tests/Useless Code/RedundantToStringCall/RedundantToStringCall.qlref | Switches to structured qlref with postprocessing via InlineExpectationsTestQuery.ql. |
| csharp/ql/test/query-tests/Useless Code/RedundantToStringCall/RedundantToStringCall.expected | Updates expected locations and adds the new StringBuilder.Append(o.ToString()) alert. |
| csharp/ql/test/query-tests/Useless Code/RedundantToStringCall/RedundantToStringCall.cs | Adds StringBuilder scenarios and inline expectations; includes a base.ToString() “GOOD” regression case. |
| csharp/ql/src/Useless code/RedundantToStringCall.ql | Excludes base.ToString() from results to prevent false positives. |
| csharp/ql/lib/semmle/code/csharp/commons/Strings.qll | Restricts StringBuilder implicit-ToString context to Append (not AppendLine). |
Copilot's findings
- Files reviewed: 6/6 changed files
- Comments generated: 0
geoffw0
previously approved these changes
Apr 17, 2026
Contributor
geoffw0
left a comment
There was a problem hiding this comment.
LGTM, couple of questions because my C# knowledge is limited.
Contributor
|
Changes look good, but perhaps make a change note (false positive removal & query alert message changed); Test output needs to be updated. |
8988182 to
7bfdfbe
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.