Skip to content

Don't treat typeof(T) as a constant as it depends on parameters.#906

Merged
mikkelbu merged 2 commits intomasterfrom
Issue896_typeof_generic
Aug 9, 2025
Merged

Don't treat typeof(T) as a constant as it depends on parameters.#906
mikkelbu merged 2 commits intomasterfrom
Issue896_typeof_generic

Conversation

@manfred-brands
Copy link
Copy Markdown
Member

Fixes #896

@manfred-brands manfred-brands requested a review from Copilot August 9, 2025 03:46
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 fixes issue #896 by preventing the analyzer from treating typeof(T) expressions as constants when T is a type parameter. The analyzer was incorrectly flagging typeof(T) as a constant value, but since it depends on generic type parameters, its value is determined at runtime and should not be considered constant.

  • Added special case handling for typeof operations with type parameters
  • Added test coverage to verify the fix works correctly

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
ConstActualValueUsageAnalyzer.cs Added logic to exclude typeof(T) operations from constant value detection when T is a type parameter
ConstActualValueUsageAnalyzerTests.cs Added test case to verify that typeof(T) with generic type parameters is not flagged as a constant

Comment thread src/nunit.analyzers/ConstActualValueUsage/ConstActualValueUsageAnalyzer.cs Outdated
@manfred-brands manfred-brands force-pushed the Issue896_typeof_generic branch from 6eec4f6 to fa4f130 Compare August 9, 2025 03:48
@manfred-brands manfred-brands requested a review from mikkelbu August 9, 2025 03:49
Copy link
Copy Markdown
Member

@mikkelbu mikkelbu left a comment

Choose a reason for hiding this comment

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

LGTM

@mikkelbu mikkelbu merged commit a9232d8 into master Aug 9, 2025
6 checks passed
@mikkelbu mikkelbu deleted the Issue896_typeof_generic branch August 9, 2025 07:30
@mikkelbu mikkelbu added this to the Release 4.10 milestone Aug 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NUnit2007 shouldn't trigger for generic types e.g. typeof(T)

3 participants