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

Declare as nullable code fix for CS8600 breaks when casts are involved #1392

Closed
jroessel opened this issue Feb 13, 2024 · 1 comment · Fixed by #1393
Closed

Declare as nullable code fix for CS8600 breaks when casts are involved #1392

jroessel opened this issue Feb 13, 2024 · 1 comment · Fixed by #1393

Comments

@jroessel
Copy link
Contributor

Product and Version Used:

Steps to Reproduce:

The following code reports CS8600 because we cast a nullable type to a non-nullable type as part of a variable declaration.

    private object? Get() => null;

    internal void Test() {
      var s = (string) Get(); // (1)
      string s2 = (string) Get(); // (2)
    }

Roslynator's code fix for CS8600 erroneously assumes the declaration type to be the problem, when it's actually the cast. So we don't get rid of the warning and in case of var actually introduce broken code.

Actual Behavior:

      var? s = (string) Get(); // (1)
      string? s2 = (string) Get(); // (2)

Expected Behavior:

      var s = (string?) Get(); // (1)
      string? s2 = (string?) Get(); // (2)

(Although in case of (2) the declaration could be adjusted by another code fix for the same diagnostic, so at least iteratively applying fixes will arrive at the same result.)

@jroessel
Copy link
Contributor Author

Currently attempting a fix.

jroessel added a commit to jroessel/roslynator that referenced this issue Feb 13, 2024
…ions

Casting a nullable type to a non-nullable type within a variable declaration no longer ignores the cast type and also no longer changes `var` to `var?`.

Fixes dotnet#1392.
jroessel added a commit to jroessel/roslynator that referenced this issue Feb 13, 2024
…ions

Casting a nullable type to a non-nullable type within a variable declaration no longer ignores the cast type and also no longer changes `var` to `var?`.

Fixes dotnet#1392.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants