Skip to content

C#: Allow virtual member access in nameof expressions in cs/virtual-call-in-constructor.#22205

Open
michaelnebel wants to merge 5 commits into
github:mainfrom
michaelnebel:csharp/nameofvirtualincons
Open

C#: Allow virtual member access in nameof expressions in cs/virtual-call-in-constructor.#22205
michaelnebel wants to merge 5 commits into
github:mainfrom
michaelnebel:csharp/nameofvirtualincons

Conversation

@michaelnebel

@michaelnebel michaelnebel commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

DCA looks good.

  • Performance is un-affected.
  • A false positive has been removed in dotnet/efcore.

@github-actions github-actions Bot added the C# label Jul 16, 2026
@michaelnebel michaelnebel force-pushed the csharp/nameofvirtualincons branch from c7e0d75 to b52ea33 Compare July 16, 2026 07:58
@michaelnebel michaelnebel marked this pull request as ready for review July 16, 2026 10:29
@michaelnebel michaelnebel requested a review from a team as a code owner July 16, 2026 10:29
Copilot AI review requested due to automatic review settings July 16, 2026 10:29

Copilot AI 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

This PR refines the C# query cs/virtual-call-in-constructor to avoid reporting virtual-member references that occur inside nameof(...) expressions, eliminating a known false positive (notably in dotnet/efcore) while keeping the query’s intent focused on actual virtual calls/accesses that can dispatch at runtime.

Changes:

  • Update the query to exclude virtual member accesses that are part of a nameof argument expression.
  • Extend the query test case to include nameof usage over virtual members (including a chained member access case).
  • Add a change note and refresh the .expected test output accordingly.
Show a summary per file
File Description
csharp/ql/src/Bad Practices/VirtualCallInConstructorOrDestructor.ql Excludes virtual member access within nameof(...) from being reported by the query.
csharp/ql/test/query-tests/Bad Practices/VirtualCallInConstructorOrDestructor/VirtualCallInConstructorOrDestructor.cs Adds nameof examples that should not produce alerts, including a chained access case.
csharp/ql/test/query-tests/Bad Practices/VirtualCallInConstructorOrDestructor/VirtualCallInConstructorOrDestructor.expected Updates expected results to match the refined query behavior and shifted test line numbers.
csharp/ql/src/change-notes/2026-07-16-virtual-call-in-constructor.md Documents the behavioral improvement as a minor analysis change.

Review details

  • Files reviewed: 4/4 changed files
  • Comments generated: 0
  • Review effort level: Low

Sign up for free to 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.

2 participants