Skip to content

Incorrect IDE0004: Remove unnecessary cast #58095

@JKamsker

Description

@JKamsker

I am currently unsure if its really a roslyn bug or a c# bug or both.

Version Used:

Microsoft Visual Studio Enterprise 2022
Version 17.0.0
VisualStudio.17.Release/17.0.0+31903.59
Microsoft .NET Framework
Version 4.8.04084

dotnet 6 / C# 10

Steps to Reproduce:

Code:

var (isEdge, moreOnes) = (true, false);

char? expected_a = isEdge ? null : moreOnes ? '1' : '0';
char? expected_b = isEdge ? null : moreOnes ? '0' : '1';

(char? expected_a_01, char? expected_b_01) = isEdge ? default : moreOnes ? ((char?)'1', (char?)'0') : ('0', '1');
(char? expected_a_02, char? expected_b_02) = isEdge ? default : moreOnes ? ('1', '0') : ('0', '1');

Debug.Assert(expected_a == expected_a_01 && expected_a == expected_a_02);
Debug.Assert(expected_b == expected_b_01 && expected_b == expected_b_02);

Expected Behavior:
expected_a, expected_a_01 and expected_a_02 should be equal. (Namely, null)
The analyzer tells me, that both casts are redundant.

Actual Behavior:
expected_a_02 and expected_b_02 both have the value 0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-IDEuntriagedIssues and PRs which have not yet been triaged by a lead

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions