Fody / NullGuard

Adds null argument checks to an assembly
MIT License
686 stars 55 forks source link

Option to generate NotNull attributes for implicitly nullable items #54

Closed fschmied closed 6 years ago

fschmied commented 9 years ago

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
drauch commented 9 years ago

Would love that as well :+1:

ulrichb commented 9 years ago

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))]

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.

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?

distantcam commented 9 years ago

@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.

ulrichb commented 9 years ago

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.

tpluscode commented 7 years ago

@distantcam what if NullGuard pulled JetBrainsAnnotations.Fody as dependency to ensure that JetBrains.Annotations is referenced?

SimonCropp commented 6 years ago

Will close this for now. Pending a non wip pr being submitted