Option to generate NotNull attributes for implicitly nullable items
See original GitHub issueIt 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:
- Created 8 years ago
- Comments:6 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
[NotNull]
attribute for return values on the method instead of a return value attribute. This means thatAttributeTargets
on the[NotNull]
attribute (make it dependent onAttributeTargets.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.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?
Would love that as well 👍