CA1508 false positive, when unwrapping nullable value-type with `?? default`.
- Dominant language
- C#
- Stars
- 3.2k
- Forks
- 1.3k
- PR merge metrics
- PR metrics pending
Description
### Describe the bug
When unwrapping nullable value-type with `?? default` after null check, it got flagged with CA1508. Although warning statement (_'ni' is never 'null'_) is true, you still need to convert `int?` to `int`. `ni ?? default` produce exactly the same IL as `ni.GetValueOrDefault()` and `ni.GetValueOrDefault()` did not get flagged with that rule.
### To Reproduce
Program.cs:
```csharp
static class Test {
static int DoSomething(int i) => 0;
static int DoSomethingElse() => 0;
static int Test1(int? ni) => ni is { } ? DoSomething(ni.GetValueOrDefault()) : DoSomethingElse();
static int Test2(int? ni) => ni is { } ? DoSomething(ni ?? default) : DoSomethingElse();
}
```
Test.csproj:
```xml
Latest
All
net10.0
enable
```
### Exceptions (if any)
warning CA1508: 'ni' is never 'null'. Remove or refactor the condition(s) to avoid dead code.
### Further technical details
details of dotnet --info
```
.NET SDK:
Version: 10.0.200
Commit: 40cd698e5c
Workload version: 10.0.200-manifests.c7707153
MSBuild version: 18.3.0-release-26119-122+40cd698e5
Runtime Environment:
OS Name: Windows
OS Version: 10.0.26200
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\10.0.200\
.NET workloads installed:
There are no installed workloads to display.
Configured to use workload sets when installing new manifests.
No workload sets are installed. Run "dotnet workload restore" to install a workload set.
Host:
Version: 10.0.4
Architecture: x64
Commit: 80d3e14f5e
.NET SDKs installed:
10.0.200 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 10.0.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 10.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 10.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Other architectures found:
None
Environment variables:
Not set
global.json file:
Not found
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.