dotnet / roslynator

Roslynator is a set of code analysis tools for C#, powered by Roslyn.

Home Page:https://josefpihrt.github.io/docs/roslynator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RCS1169 not being diffused by roslynator_unity_code_analysis.enabled = true

somedeveloper00 opened this issue · comments

Product and Version Used:
vscode with C# dev kit | Unity project | roslynator as nuget package (latest to date)
Steps to Reproduce:
add the following to .editorconfig

[*.cs]
roslynator_unity_code_analysis.enabled = true

then write a script that a field with [SerializeField] attribute, like

using UnityEngine;

namespace Namespace
{
    public class Test : MonoBehaviour
    {
        [SerializeField] private float speed;

        private void Update()
        {
            transform.position += speed * Time.deltaTime * Vector3.forward;
        }
    }
}

Actual Behavior:

using UnityEngine;

namespace Namespace
{
    public class Test : MonoBehaviour
    {
        [SerializeField] private readonly float speed;

        private void Update()
        {
            transform.position += speed * Time.deltaTime * Vector3.forward;
        }
    }
}

(note that Update did not get removed, showing that [RCS1213](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1213) is being diffused)
Expected Behavior:

using UnityEngine;

namespace Namespace
{
    public class Test : MonoBehaviour
    {
        [SerializeField] private float speed;

        private void Update()
        {
            transform.position += speed * Time.deltaTime * Vector3.forward;
        }
    }
}

I think it has something to do with Unity not using Attribute with the SerializeField class name. I looked at the source codes for roslynator and saw that at

private static readonly MetadataName UnityEngine_SerializeFieldAttribute = MetadataName.Parse("UnityEngine.SerializeFieldAttribute");
its searching for UnityEngine.SerializeFieldAttribute, but in Unity, it's declared as UnityEngine.SerializeField

I think it has something to do with Unity not using Attribute with the SerializeField class name.

Yes, that's the problem.

Do you have any idea why suffix Attribute is omitted in the name of this attribute?

Do you have any idea why suffix Attribute is omitted in the name of this attribute?

I have no idea. Might be an old thing that remained in the engine for compatibility purpures, but then they change a lot of APIs in each major version, so I don't know why they don't change this as well (low priority?). But yeah, there's not official reason publicly available anywhere.

I checked and asked around, the attribute has always been just SerializeField. So it's safe to change the analyzer code to look only for UnityEngine.SerializeField