Skip to content

Visual studio suggests removing redundant parenthesis for spans that aren't redundant. #44629

@mburbea

Description

@mburbea

Version Used: VS 2019 preview 16.7

Steps to Reproduce:
Add the following method to a class.

private static void BadSuggestion()
{
    var span = (stackalloc byte[8]);
    for(int i = 0; i < span.Length; i++)
    {
         span[i] = (byte)0xFF;
    }
}

Visual studio recommends removing the redundant parenthesis in the line var span = (stackalloc byte[8]);. However doing so leads to the following errors

error CS0214: Pointers and fixed size buffers may only be used in an unsafe context
error CS1061: 'byte*' does not contain a definition for 'Length' and no accessible extension method 'Length' accepting a first argument of type 'byte*' could be found (are you missing a using directive or an assembly reference?)
error CS0214: Pointers and fixed size buffers may only be used in an unsafe context

Expected Behavior:
VS would not suggest to remove the parenthesis.

As an aside, perhaps the issue is that

static unsafe void Foo()
{
     var thisIsABytePointer = stackalloc byte[8];
     var thisIsASpanOfBytes = (stackalloc byte[8]);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-IDEBugResolution-FixedThe bug has been fixed and/or the requested behavior has been implemented

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions