Roslyn: CSharpSyntaxGenerator.GetAllowedModifiers incorrectly ignores unsafe modifier

Created on 15 Jan 2018  路  4Comments  路  Source: dotnet/roslyn

Steps to Reproduce:

Given

 CodeFixContext context
 ClassDeclarationSyntax declaration

and assuming that

  • the document is a C# document,
  • the class declaration has an unsafe modifier

then

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.

Area-IDE Bug help wanted

All 4 comments

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 :-)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

asvishnyakov picture asvishnyakov  路  3Comments

marler8997 picture marler8997  路  3Comments

AdamSpeight2008 picture AdamSpeight2008  路  3Comments

joshua-mng picture joshua-mng  路  3Comments

orthoxerox picture orthoxerox  路  3Comments