-
-
Notifications
You must be signed in to change notification settings - Fork 794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tag Mock.Object as NotNull (JetBrains.Annotations) #1043
Comments
Hi @JeromeJ, I think it would be sensible to not apply these attributes in single locations—ideally, we'd enable I've previously looked at starting work on nullable reference type annotations, what stopped me is that we currently have a ton of |
Oyeah I agree it would probably be better to not focus it on a single point. Yeah... That's why I was mentioning JetBrains.Annotations for all of us who aren't ready to switch over to C# 8 before a (sad) long while but I also have high hopes for it but I do enjoy the safety that it can already provide now (mostly by using pessimistic config to emulate c#8's feature as close as possible). |
Using ReSharper there is a shortcut to add NotNull and CanBeNull using "!" and "?".If retyping "!" on a parameter marked as not null it auto adds `if(foo == null) throw new ArgumentNotNull(nameof(foo));` which is sorta equivalent to your Debug.Assert afaik but easy to add.
|
In my personal opinion, It's pretty clear to me that when converting to C# nullable reference types, all Not a particularly difficult task, but one that takes patience and a good bit of time, as one has to go over the whole library. |
@JeromeJ, I've been taking a brief look... it appears like one could prepare external annotations as an XML document and submit it to JetBrains/ExternalAnnotations. That would seem like a good solution: you get the code analysis result that you need, without us bringing in another dependency / tool. |
Indeed! This could be a nice solution! (even though those XML are painful to write..) I'll look into it! Dependency-wise, if I'm not mistaken, while you could also use the nugget (and thus creating a dependency), you, alternatively, could get away with just using a good ol' c/c'ed .cs file (KISS) containing all the annotations as it works on a namespace base. |
As far as I understand it, including the source and conditionally removing it during compilation (using While that may work in theory I think it would be neater to just ship the annotations out of band (as XML)... not everyone will need them but those using JetBrains tools would, I suppose, automatically get a copy. |
I am going to close this, as I still believe external annotations as mentioned here are the way to go, since that means we don't make the Moq NuGet package dependent on JetBrains.Annotations. |
From static analysis I made myself, I don't think Mock.Object can ever be null but, since I set up my ReSharper as pessimistic, it raises it as a false positive of "Possible NullReferenceException" since it hasn't explicitly tagged as
[NotNull]
.Doing so would allow users to not have to check "to be sure" for things that it's actually not required.
(Actually it's the same with many methods, such as Setup, which always return a non null object but aren't tagged as such)
The text was updated successfully, but these errors were encountered: