fix(mutating): avoid CS0136 from recursive/var pattern designations#3602
Open
slang25 wants to merge 4 commits into
Open
fix(mutating): avoid CS0136 from recursive/var pattern designations#3602slang25 wants to merge 4 commits into
slang25 wants to merge 4 commits into
Conversation
Mutating an expression that introduces a variable via a recursive or var
pattern designation (e.g. `s is { Length: > 0 } region`) at expression
level produces a ternary that duplicates the declaration across branches,
triggering CS0136.
- Teach ContainsDeclarations to detect SingleVariableDesignation when
its parent is a RecursivePatternSyntax/VarPatternSyntax, leaving other
designations (out var, tuple deconstruction, list patterns) untouched.
- In MutationStore.Inject, when the source expression contains a
declaration, skip ternary placement and forward the pending mutations
to the enclosing block so they can be controlled by an if-statement.
Fixes stryker-mutator#3597
4 tasks
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Prevents C# compiler error CS0136 when mutating expressions that introduce variables via pattern designations by improving declaration detection and avoiding expression-level ternary placement for such cases.
Changes:
- Extend
ContainsDeclarationsto detectSingleVariableDesignationfor recursive/var patterns. - Skip expression-level injection in
MutationStore.Injectwhen the source expression contains declarations (to avoid duplicated declarations in ternary branches). - Add unit tests covering pattern designation declaration detection.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/Stryker.Core/Stryker.Core/Mutants/MutationStore.cs | Avoids ternary-based expression injection when declarations are present to prevent CS0136. |
| src/Stryker.Core/Stryker.Core/Helpers/RoslynHelper.cs | Enhances declaration detection for recursive/var pattern designations. |
| src/Stryker.Core/Stryker.Core.UnitTest/Helpers/RoslynHelperTests.cs | Adds tests validating ContainsDeclarations behavior for patterns and non-declaration expressions. |
- Extract IsRecursiveOrVarPatternDesignation helper from ContainsDeclarations. - Clarify MutationStore.Inject comment: pending store is left intact so Leave() forwards mutations to an enclosing level. - Add negative test cases for recursive/var patterns without a SingleVariableDesignation. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Comment on lines
+12
to
+16
| [TestMethod] | ||
| [DataRow("s is { Length: > 0 } region")] | ||
| [DataRow("s is var captured")] | ||
| [DataRow("s is int n")] | ||
| public void ContainsDeclarationsShouldDetectPatternDesignations(string expression) |
Comment on lines
+28
to
+36
| [TestMethod] | ||
| [DataRow("s.Length > 0")] | ||
| [DataRow("s is [_, _]")] | ||
| [DataRow("(a, b)")] | ||
| // Recursive / var patterns without a SingleVariableDesignation introduce | ||
| // no variable, so they must not trigger ContainsDeclarations. | ||
| [DataRow("s is { Length: > 0 }")] | ||
| [DataRow("s is var _")] | ||
| public void ContainsDeclarationsShouldReturnFalseWithoutDeclarations(string expression) |
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.
Summary
s is { Length: > 0 } region) at expression level produced a ternary that duplicated the declaration across branches, triggering CS0136.ContainsDeclarationsto detectSingleVariableDesignationwhen its parent is aRecursivePatternSyntax/VarPatternSyntax. The scoping leaves other designations (out var, tuple deconstruction, list patterns) at their previous expression-level behavior.MutationStore.Inject, when the source expression contains a declaration, skip ternary placement and forward the pending mutations to the enclosing block so they can be controlled by an if-statement.Fixes #3597. Split out from #3594.
Test plan
RoslynHelperTestscover recursive/var/declaration patternsMutatorsandMutantstest suites still passs is { Length: > 0 } regionno longer produces CS0136 mutants