Wrong ToString hint

   see this: (using latest EAP build)
http://img135.imageshack.us/img135/1466/unledes.png

For example Sharepoint class SPFieldLookupValue returns null in ToString, when created with default constructor SPFieldLookupValue()

4 comments
Comment actions Permalink

Hello Martin,

At the moment 'ToString' method is annotated with NotNull attribute because
.ToString() is never expected to return null in .NET Framework. That's why
you get 'Expression is always false' warning as well as 'Possible assignment
to an entity marked with NotNull' on 'return null;' statement. You can avoid
this behavior by removing the annotation for 'ToString' from \ExternalAnnotations\mscorlib\mscorlib.4.0.0.0.Nullness.Generated.xml
(do not forget to restart all instance of ReSharper after doing this). Personally,
I think returning null from ToString is a very bad practice. Thank you!

Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

see this: (using latest EAP build)
Image:http://img135.imageshack.us/img135/1466/unledes.png
For example Sharepoint class SPFieldLookupValue returns null in
ToString, when created with default constructor SPFieldLookupValue()

---
Original message URL:
http://devnet.jetbrains.net/message/5304425#5304425




0
Comment actions Permalink

I agree that returning null in ToString is very bad idea (I don't do that in my code :) ), but unfortunately it is happening at least in one Micorosoft SharePoint core classes. Personally I would preffer accurate hint in this case instead of "expression always returns false" - The NotNull Attribute is only a hint, it's not guaranteed by compiler or languge itself, that all methods with such attributes could not return null value.

0
Comment actions Permalink

This is a bug in ReSharper that persists to the current version.

It is entirely expected that ToString() overrides can return null sometimes -- it's very common to override them to return the value of some member field or property, which might also be nullable.

Indicating that code intended to deal with this is somehow redundant and can be removed is incorrect and error-prone.

This annotation needs to be removed for all installs.

0
Comment actions Permalink

Hello Gavin,

 

Thank you for the feedback.

As MS officially made ToString nullable - https://github.com/dotnet/coreclr/pull/23510, we'll change the annotation for the returnable type of  object.ToString() from NotNull to CanBeNull.

Thank you.

0

Please sign in to leave a comment.