Skip to content

[Low priority] Unity objects should not use coalescing assignment #20

Open
@lcnvdl

Description

@lcnvdl

Hi! I think I found something that can be fixed easily :)

https://github.com/microsoft/Microsoft.Unity.Analyzers/blob/main/doc/UNT0023.md

Image

UNT0023 Coalescing assignment on Unity objects
Unity overrides the null comparison operator for Unity objects, which is incompatible with coalescing assignment.

Examples of patterns that are flagged by this analyzer

using UnityEngine;

class Camera : MonoBehaviour
{
    public Transform a = null;
    public Transform b = null;

    public Transform NP()
    {
        return a ??= b;
    }
}

Solution

Use null comparison:

using UnityEngine;

class Camera : MonoBehaviour
{
    public Transform a = null;
    public Transform b = null;

    public Transform NP()
    {
        return a = a != null ? a : b;
    }
}

A code fix is offered for this diagnostic to automatically apply this change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions