When using CodeRush for Roslyn, I noticed a false positive coming from one of the analyzers. This is to do with the if block matching the else block, below is the excerpt of code:
Visual BasicIf Value.TriggerIdentity.DataSetIdentity.DataSetIndex = 0 OrElse Value.TriggerIdentity.TriggerIndex = 0 Then Exit Sub
As you would probably agree, this is a false positive.
Another example, this time with a string:
If Description Is Nothing OrElse Description.Length = 0 Then Throw New ApplicationException("Exception Description is Null")
Hi Mark,
Thank you for providing the code samples. I have tried to reproduce this issue locally but CodeRush for Roslyn doesn't detect any Code Issues in your code:
It seems that the issue has different nature and is more complex. Please provide us with the code of the whole method in which the issue is reproduced.
Alexander, leave it with me and I will get back to you with a proper example, probably on Monday now looking at the time.
Alexander, as promised here is a more concrete example, which I've confirmed reproduces this behaviour when in a standalone project:
Public Class Test Structure A Dim StopIndex As Integer End Structure Public Shared Sub Testing() Dim Test As A = Nothing With Test If .StopIndex > 0 Then Else End If End With End Sub End Class
Hi Mark,
Thank you for providing the code sample. In this case, the if-block and else-block are identical (both are empty). So, we think that the diagnostics show correct results. Can you please clarify why it is a false positive in your opinion?
My apologies, I had a lack of understanding what it was trying to tell me. What it's actually telling me is the code contained within the if and else block are the same. As far as I can see this is functioning as expected, but if I do find a relevant example of a false positive I will post one.
No problem, Mark. We look forward to your new example.
Alex, hopefully this is a better example of what I hope would be considered a false positive:
If PhoneNumber.Substring(2, 1) = "0" Then 'Eight Digit Local Number Return "(" & PhoneNumber.Substring(0, 3) & ") " & PhoneNumber.Substring(3, PhoneNumber.Length - 3) ElseIf PhoneNumber.Substring(2, 1) = "6" Then If PhoneNumber.Length >= 5 AndAlso PhoneNumber.Substring(3, 2) = "24" Then '07624 Return "(" & PhoneNumber.Substring(0, 5) & ") " & PhoneNumber.Substring(5, PhoneNumber.Length - 5) Else 'Eight Digit Local Number Return "(" & PhoneNumber.Substring(0, 3) & ") " & PhoneNumber.Substring(3, PhoneNumber.Length - 3) End If Else 'Eight Digit Local Number Return "(" & PhoneNumber.Substring(0, 3) & ") " & PhoneNumber.Substring(3, PhoneNumber.Length - 3) End If
While I appreciate that there are two branches within the if statement that are the same, there is an elseif statement between them, which would in my opinion, render this warning invalid. As we are using the code in the elseif statement to determine which branch of code to execute.
Mark,
Thank you for the code sample. Please give us some time to research the situation in detail and see if we can improve the analysis engine.
Not a problem, always happy to help improve and provide feedback.