Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code fix for adding null-check in a nullable context #39996

Open
marinasundstrom opened this issue Nov 24, 2019 · 1 comment
Open

Code fix for adding null-check in a nullable context #39996

marinasundstrom opened this issue Nov 24, 2019 · 1 comment
Labels
Area-IDE Feature Request Need Design Review The end user experience design needs to be reviewed and approved.
Milestone

Comments

@marinasundstrom
Copy link

marinasundstrom commented Nov 24, 2019

Something I miss in a context where variables are nullable is a fix to either add an if-statement or a .? (if applicable) when null could be expected.

It would minimize the number of key strokes.

The exact behavior should be discussed. But preferably, it should be a suggestion.

@sharwell sharwell added Feature Request New Language Feature - Nullable Reference Types Nullable Reference Types Need Design Review The end user experience design needs to be reviewed and approved. labels Nov 25, 2019
@sharwell sharwell added this to In Queue in IDE: Design review via automation Nov 25, 2019
@sharwell sharwell added this to the Backlog milestone Nov 25, 2019
@sharwell sharwell moved this from In Queue to On deck in IDE: Design review Nov 25, 2019
@marinasundstrom
Copy link
Author

marinasundstrom commented Nov 27, 2019

I have identified 2 different fixes:

  1. Put an if (x != null) statement at a particular occurrence.
  2. Surround the rest of the code block. Find the place in the current scope where the nullable variable is first dereferenced. Put an if (x != null) statement around the remainder of the code in that scope.

The type:

public class Foo 
{
    public Bar? Bar { get; set;  }
}

Before fix:

var bar = foo.Bar;

if (bar.X == 0) 
{
    Console.WriteLine(bar);
}

After fix:

var bar = foo.Bar;

if (bar != null) 
{
    if (bar.X == 0) 
    {
        Console.WriteLine(bar);
    }
}

@sharwell sharwell moved this from On deck to Need Update in IDE: Design review Jan 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE Feature Request Need Design Review The end user experience design needs to be reviewed and approved.
Projects
Status: Need Update
IDE: Design review
  
Need Update
Development

No branches or pull requests

4 participants