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

EnsureArg.IsNotNull for nullable value type #154

Closed
hannasm opened this issue Mar 6, 2021 · 9 comments
Closed

EnsureArg.IsNotNull for nullable value type #154

hannasm opened this issue Mar 6, 2021 · 9 comments

Comments

@hannasm
Copy link

hannasm commented Mar 6, 2021

Uri? a = null;
EnsureArg.IsNotNull(a);
var x = a.ToString();

On line 3, Visual studio generates a CS8602, dereference of a possibly null reference

@ndrwrbgs
Copy link
Contributor

From dotnet/roslyn-analyzers#2578 it looks like this shouldn't be the case. But I can confirm that in a csproj like so it does (please include in future reports for expediency).

Not seeing much online talking about it, though, seems nullable might not have wide adoption yet?

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Ensure.That" Version="10.0.0" />
  </ItemGroup>

</Project>

@ndrwrbgs
Copy link
Contributor

Brute force testing seems the current SA from roslyn team requires EXPLICITLY the https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.codeanalysis.notnullattribute?view=net-5.0 attribute, without compatibility with the existing R# attributes.

Sadly, the old ValidatedNotNull will still be required for projects using those versions, and NotNull will be required for new projects, while ContractAnnotation is required for CodeContracts based projects. Microsoft has quite the repertoire of static analysis approaches. I apologize in advance for how attribute-y the code is getting @danielwertheim!

It does appear the nice JetBrains team made compat with the C#8 attributes though, so we can simply replace in some places.

@ndrwrbgs
Copy link
Contributor

@hannasm will you be able to confirm the preview package when published?

@hannasm
Copy link
Author

hannasm commented Mar 30, 2021 via email

@danielwertheim
Copy link
Owner

There's now a pre-release out regarding this: https://www.nuget.org/packages/Ensure.That/10.1.0-p1

@danielwertheim
Copy link
Owner

@hannasm did the preview release solve your issue?

@hannasm
Copy link
Author

hannasm commented Apr 3, 2021 via email

@ndrwrbgs
Copy link
Contributor

ndrwrbgs commented Apr 3, 2021 via email

@danielwertheim
Copy link
Owner

Thanks @hannasm for verifying.

@ndrwrbgs Agreed. It's now released as v10.1.0. Thanks for all the work fixing this.

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

No branches or pull requests

3 participants