Steps to Reproduce:
Given
CodeFixContext context
ClassDeclarationSyntax declaration
and assuming that
unsafe modifierthen
var generator = SyntaxGenerator.GetGenerator(context.Document);
var newDeclaration = generator.WithModifiers(declaration, generator.GetModifiers(declaration));
will remove the unsafe modifier.
Expected Behavior:
newDeclaration has the same modifiers as the old declaration
Actual Behavior:
The unsafe modifier is removed.
Root cause is a series of bugs in Microsoft.CodeAnalysis.CSharp.CodeGeneration.CSharpSyntaxGenerator.GetAllowedModifiers, which seems to be ignorant of the existence of the unsafe modifier.
This causes issues in CodeFixProviders, such as the one reported here.
It also needs to be updated to support ref and readonly on structs.
@sharwell Why did you mark this as "help wanted"? Do you need additional information?
@KrisVandermotten Tagging the issue with "help wanted" just says this issue is understood and can be picked by community contributors if interested and we will gladly accept the PR with fix :-)
Fixed with https://github.com/dotnet/roslyn/pull/32422