Skip to content

Conversation

@michaelnebel
Copy link
Contributor

@michaelnebel michaelnebel commented Mar 11, 2025

In this PR we start to take conditions into account for matching in switch statements and expressions. That is, we allow match all (discard) patterns, if there is a condition. Furthermore, we also apply the logic to switch/case statements.

Example

public string M(int i)
{
    return i switch
    {
        _ when i % 2 == 0 => "even", // We used to flag this in cs/constant-condition
        _ => "odd"
    };
}

This reduces the number of false positives.

DCA looks good.

@michaelnebel michaelnebel marked this pull request as ready for review March 11, 2025 15:22
Copilot AI review requested due to automatic review settings March 11, 2025 15:22
@michaelnebel michaelnebel requested a review from a team as a code owner March 11, 2025 15:22
Copy link
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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link
Contributor

@hvitved hvitved left a comment

Choose a reason for hiding this comment

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

A small stylistic suggestion, otherwise LGTM.

Co-authored-by: Tom Hvitved <hvitved@github.com>
@michaelnebel michaelnebel requested a review from hvitved March 12, 2025 12:57
@michaelnebel michaelnebel merged commit e79cb44 into github:main Mar 12, 2025
21 checks passed
@michaelnebel michaelnebel deleted the csharp/constant-condition branch March 12, 2025 13:42
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