Roslyn: IDE0044 (add readonly modifier) fires incorrectly for fixed buffers

Created on 24 Apr 2018  路  10Comments  路  Source: dotnet/roslyn

Version Used: 15.7.0 Preview 4.0

Steps to Reproduce:

  1. In a new C# project, set "Allow unsafe" to true in the project properties.
  2. In a new file, type
    ```C#
    unsafe struct Foo
    {
    private fixed byte _data[8];
    }
2. Put caret on the _data field and press Ctrl . (light-bulb)

**Expected Behavior**:
No tip

**Actual Behavior**:
"Add readonly modifier" (IDE0044) is suggested to replace 'fixed' with 'readonly'.
If you take the suggestion, the app fails to compile with:

error CS0650: Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type.
error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
```

Area-IDE Bug Resolution-Fixed

Most helpful comment

@Xwilarg unfortunately that will not work in Unity since private fields decorated with [SerializeField] cannot be read only. They are exposed to the Unity editor and meant to be edited in code.
However, I spoke to the folks at Visual Studio Tools For Unity and apparently this warning can be turned off.

Under the Tools->Options menu, select the TextEditor->C#->Code Style->General tab. Under Field preferences, there is a Prefer readonly option. Set that to No.

All 10 comments

Hello,

I just wanted to indicate that this issue occurs also in Unity when decorating a private variable as [SerializedField]. Roslyn marks the field with IDE0044 , however, no fix it suggested. In fact the tool tip is completely blank and the only "suggested fix" is to suppress it.

@rob8861 I have the suggestion to add a readonly modifier on Unity
readonlymodifier

However the description is empty
description

@Xwilarg for those of us who are not Unity devs, could you explain what the issue is here? Is this a field that is only ever written in a constructor? Or is it written somewhere else? If so, where?

@Xwilarg the empty description is a duplicate of #26219. If there are other issues, can you describe those in some additional detail by filing a separate bug? I'd like to keep the current bug easy and focused on fixed fields.

@CyrusNajmabadi I think Unity documentation would explain better than me https://docs.unity3d.com/ScriptReference/SerializeField.html

I used them because they allow you to fill variable's values directly from the Unity editor
unity

@Xwilarg That reference talks about a specific attribute being on certain fields. However, in your post there aren't any attributes on "tookStairs". So i'm a bit confused :)

@Xwilarg unfortunately that will not work in Unity since private fields decorated with [SerializeField] cannot be read only. They are exposed to the Unity editor and meant to be edited in code.
However, I spoke to the folks at Visual Studio Tools For Unity and apparently this warning can be turned off.

Under the Tools->Options menu, select the TextEditor->C#->Code Style->General tab. Under Field preferences, there is a Prefer readonly option. Set that to No.

@CyrusNajmabadi Oh sorry, in fact there was a [SerializedField] but it was out of the screenshot.

@Xwilarg this issue is also repaired using public variables instead a [SerializedField], and my question about this is, in a project dev, whit partners and friend in job is so important the hidden info principle?

Using log4net I get it if i leave the readonly modifier from this line (and other like it) ...

static readonly ILog log = LogManager.GetLogger(typeof(Startup));

... this is just sat in the top of the class in question, not sure why this is an issue as the problem is not well explained.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

glennblock picture glennblock  路  3Comments

binki picture binki  路  3Comments

JesperTreetop picture JesperTreetop  路  3Comments

MadsTorgersen picture MadsTorgersen  路  3Comments

marler8997 picture marler8997  路  3Comments