Current filter:
                                You should refresh the page.
                                  • 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!

                                    [VB.NET]
                                    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 all comments
                                • Pavel A (DevExpress) 11.20.2017

                                  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:

                                  [VB.NET]
                                  <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.

                                • Robert 15 11.21.2017

                                  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.

                                • Pavel A (DevExpress) 11.21.2017

                                  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.

                                • Robert 15 12.01.2017

                                  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?

                                • Pavel A (DevExpress) 12.04.2017

                                  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:

                                  [VB.NET]
                                  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.

                                • Robert 15 04.18.2018

                                  Hello Pavel,

                                  have you stopped checking "Possible Null ReferenceException" at all for properties?

                                  [VB.NET]
                                  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
                                • Alex Z (DevExpress) 04.18.2018

                                  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.

                                1 Solution

                                Creation Date Importance Sort by

                                Fixed:


                                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 all comments
                                • Robert 15 09.21.2018

                                  Hi,

                                  at first I tried to readjust the original situation: No notification at all.

                                  Then in Options\Editor\Visual Basic\Code Analysis\Possible NullReferenceException I set all items to "Thorough" and checked "Check anonymous methods' parameters".

                                  But nothing happens. No notification at all again. Have I missed something?

                                • Alexander K (DevExpress) 09.21.2018

                                  Hi Robert,

                                  Thank you for informing us of your results. We are concerned to hear that the analyzer doesn't work on your side.

                                  I tried to reproduce this issue on my side but my attempts were unsuccessful. If I set the "Thorough" mode of analysis, "Possible System.NullReferenceException" is shown for properties on my side:

                                  Please check that the diagnostic is enabled on your side. For this, perform the following steps:

                                  1. From the Visual Studio menu, select "CodeRush\Options..." .
                                  2. In the tree view on the left, navigate to the "Editor\Visual Basic\Code Analysis" folder.
                                  3. Select the "Code Issues Catalog" options page.
                                  4. Check the "Enabled" item for the "CRR0027 - Possible System.NullReferenceException" code issue.
                                  5. If you use Visual Studio's background analysis, check the "Register in VS" item. Note that this change will be applied only after Visual Studio restart.

                                  If the issue persists with the enabled diagnostics, provide us with a code sample and your CodeRush settings. You can import CodeRush settings via the "CodeRush\Support\Import and Export Settings..." dialog.

                                • Robert 15 09.21.2018

                                  Hi Alexander,

                                  as I can see from screenshot, you used the second example. In this example I get only the notification for "PropB". Following the code I'm using for investigation (only copy/paste from this ticket, and class renamed in the second example):

                                  [VB.NET]
                                  Namespace T578383 'The "Possible NullReferenceException" diagnostic 'Mark/Attribute members/parameters as NotNull 'https://www.devexpress.com/Support/Center/Question/Details/T578383 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 Public Class Class2 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 End Namespace
                                • Alexander K (DevExpress) 09.21.2018

                                  Robert,

                                  Thank you for providing the code sample and CodeRush settings.

                                  In the Thorough mode, CodeRush performs an additional check for properties and detects properties that always return an initialized value (the PropA and PropB from the Class1 type, and PropA from the Class2 type in your sample). In these cases, a diagnostic is not shown. If you change getter accessors of these properties on a single return statement, then a diagnostic will be shown. Also, you can set the Skeptical mode for properties. In this mode, additional check is not performed and a diagnostic will be shown.

                                • Robert 15 09.21.2018

                                  Hi Alexander,

                                  ah, I see. Ok, this works now.

                                  Question: When set to "Skeptical", defining an attribute "IsNotNullAttribute" or "NotNullAttribute" (see below) and assigning it to a property, CRR0027 should be omitted? I'm right? But when I do this, CRR0027 is still reported.

                                  [VB.NET]
                                  <AttributeUsage(AttributeTargets.Property)> Public NotInheritable Class IsNotNullAttribute Inherits Attribute End Class <AttributeUsage(AttributeTargets.Property)> Public NotInheritable Class NotNullAttribute Inherits Attribute End Class Public Class Class3 Private _propA As String <IsNotNull> <NotNull> 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 'CRR0027 Return PropA.Length End Function End Class
                                • Alexander K (DevExpress) 09.24.2018

                                  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.

                                • Robert 15 09.24.2018

                                  Hi Alexander,

                                  it works like desired. Cool! <thumbs up>

                                • Alexander K (DevExpress) 09.24.2018

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