0

What is the reason why this static analysis attribute is not allowed to be declared on constructors? I would like to annotate that a constructor throws an exception when a null argument is passed.

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
internal sealed class ContractAnnotationAttribute : Attribute
{
    //...
}

PS: I am asking in a hope that there is a some sort of general principle for this and not just their personal decision.

Den
  • 1,827
  • 3
  • 25
  • 46

1 Answers1

2

There's an open ticket to add support here which you can vote on, and track: http://youtrack.jetbrains.com/issue/RSRP-401969

However, the only annotation you can apply is that a parameter must be not-null. You can achieve pretty much the same result using the NotNull annotation on those parameters.

citizenmatt
  • 18,085
  • 5
  • 55
  • 60
  • I hope they will implement it eventually. Feels incomplete. – Den Jun 02 '14 at 09:32
  • It's not exactly the same result. There's no way with NotNull to explicitly specify how someone passing null anyway would actually be handled. On the other hand, [ContractAnnotation("foo:null=>stop", true)] makes it much more clear that we're going to be seeing an exception. – KatDevsGames Jan 31 '16 at 19:31
  • Good point. You can try marking the constructor with `[AssertionMethod]`, and then marking each parameter with e.g. `[AssertionCondition(AssertionConditionType.IS_NOT_NULL)]` or `[AssertionCondition(AssertionConditionType.IS_FALSE)]`. These attributes are technically deprecated in favour of `ContractAnnotation`, but are still supported. – citizenmatt Feb 01 '16 at 09:30