Fix NRE in VisualBasicAddExplicitCastCodeFixProvider #62075
Fix NRE in VisualBasicAddExplicitCastCodeFixProvider #62075dibarbet merged 5 commits intodotnet:mainfrom
Conversation
| Dim invocationNodeExpression = spanNode.GetAncestors(Of ExpressionSyntax).FirstOrDefault( | ||
| Function(node) Not node.ChildNodes.OfType(Of ArgumentListSyntax).IsEmpty()) |
There was a problem hiding this comment.
| Dim invocationNodeExpression = spanNode.GetAncestors(Of ExpressionSyntax).FirstOrDefault( | |
| Function(node) Not node.ChildNodes.OfType(Of ArgumentListSyntax).IsEmpty()) | |
| Dim invocationNodeExpression = spanNode.GetAncestors(Of InvocationExpressionSyntax).FirstOrDefault( | |
| Function(node) node.ArgumentList is not nothing) |
?
There was a problem hiding this comment.
Unclear as to what this change is for.
Not against making the change, Just need clarification on why this is better than the code that was originally there.
There was a problem hiding this comment.
instead of checking every arbitrary node, you are only looking at invocations. and because you're only looking at invocatinos, you don't have to enumerate all child nodes looking for argumentlists, you can just directly check if it has an argument list or not.
(the same feedback applies to the check below this for atttributes as well).
thanks!
There was a problem hiding this comment.
Gotcha! I'll make that change tonight and test it once I'm off work
thanks.
There was a problem hiding this comment.
Code change made and tested locally, just working through adding the unit test.
I'm trying to work through adding this unit test but I'm hitting a bit of a roadblock. I'm positive this is just due to my not understanding how the unit tests work, but I've added one by looking at what the others are doing and trying to replicate that with this scenario, but the test just doesn't seem to be working. The below is what I am trying to do to implement this test. however, with the above code, I get the following error This is my first time delving into these unit tests, so I'm positive I'm just missing something super simple |
This might not be the only cause, but I think the caret location marker is missing brackets. Your code shows but I believe it should be |
ah ha. I'm getting further now. It throws "No action was offered when one was expected." but i'm probably missing something in the class definition that the test framework is expecting. |
|
I think I have it working. Just need to do a tiny bit more testing. |
…and adding Unit Test
|
|
||
| <Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)> | ||
| Public Async Function TestClassAttribute() As Task | ||
| Await TestMissingInRegularAndScriptAsync( |
There was a problem hiding this comment.
So this tests that the code action is missing, but we should be testing that it is present. The test snippet you posted before I think was the correct way to go.
I did notice though that the diagnostic only shows up when Option Strict Off is set. So for your old test code you may be able to fix it just by changing the On to Off
There was a problem hiding this comment.
ah, that explains why I couldn't get the test to fail. I will revert that tomorrow. I left the power adapter for my Dev laptop at work.
There was a problem hiding this comment.
Reverted and set Option Strict Off, but now I'm getting
System.Exception : No action was offered when one was expected.
I debugged, and it looks like the CodeAction is null, but i'm not understanding how the test framework actually gets the CodeActions to perform. below is my most recent test code.
<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
Public Async Function TestClassAttribute() As Task
Await TestInRegularAndScriptAsync(
"Option Strict Off
Imports System.Runtime.InteropServices
<ClassInterface([|1|])>
Class Program
End Class",
"Option Strict Off
Imports System.Runtime.InteropServices
<ClassInterface(CShort(1))>
Class Program
End Class")
End Function
There was a problem hiding this comment.
Hmm - nothing immediately obvious stands out to me as being wrong. Potentially the test workspace doesn't have the right reference for System.Runtime.InteropServices? Not sure that is the case though. I can check out this branch tomorrow (its the end of my work day now) and see if I can help debug where its going wrong and report back.
There was a problem hiding this comment.
i'd recommend not testing this exact interface, but rather just include a simple copy of it in the test.
There was a problem hiding this comment.
@kfechter I finally got it working - looks like I needed to pass the option strict as a compilation option instead. The change I made is here - dibarbet@5cf383c
I can either update your branch here or I can leave it to you to update the test, whichever you prefer.
There was a problem hiding this comment.
I can update the test. I appreciate the help!
| Dim invocationNodeExpression = spanNode.GetAncestors(Of InvocationExpressionSyntax).FirstOrDefault( | ||
| Function(node) node.ArgumentList IsNot Nothing) | ||
|
|
||
| Dim invocationNodeAttribute = spanNode.GetAncestors(Of AttributeSyntax).FirstOrDefault( |
There was a problem hiding this comment.
| Dim invocationNodeAttribute = spanNode.GetAncestors(Of AttributeSyntax).FirstOrDefault( | |
| Dim attributeNode = spanNode.GetAncestors(Of AttributeSyntax).FirstOrDefault( |
dibarbet
left a comment
There was a problem hiding this comment.
lgtm! Our main branch is locked down due to some infra issues, but I'll merge this when it opens. Thanks!!
Fixes issue #61538
NRE no longer occurs when a class attribute is used that triggers BC30519. Cast suggestions are now provided.