Roslyn: Simplifying default expression changes meaning of code

Created on 6 Sep 2017  路  16Comments  路  Source: dotnet/roslyn

Version Used: Visual Studio Community 2017, C# 7.1

Steps to Reproduce:

Type the following code:

public bool? IsStringCorrect(string expected, string actual) => expected == null ? default(bool?) : expected == actual;

The IDE marks it with the message IDE0034: 'default' expression can be simplified.
When applying the suggested correction the code is this:

public bool? IsStringCorrect(string expected, string actual) => expected == null ? default : expected == actual;

Expected Behavior:

Either the first example should not trigger IDE0034 or the second example (when the suggested correction is applied) should behave the same.

Actual Behavior:

Both examples behave differently. When expected is null the first example returns null and the second example returns false

Area-IDE Bug Resolution-Fixed

Most helpful comment

@sharwell I think that was fixed in PR #20856
I can't reproduce it on VS 2017 15.3.3

All 16 comments

FYI @CyrusNajmabadi @Pilchie

@zaytsev-victor this seems up your alley if you are interested :smile:

@sharwell I think that was fixed in PR #20856
I can't reproduce it on VS 2017 15.3.3

@WilhelmZapiain Can you check which version of Visual Studio 2017 you are using?

Visual Studio Community 2017 15.3.3
Tested on a brand new Desktop Console app, targeting .NET framework 4.6.2 and building on C# latest minor version.

@jasonmalinowski Do you know when dev15.6 branch became master again? I'm trying to figure out if the assumed fix for this issue went into 15.3 or 15.5.

@sharwell If you're trying to figure out which release this went into, better to use git tag --list --contains <sha1> than trying to figure it out via branches.

Using a preview of 15.4 and this has indeed been fixed.

@WilhelmZapiain so far we've been unable to reproduce this in 15.3.4 and 15.4 Preview. If you are still encountering this, can you post a more complete example, e.g. a complete file which demonstrates this issue?

I am still seeing it and just upgraded Visual Studio.

Using 15.3.5
Steps to reproduce:

  1. Create new Console App C#
  2. Go to Project Properties/Build/Advanced and change Language version to C# latest minor version (latest)
  3. Typing the following code shows up IDE0034

```C#
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
}

    public static bool? IsStringCorrect(string expected, string actual) => expected == null ? default(bool?) : expected == actual;
}

}
```

@WilhelmZapiain Thank you, that was sufficient for me to reproduce the issue in both 15.3.4 and 15.4 Preview 2.

Can't reproduce on the latest master.
img

@zaytsev-victor Awesome 馃槅

@WilhelmZapiain Let's look at this again after 15.5 Preview 1 comes out. If it's fixed in master today then we can expect it will already be fixed for you starting with that preview.

For now I'm marking this as blocked on the release of 15.5 Preview 1, unless someone feels like going through code to figure out exactly where it got fixed. 馃榿

@sharwell
Please read this too: #22484

Thanks for reporting this issue.
It was fixed by this 15.5 PR: https://github.com/dotnet/roslyn/pull/20856
Closing

Was this page helpful?
0 / 5 - 0 ratings