CS8604 False-negative in .Net Standard project vs .Net Core
See original GitHub issueDescribe the bug
In a .Net Standard project, trying to use a Stream?
instead of a Stream
raises a CS8604 warning. But not when using a Stream?
to initialize a StreamReader
.
In a .Net Standard project, both situations raise a CS8604 warning.
Steps To Reproduce
Here is the short class that I am using, with <Nullable>enable</Nullable>
in .NetStandard 2.1 project and .NetCoreApp 3.1 project.
public class Test
{
public async Task<Stream?> GetFile(string file)
{
Stream? stream = null;
if (File.Exists(file))
{
stream = File.OpenRead(file);
}
return await Task.FromResult(stream);
}
public async Task TestStreamReader(string file)
{
using(Stream stream = await GetFile(file))
{
// Do stuff...
}
using(StreamReader reader = new StreamReader(await GetFile(file)))
{
// Do stuff...
}
}
}
Expected behavior
CS8604 for both using Stream
and StreamReader
in .Net Standard and .Net Core
Actual behavior
CS8604 for Stream
in .Net Standard and for Stream
and StreamReader
in .Net Core
Analyzer
Package: Microsoft.CodeAnalysis.FxCopAnalyzers
Version: v3.3.0 (Latest)
Diagnostic ID: CA1716
Additional context
I understand this might be “intentional” as it should only work properly in .Net Core App if I understand this correctly but I feel like the fact that this is “half” working in .Net Standard can make it confusing.
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (4 by maintainers)
Yes, but there is also a project that lets you copy annotations from one target framework to another.
Correct, there are no plans that I am aware of to introduce nullability annotations to netstandard2.1.