Fix "Generate method" to add static modifier for function pointers#80985
Merged
CyrusNajmabadi merged 7 commits intomainfrom Nov 3, 2025
Merged
Fix "Generate method" to add static modifier for function pointers#80985CyrusNajmabadi merged 7 commits intomainfrom
CyrusNajmabadi merged 7 commits intomainfrom
Conversation
- Modified TryDetermineTypeToGenerateInWorker to detect address-of expressions used with function pointers - Added IsInFunctionPointerAddressOfContext helper method to check if expression is within address-of operator that targets a function pointer type - Added test case InferMethodFromAddressOfInNonStaticContext to verify fix works in non-static contexts Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
- Updated IsInFunctionPointerAddressOfContext to access AddressOfExpression directly from ISyntaxKindsService (which inherits from ISyntaxKinds) - Fixed incorrect property access that was causing CS1061 error Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
- Simplified IsInFunctionPointerAddressOfContext with early returns to reduce nesting - Removed redundant addressOfExpression variable and use expression.Parent directly - Improved code readability Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix generate method to include static modifier for function pointers
Fix "Generate method" to add static modifier for function pointers
Nov 2, 2025
src/Analyzers/CSharp/Tests/GenerateMethod/GenerateMethodTests.cs
Outdated
Show resolved
Hide resolved
src/Analyzers/Core/CodeFixes/GenerateMember/AbstractGenerateMemberService.cs
Outdated
Show resolved
Hide resolved
| if (syntaxKinds == null || expression.Parent.RawKind != syntaxKinds.AddressOfExpression) | ||
| return false; | ||
|
|
||
| var typeInfo = semanticModel.GetTypeInfo(expression.Parent, cancellationToken); |
Contributor
There was a problem hiding this comment.
@copilot no need to do this type check. If we're ever generating a method for an adress-of operation, it must always be static no matter what
Contributor
Author
There was a problem hiding this comment.
Removed the type check - now always returns true for address-of operations in fefc2ae
- Add WorkItem attribute to test method - Simplify IsInAddressOfContext to use syntaxFacts.SyntaxKinds - Remove unnecessary type check - address-of always requires static - Add test for qualified name scenario (&C.M2) Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
| { | ||
| // If we're generating a method for an address-of operation, it must always be static | ||
| return expression.Parent != null && expression.Parent.RawKind == syntaxFacts.SyntaxKinds.AddressOfExpression; | ||
| } |
Contributor
There was a problem hiding this comment.
@copilot this method is too simple. Just inline it.
Contributor
Author
There was a problem hiding this comment.
Inlined in 343d351e
The helper method was too simple, so inlined it directly into the condition check per feedback from @CyrusNajmabadi Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
CyrusNajmabadi
approved these changes
Nov 2, 2025
Contributor
|
@dotnet/roslyn-ide ptal. |
JoeRobich
approved these changes
Nov 3, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix "Generate method" to add
staticmodifier for function pointersFixes: Function pointers can only point to static methods in C#, but the "Generate method" code fix was not adding the
staticmodifier when invoked from a non-static context.Implementation Summary
Problem:
Before: Generated
private void M2()(missingstatic, causes CS8759)After: Generates
private static void M2()✅Changes Made
Modified
src/Analyzers/Core/CodeFixes/GenerateMember/AbstractGenerateMemberService.csTryDetermineTypeToGenerateInWorkerto detect address-of contextsAddressOfExpression, force staticif (!isStatic && expression.Parent != null && expression.Parent.RawKind == syntaxFacts.SyntaxKinds.AddressOfExpression)Added test cases in
src/Analyzers/CSharp/Tests/GenerateMethod/GenerateMethodTests.csInferMethodFromAddressOfInNonStaticContext- simple name scenario&M2InferMethodFromAddressOfInNonStaticContextWithQualifiedName- qualified name scenario&C.M2Key Improvements (from PR feedback)
syntaxFacts.SyntaxKindsdirectlyVerification
Original prompt
staticmodifier when generating method for function pointer #80984💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.