Fix generate event handler with invalid name (#59935)#60810
Fix generate event handler with invalid name (#59935)#60810jasonmalinowski merged 3 commits intodotnet:mainfrom maciek231:GenerateEventHandlerNameForEventsInGenericClass
Conversation
| { | ||
| void M() | ||
| { | ||
| Generic<int>.MyEvent +$$ |
There was a problem hiding this comment.
Can you add a test for global::Generic<int>.MyEvent? I'm expecting it to fail.
There was a problem hiding this comment.
For that, I suggest using this extension method
There was a problem hiding this comment.
Yes, it failed both for generic and non-generic. I added more tests and fix that
| // Even easier -- the LHS of the dot is the name itself | ||
| return lhsNameSyntax.ToString(); | ||
| } | ||
| return lhs.ToString(); |
There was a problem hiding this comment.
GetRightmostName could return null (at least it's annotated as such), does this need a null check? My gut is it seems like you'd need to have something malformed to the left of the period but still somehow have succeeded in binding the event, so that seems like it'd be a difficult thing to achieve.
There was a problem hiding this comment.
@jasonmalinowski Nice catch!
But will it really be something malformed? I think it can be null in a completely valid scenario like this?
using System;
class C
{
public event EventHandler MyEvent;
public static C CreateC()
{
return new C();
}
public void M2()
{
CreateC().MyEvent +=
}
}Here memberAccessExpression.Expression is InvocationExpressionSyntax where GetRightmostName returns null. We need to fall into syntaxFactsService below when it's null, I think.
There was a problem hiding this comment.
@Youssef1313 Ah right, of course. I was mentally reading through the GetRightmostName() then forgetting that we're passing in the LHS of the original dot, so yeah. If we don't have a test for that case, we should add one regardless,
| } | ||
|
|
||
| if (lhs is NameSyntax lhsNameSyntax) | ||
| if (lhs != null) |
There was a problem hiding this comment.
lhs is not null maybe
|
Thanks @maciek231 for the fix! |
Fix #59935