JetBrains / resharper-unity

Unity support for both ReSharper and Rider
Apache License 2.0
1.21k stars 134 forks source link

Unused private fields are not flagged as "unused" #587

Closed rob8861 closed 6 years ago

rob8861 commented 6 years ago

Hello,

I recently noticed that unused private fields no longer get flagged as being "unused" (getting dimmed). However, if I decorate the field with [SerializedField] then they will be flagged as "unused" if they are truly not being used in the code.

For example

public SomeClass : MonoBehavious 
{

         private float _x;  // will not be flagged as "unused"

         [SerilaizedField]
         private float _y; // will be flagged as "unused"

         // both _x and _y are not being used in the class
}

NOTE: A non-Unity project does not exhibit this issue.

citizenmatt commented 6 years ago

This is actually expected behaviour. Unity's generated project files disable warnings for the CS0169 compiler warning, which warns when a class member, such as a field, is unused. Rider (and the underlying ReSharper engine) respect this setting, and so doesn't show warnings for unused private fields. I don't believe anything has changed in the Rider/ReSharper behaviour here, so I'm going to close this with the dreaded "by design".

oxysoft commented 5 years ago

Hi, is it be possible to know why? I've been wondering for a while why Rider doesn't warn me of unused members. I've been exclusively with Unity and Rider for so long that I had actually forgotten that the C# compiler warns you of unused members. WHICH is a hugely useful feature! I was just returning to an old API this morning that had been written hastily and "live on the battlefield" so to speak. There were a few unused fields peppered throughout as a result. If Rider could have highlighted unused members immediately, I could have directed my attention more efficiently. Unless there is an important reason why, it should certainly be possible to override this setting. Perhaps it is not Jetbrains' job to clean up this mess, but either way we should try to find a solution.

citizenmatt commented 5 years ago

Unity disable the warning to simplify working with serialised fields. It's possible to have a private field be a serialised field if you use the [SerializeField] attribute. In this case, the field isn't initialised by code, so the compiler should show a warning. However, it is initialised by Unity, so the warning is redundant.

bartlomiej-dawidow commented 4 years ago

It'd be great if resharper-unity had an option to override that behavior and only ignored the warning if it's annotated with [SerializeField].

van800 commented 4 years ago

@bartlomiej-dawidow Please follow this request https://youtrack.jetbrains.com/issue/RIDER-38826 about it.

benblo commented 4 months ago

I just noticed a variant of this behavior: Rider greys-out protected/public unused fields, but not private ones! (If anything, it should be the opposite). Whether or not the field is serialized doesn't matter:

image

I tried unchecking the "Suppress Common Warnings" in Unity, now I get yellow squiggles on private fields (but not protected/public ones), but still no greying out.

image

I fail to imagine a scenario where this is desirable. I understand that the warning is controlled by csproj-generation, but a Roslyn analyzer could catch them nonetheless.

IMO that setting shouldn't matter as far as the IDE/analyzer is concerned, whether or not we want a compiler warning is orthogonal to wanting consistent analysis & UX (greying-out).


Also, the greyed-out fields don't appear in the Problems list/window, is there a Rider settings to do that? ie report IDE/analyzer issues as problems. With "suppress" off, I can see the unused private fields (as warning), but still not the protected/public ones. (I actually understand why, those might be used in potential derived classes, although it still seems like it would be interesting to have the option to say "warn me anyway"; anyway, this bears on the compiler, not Rider.)

image


This happens with latest Rider (2024.1), Unity 2022.2.16, package 3.0.28.

Rider's About window

JetBrains Rider 2024.1 Build #RD-241.14494.307, built on April 8, 2024 Licensed to Benoit FOULETIER Subscription is active until September 12, 2024. Runtime version: 17.0.10+8-b1207.12 amd64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o. Windows 10.0 .NET Core v8.0.1 x64 (Server GC) GC: G1 Young Generation, G1 Old Generation Memory: 4044M Cores: 8 Registry: ide.experimental.ui=true ide.new.project.model.index.case.sensitivity=true Non-Bundled Plugins: net.seesharpsoft.intellij.plugins.csv (3.3.0-241)

van800 commented 4 months ago

@benblo The behaviour with not greyed out private fields is not specific to Unity project, please ask with Help->Report a Bug or Help->Contact Support.

benblo commented 3 months ago

@benblo The behaviour with not greyed out private fields is not specific to Unity project, please ask with Help->Report a Bug or Help->Contact Support.

Thanks, I finally took the time to report it there: https://youtrack.jetbrains.com/issue/RIDER-113399/Unused-private-fields-are-not-flagged-as-unused