question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Option to generate NotNull attributes for implicitly nullable items

See original GitHub issue

It would be great if NullGuard had an option of adding NotNull attributes (type to be configured by the user) to those items (parameters, method return values, property values) it guards against null values. This would enable library and framework authors to auto-generate nullability annotations (consistent with the actual null checks) to be interpreted by ReSharper and similar tools.

E.g., library source code:

public void Foo (string s1, [CanBeNull] string s2)
{
  // code relying on s1 not being null, whereas s2 can be null
}

Representation of woven library code:

public void Foo ([NotNull] string s1, [CanBeNull] string s2)
{
  if (s1 == null)
    throw new ArgumentException ("NullGuard...");

  // code relying on s1 not being null, whereas s2 can be null
}

Library usage:

Foo(null, "x"); // ReSharper warning: Possible 'null' assignment to entity marked with 'NotNull' attribute

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ulrichbcommented, Aug 24, 2015

Yes, it would be great if consumers of a NullGuard-rewritten library which use ReSharper get these nullability annotations “for free”.

Regarding the implementation, I see two possibilities to get references to the JetBrains annotation attributes during the weaving process.

1. Reference the attributes in a configuration assembly attribute.

[assembly: AnnotateNullChecksWith(typeof(JetBrains.Annotations.NotNullAttribute))]
  • PRO: Would not depend on JetBrains annotations (arbitrary attributes could be injected).
  • CON: Here the problem is that ReSharper expects the [NotNull] attribute for return values on the method instead of a return value attribute. This means that
    • a) we would have to implement this feature especially for ReSharper again, or
    • b) to be more generic, we could use a “InjectAttributeOnMethodInsteadOfReturnValue”-flag in the configuration assembly attribute, or
    • c) we could evaluate the AttributeTargets on the [NotNull] attribute (make it dependent on AttributeTargets.ReturnValue)

2. Use internalized JetBrains annotation attributes. (as described in this bog post)

E.g. inject an internal JetBrains.Annotations.NotNullAttribute class type into the rewritten assembly and use this to inject the [NotNull] attributes.

  • PRO: The library-writers doesn’t have to reference (or declare) JetBrains annotations if they doesn’t use ReSharper by themselves. If the annotations would be injected by default (or opt-out) library-consumer, which use ReSharper, would get the annotations and therefore a stronger ReSharper analysis for free. (The library writer would just have to update NullGuard.)
  • CON: Complexity. We would first have to check if in the rewritten assembly already contains a JetBrains.Annotations.NotNullAttribute and, if not, inject one.

I tend to option 2 with opt-out configuration because it shouldn’t be that complicated to implement the check and create a type with one attribute and a default c’tor in Cecil and we would get this zero-configuration benefit for ReSharper users of NullGuard-rewritten assemblies.

@maintainers: Would you accept a PR?

1reaction
drauchcommented, Aug 13, 2015

Would love that as well 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

C#'s can't make `notnull` type nullable
Basically you're asking for something that can't be represented in IL. Nullable value types and nullable reference types are very different ...
Read more >
Nullable reference types
This article provides an overview of nullable reference types. You'll learn how the feature provides safety against null reference ...
Read more >
Resolve nullable warnings
Several compiler warnings indicate code that isn't null-safe. Learn how to address those warnings by making your code more resilient.
Read more >
Code annotation attributes | ReSharper Documentation
Indicates that the value of the marked element could be null sometimes, so checking for null is required before its usage. Example. [CanBeNull] ......
Read more >
Using C# 8 and Nullable Reference Types in .NET Framework
Nullability Attributes · Conditional postconditions: MaybeNotNullWhen(bool), NotNullWhen(bool) · Dependent null-ness: NotNullIfNotNull · Flow: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found