Bug Report T578383
Visible to All Users

Static Code Analysis - The "Possible NullReferenceException" diagnostic - Mark/Attribute members/parameters as NotNull

created 7 years ago (modified 7 years ago)

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 Basic
Public 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
Show previous comments (4)
DevExpress Support Team 7 years ago

    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:

    Visual Basic
    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?

      Visual Basic
      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
      DevExpress Support Team 7 years ago

        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.

        Answers approved by DevExpress Support

        created 6 years ago

        We have fixed the issue described in this ticket and will include the fix in our next maintenance update. To apply this solution before the official update, request a hotfix by clicking the corresponding link for product versions you require.

        Note: Hotfixes may be unavailable for beta versions and updates that are about to be released.

        Additional information:

        Hi Robert,

        In the CodeRush version 18.1.11, we have added support for the NotNull attribute. CodeRush checks only the attribute name excluding the namespace. So, you can declare this attribute in your code in any way convenient for you and mark required members with this attribute. We are looking forward to your feedback.

          Show previous comments (5)
          DevExpress Support Team 6 years ago

            Hi Robert,

            Thanks for informing of your results and providing the code sample. We have reproduced this incorrect behavior in "Skeptical" mode in Visual Basic. We have corrected it and prepared a build with this changes: DevExpress.CodeRush.Roslyn-18.1.11.18267.vsix.

            Also, the correct attribute to mark members is "NotNull". "IsNotNull" was the typo in descriptions on options page and it is corrected as well. Thank you for your observation.

              Hi Alexander,

              it works like desired. Cool! <thumbs up>

              DevExpress Support Team 6 years ago

                Thank you for informing us of your results, Robert. We are happy to hear that now the analysis works correctly on your side.

                Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

                Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.