dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.04k stars 4.03k forks source link

SuppresNullableWarningExpression on switch expressions' governing expressions should suppress CS8655 #43172

Open TessenR opened 4 years ago

TessenR commented 4 years ago

Version Used:

Branch master (10 Mar 2020)
Latest commit 441c154 by msftbot[bot]:
Merge pull request #42303 from CyrusNajmabadi/useCompoundParens

Support parenthesized expressions in use-compound-operator.

Steps to Reproduce:

Compile the following code:

using System;
#nullable enable
public class C {
    public void M(string? s) {
        _ = s! switch { {} => 1 };

    }
}

Expected Behavior: No warnings. Suppressed expressions are considered not nullable in all other contexts so this switch's input should be considered not nullable

Actual Behavior: warning CS8655: The switch expression does not handle some null inputs (it is not exhaustive). is reported

Notes The current behavior makes it impossible to easily suppress the warning as the following doesn't work either: (s switch { {} => 1 })!

gafter commented 4 years ago

I believe this is by design. {} is considered a "pure" null check. Therefore, using it implies that you expect that the input might be null, and we take that into consideration to consider the input possibly null. If you want to handle all inputs, use a wildcard instead (e.g. var _ or _).