question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incorrect IDE0004: Remove unnecessary cast

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
CyrusNajmabadicommented, Dec 7, 2021

@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!

0reactions
CyrusNajmabadicommented, Dec 8, 2021

Not a problem at all. Nothing to be sorry about. 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found