Incorrect IDE0004: Remove unnecessary cast
See original GitHub issueI 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
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
IDE0004: Remove unnecessary cast - .NET
Overview. This rule flags unnecessary type casts. A cast expression is unnecessary if the code semantics would be identical with or without it....
Read more >"Redundant cast" (ide0004) : RSRP-486421
ReSharper will blur out both double type casts and suggest that they are redundant. Removing one of the type casts will mark it...
Read more >Visual Studio 2015: Invalid "Cast is redundant" warning in ...
It insists that the (int) "cast is redundant", and suggests as a potential fix to "Remove Unnecessary Cast", which of course is wrong,...
Read more >Microsoft .NET Code Analysis: Remove Unnecessary Casts
Code correctness: Removing unnecessary casts helps ensure that the code accurately reflects the intended logic. By eliminating unnecessary type ...
Read more >Remove Redundant Type Casts
The rule first searches the next type-cast operation. If the expression is casted to a type which already is exactly the type of...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@JKamsker Correct. As i mentioned, this is fixed in 17.1 (not 17.0) and i have a PR out showing that this no longer reproes to ensure this doesn’t regress. Thanks!
Not a problem at all. Nothing to be sorry about. 😃