Closed fschmied closed 6 years ago
Would love that as well :+1:
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))]
[NotNull]
attribute for return values on the method instead of a return value attribute. This means that
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.
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?
@ulrichb Sorry to get back to you so late.
Yes I would accept a PR for option 2. That seems the best approach instead of trying to add an external reference to a library.
Hi @distantcam,
OK. Many thanks!
Just to make it sure: In option 1, not NullGuard, but the developers of consuming projects would have to reference JetBrains.Annotations
or copy the attribute source code into an own library (as described in the blog post linked above). Afterwards, they could use [assembly: AnnotateNullChecksWith(typeof(NotNullAttribute))]
with the referenced NotNullAttribute
.
@distantcam what if NullGuard pulled JetBrainsAnnotations.Fody
as dependency to ensure that JetBrains.Annotations
is referenced?
Will close this for now. Pending a non wip pr being submitted
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 againstnull
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:
Representation of woven library code:
Library usage: