Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New C#7 pattern matching and reachability

Tags:

c#

This is based on this question, which seems to me a language glitch or a missing warning in the compiler. My reasoning is that is will always return true in expressions of the following type:

int i;
if (i is var j) ....

Taking it a step further, consider the following code:

int i;
if (i is int) .... //Compiler warning: The given expression is always
                   //of the provided ('int') type.

But, same as with var, if you do:

if (i is int j) ...
else ...

You won't get any warning (neither given expression is always true or unreachable code detected in the else clause) to what, essentially, is the same behavior.

This makes me consider if maybe my initial reasoning is wrong. Is there a some obscure use/corner case I'm missing where this really makes a difference? And if not, is the missing warning and the inconsistent behavior an oversight (or even bug) in the compiler?

like image 539
InBetween Avatar asked Aug 25 '17 14:08

InBetween


People also ask

How much is the latest Benz 2022?

The 2022 C-Class sedan starts at $43,550. The midtier Exclusive model starts at $45,800, and the top Pinnacle trim has a starting MSRP of $47,500. All-wheel drive is available in all models for an additional $2,000.

Is the C class a good car?

Mercedes-Benz's entry-level sports sedan has been thoroughly redesigned for the 2022 model year but the C-class sticks with its winning formula of luxury and prestige at an affordable price. The modernization gives the C-class what it needs to better battle rivals such as the Audi A4, BMW 3-series, and Genesis G70.

Is Benz C180 a good car?

A super duper car with full of comfort with excellent steering. The Mercedes benz ranks 4 out of 20 in luxury cars.

Is there a C180 AMG?

Mercedes-Benz C Class C180 AMG Line 4dr Auto Reviews 2022 | Top Gear.


1 Answers

I believe this is by design, referencing the roslyn source code, starting here: Binder_Operators, Line 2848

Because the heuristic presented here is used to change codegen, it must be conservative. It is acceptable for us to fail to report a warning in cases where humans could logically deduce that the operator will always return [true]. It is not acceptable to inaccurately warn that the operator will always return [true] if there are cases where it might [fail].

like image 182
djones Avatar answered Sep 29 '22 17:09

djones