I often use following construct to late/dynamic create complex objects - the properties are never null. Of course, the notification for CRR0027 is correct, but I need the possibility to mark the property as "NotNull" to overcome the need to always check for NullReferenceException like in "Sub Bar()". Please consider that normally there are multiple properties like that!
Visual BasicPublic Class Class1
Private _propA As ObjectModel.Collection(Of String)
Property PropA As ObjectModel.Collection(Of String)
Get
If _propA Is Nothing Then
_propA = New ObjectModel.Collection(Of String)
'Do some other stuff with _prop
End If
Return _propA
End Get
Set(value As ObjectModel.Collection(Of String))
_propA = value
End Set
End Property
Private _propB As ObjectModel.Collection(Of Class1)
Property PropB As ObjectModel.Collection(Of Class1)
Get
If _propB Is Nothing Then
_propB = New ObjectModel.Collection(Of Class1)
End If
Return _propB
End Get
Set(value As ObjectModel.Collection(Of Class1))
_propB = value
End Set
End Property
Sub Foo()
'CRR0027 notification
If PropA.Contains("Hello") Then
'Do something else ...
'CRR0027 notification
PropB.Add(New Class1)
End If
' Do other things ...
' Access PropA again
PropA.Add("New Value")
'...
End Sub
Sub Bar()
'This works, but every accessed property must be checked beforehand
If (PropA IsNot Nothing) AndAlso (PropB IsNot Nothing) Then
If PropA.Contains("Hello") Then
'Do something else ...
PropB.Add(New Class1)
End If
' Do other things ...
' Access PropA again
PropA.Add("New Value")
'...
End If
End Sub
End Class
Hello Robert,
Thank you for providing the code samples.
At present, we do not support any ability to mark that property as "cannot be null".
We support the standard "SuppressMessage" attribute to suppress the diagnostic for specific method, you can use it for the "Foo" method in your code:
<CodeAnalysis.SuppressMessage("Expressions", "CRR0027")>
But it seems inconvenient, because it should be specified for every method where the "PropA" property is used.
We believe that your request makes sense, so we have discussed two options and would like to hear your opinion, how it will be convenient for you:
- create an ability to mark the "PropA" property with the "SuppressMessage" attribute and check that such properties cannot be null in our "Possible NullReference" diagnostic;
- create an assembly with some attributes such as "NotNull", "CanBeNull" and so on. In this case, you have to add the reference to this assembly in you project.
We are looking forward to your response.
Hello Pavel,
it's difficult to say what's the best way. When using a referenced assembly, this assembly should not be needed to be distributed - and also not to be in the build-out-folder. (also no IL-instructions for the annotations).
Before CRR, I used another product. As this other product focused more an more only on C# and ignoring VB.net, I gave CRR a chance - because it uses Roslyn what let me think, that CRR will support newer VB.net versions right when they are released.
The other product had a good grown static analyzer - but meanwhile for VB.net with many (!) annoying false positives.
To come to an end: Using Suppress-Message or Annotations specified in an assembly depends on in which direction you want to develop CRR Static Analyzer, and how fine graduate should a developer mark code-segments.
Hello Robert,
Thank you for the clarification and sharing your opinion with us.
I have added your request to our product backlog, and we will take it into account when considering improvements for the "Possible NullReferenceException" diagnostic.
We notify you as soon as we make any progress with this.
We are happy to hear that you chose CodeRush for Roslyn, so if you miss any other features, feel free to contact us, and let us know.
Hi Pavel,
sorry for being so annoying, but for me it's now hardly possible to give you more feedback to "Possible NullReferenceException", when marking such properties is not available. Today I get hundreds of notifications because of this "issue", so it's like finding a needle in a haystack to recognize other issues for "Possible NullReferenceException".
Have you any estimates on when this "issue" gets resolved?
Hello Robert,
Thank you for the update.
We appreciate you giving feedback about this feature.
We understand that it shows you a lot of false positives and we definitely agree that this can be annoying.
At present we consider the following improvements that should allow us to get rid of such false positives:
- Analyze the property/method body and make sure that its return value cannot be null.
- Support Contract.Ensures as a way to mark a property as "Cannot be null". In this case the property will look like this:
Property PropA As String Get Contract.Ensures(Contract.Result(Of String) IsNot Nothing) If _PropA Is Nothing Then _PropA = "Hello" End If Return _PropA End Get Set(value As String) _PropA = value End Set End Property
- Support [Null / NotNull] attributes to mark properties / methods (this way will force adding a reference to an assembly that contains these attributes to your project).
Since implementing any of these ways is a quite resource-consuming task, I cannot give you an exact time frame.
Yet, we will definitely let you know when we have any results.
If this feature causes more inconvenience at the moment rather than brings good, let me advise you turn it off.
To do this, uncheck the "Search for possible System.NullReferenceException exceptions" item on the "Editor \ All Languages \ Static Code Analysis" option page.
Hello Pavel,
have you stopped checking "Possible Null ReferenceException" at all for properties?
Public Class Class1 Private _propA As String Public Property PropA As String Get If _propA Is Nothing Then _propA = "Hello" End If Return _propA End Get Set(value As String) _propA = value End Set End Property Public Function PropALength() As Integer 'No CRR0027 (in this case this should be OK) Return PropA.Length End Function Public Property PropB As String Public Function PropBLength() As Integer 'No CRR0027 (but in this case it should notify!) Return PropB.Length End Function End Class
Robert,
I have checked this and it appears that this indeed was disabled, since such checks lead to performance issues and high background CPU usage.
We are currently working on improving the performance of the null reference exception diagnostic analysis and will enable properties checking in future updates.
We will update this ticket as soon as we make any progress in this regard.