JetBrains / JetBrains/resharper-unity

Range attribute usage leads to confusing inspection

Open
#2,149 0 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

Hey there!

Example code:

```
using UnityEngine;

namespace CodeStage
{
public class JetBrainsBug : MonoBehaviour
{
[Range(5, 100)]
public int value = 10;

private void Update()
{
if (value < 3) // ReSharper: Expression is always false
{
Debug.Log("I'm lesser than 3!");
}
}
}
}
```

if (value < 3) line produces "Expression is always false" inspection with suggestions to remove if or replace it with `false`.
The problem is "value" field can easily be changed outside the JetBrainsBug class to absolutely any value including those which are less than 3.

Here is a simplest example of changing value to 1 from the same class without any warnings:

```
namespace CodeStage
{
public class JetBrainsBug : MonoBehaviour
{
[Range(5, 100)]
public int value = 10;

public int Value
{
get => value;
set => this.value = value;
}

private void Start()
{
value = 1; // possible violation of ValueRange attribute
Value = 1; // silent!
}

private void Update()
{
if (value < 3) // ReSharper: Expression is always false
{
Debug.Log("I'm lesser than 3!");
}
}
}
}
```

Thus this inspection causes confusion and may lead to potential erroneous actions from the programmer especially when working on libraries \ APIs which will be used by other developers.

Contributor guide

Open the contributing guide

Research direction

Reproduce the inspection in a Unity C# project using the JetBrainsBug examples, including the public field and property assignments. Start by locating the Unity Range attribute handling and inspection tests or entry point; done means valid code that can receive out-of-range values no longer produces a misleading always-false warning.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.