Skip to content

C#: Harden cs/useless-cast-to-self with respect to unknown types. - #22393

Merged
michaelnebel merged 2 commits into
github:mainfrom
michaelnebel:csharp/useless-cast-to-self-unknown-type
Aug 20, 2026
Merged

C#: Harden cs/useless-cast-to-self with respect to unknown types.#22393
michaelnebel merged 2 commits into
github:mainfrom
michaelnebel:csharp/useless-cast-to-self-unknown-type

Conversation

@michaelnebel

@michaelnebelmichaelnebel commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Especially for build-mode: none expressions can more easily be of unknown type (and the same goes for types used in casts). Such cases should not be reported by the query cs/useless-cast-to-self.

This change removes around 7k results across the repos in the DCA suite.

@michaelnebel
michaelnebelforce-pushed the csharp/useless-cast-to-self-unknown-type branch from 75481e2 to 97985c8CompareAugust 20, 2026 07:31
@michaelnebel
michaelnebel marked this pull request as ready for review August 20, 2026 09:16
@michaelnebel
michaelnebel requested a review from a team as a code ownerAugust 20, 2026 09:16
CopilotAI balanced review requested due to automatic review settings August 20, 2026 09:16

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Prevents false positives from unknown C# types, especially in buildless databases.

Changes:

  • Excludes UnknownType from redundant self-cast results.
  • Documents the behavior change.
Show a summary per file
FileDescription
csharp/ql/src/Language Abuse/UselessCastToSelf.qlFilters casts involving unknown types.
csharp/ql/src/change-notes/2026-08-20-harden-useless-cast-to-self.mdAdds the fix note.

Review details

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Balanced

type = cast.getTargetType() and
type = e.getType() and
not type instanceof NullType and
not type instanceof UnknownType and
@michaelnebel
michaelnebel merged commit d83985a into github:mainAug 20, 2026
25 checks passed
@michaelnebel
michaelnebel deleted the csharp/useless-cast-to-self-unknown-type branch August 20, 2026 09:49
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@michaelnebel@hvitved