Skip to content

C#: Fix FPs in RedundantToStringCall.ql#21726

Merged
hvitved merged 4 commits intogithub:mainfrom
hvitved:csharp/useless-to-string-fps
Apr 17, 2026
Merged

C#: Fix FPs in RedundantToStringCall.ql#21726
hvitved merged 4 commits intogithub:mainfrom
hvitved:csharp/useless-to-string-fps

Conversation

@hvitved
Copy link
Copy Markdown
Contributor

@hvitved hvitved commented Apr 17, 2026

No description provided.

@github-actions github-actions bot added the C# label Apr 17, 2026
@hvitved hvitved added the no-change-note-required This PR does not need a change note label Apr 17, 2026
@hvitved hvitved marked this pull request as ready for review April 17, 2026 07:55
@hvitved hvitved requested a review from a team as a code owner April 17, 2026 07:55
Copilot AI review requested due to automatic review settings April 17, 2026 07:55
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ImplicitToStringExpr modeling for StringBuilder to cover Append(...) but not AppendLine(...).
  • 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
geoffw0 previously approved these changes Apr 17, 2026
Copy link
Copy Markdown
Contributor

@geoffw0 geoffw0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, couple of questions because my C# knowledge is limited.

@michaelnebel
Copy link
Copy Markdown
Contributor

Changes look good, but perhaps make a change note (false positive removal & query alert message changed); Test output needs to be updated.

@hvitved hvitved force-pushed the csharp/useless-to-string-fps branch from 8988182 to 7bfdfbe Compare April 17, 2026 11:57
Copy link
Copy Markdown
Contributor

@michaelnebel michaelnebel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent 👍

@hvitved hvitved merged commit 14bdb62 into github:main Apr 17, 2026
25 checks passed
@hvitved hvitved deleted the csharp/useless-to-string-fps branch April 17, 2026 12:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C# documentation no-change-note-required This PR does not need a change note

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants