JetBrains / JetBrains/resharper-unity

Suggestion to rewrite as ??= is incorrect

Open
#1,818 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
1.2k
Forks
142
PR merge metrics
No merged PRs in 30d

Description

(Visual Studio)

In the following code:

```
using UnityEngine;

public class Billboard : MonoBehaviour
{
public Camera Camera;

private void Start()
{
if (Camera is null)
{
Camera = Camera.main;
}
}

private void Update()
{
transform.LookAt(Camera.transform.forward);
}

private void OnDrawGizmos()
{
Debug.DrawRay(transform.position, transform.forward);
}
}
```

It suggests to rewrite as `Camera ??= Camera.main` which won't work.

But if you'd write this, the suggestion doesn't show which is correct:

```
if (Camera == null)
{
Camera = Camera.main;
}
```

Contributor guide

Open the contributing guide

Research direction

No source files or tests are named. Reproduce the inspection in Visual Studio using both code snippets from the issue, then trace the analyzer or quick-fix responsible for suggesting ??=. Done means the inspection no longer proposes an invalid rewrite for the first snippet while preserving correct behavior for the second.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, unity
Domain
game-dev, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.