Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RCS1169 not being diffused by roslynator_unity_code_analysis.enabled = true #1418

Closed
somedeveloper00 opened this issue Mar 6, 2024 · 4 comments · Fixed by #1419
Closed

Comments

@somedeveloper00
Copy link

somedeveloper00 commented Mar 6, 2024

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;
        }
    }
}
@somedeveloper00
Copy link
Author

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

@josefpihrt
Copy link
Collaborator

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?

@somedeveloper00
Copy link
Author

somedeveloper00 commented Mar 6, 2024

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.

@somedeveloper00
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants