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

private standard Unity MonoBehaviour methods generate IDE0051 #170

Closed
WarpedPixel opened this issue May 26, 2021 · 5 comments
Closed

private standard Unity MonoBehaviour methods generate IDE0051 #170

WarpedPixel opened this issue May 26, 2021 · 5 comments
Labels
other project This issue is not related to this project

Comments

@WarpedPixel
Copy link

(Hopefully this is the right way to report what looks like a bug to me. Love the analyzers and trying to get to zero warning in my code, thanks!)

Bug description

This code generates IDE0051 with analyzer 1.10.2:

public class UserInput : MonoBehaviour
{
  void Start() // this line triggers IDE0051
  {
        InitializeMouse();
  }
  public void Update() // this line does NOT trigger IDE0051
  {
        GetMouseClick();
  }
}

Expected behavior

These methods are not "unused" since, when derived from MonoBehaviour, they are actually invoked by Unity.
This is regardless if they are public or private.
So the expected behavior is for all these methods with standard Unity MonoBehavior signatures (Start, Awake, Update, etc.) to never flag IDE0051. While other private methods continue to flag that normally.

Workaround

Just make sure all your standard MonoBehaviour methods are declared public

@sailro
Copy link
Member

sailro commented May 26, 2021

Hello @WarpedPixel, we already have a dedicated suppressor to handle that:

here is the documentation:
https://github.com/microsoft/Microsoft.Unity.Analyzers/blob/main/doc/USP0003.md

here is the related test:
https://github.com/microsoft/Microsoft.Unity.Analyzers/blob/main/src/Microsoft.Unity.Analyzers.Tests/MessageSuppressorTests.cs#L37

So my guess is that the issue is perhaps on your side ?

  • What is your Visual Studio version ?
  • What is your Unity version ?
  • You say you are using the nuget 1.10.2 ? Could you share your csproj files with us? You can send them to vstusp-at-microsoft-dot-com.

Thanks!

@WarpedPixel
Copy link
Author

WarpedPixel commented May 26, 2021

You have indeed. Running vscode latest on Mac Catalina latest. Unity 2019.4.23f1. I had to patch my csproj files to limit LangVersion to 8.0 since that is what Unity supports. I definitely see other UNTxxxx warnings. For example, I see UNT0001 if the Update body in my example above is empty. I'll test a few things.

EDIT: I also had to manually install the NuGet package following these instructions (but I did not disable IDE0051): https://code.visualstudio.com/docs/other/unity#_enabling-unity-warnings

@sailro
Copy link
Member

sailro commented May 26, 2021

Ah that's why: vscode (omnisharp in fact) is not yet able to use suppressors (USPxxxx), only regular analyzers (UNTxxxx) are working so far.

The original issue is tracked here:
OmniSharp/omnisharp-roslyn#1711

The good news, a PR seems to fix that:
OmniSharp/omnisharp-roslyn#2116

@sailro sailro added the other project This issue is not related to this project label May 26, 2021
@WeirdBeardDev
Copy link

@WarpedPixel here are directions on how to setup an .editorconfig file and then turn off IDE0051.

@sailro sweet, I've been waiting for this to be addressed. Thanks!

@enkeyz
Copy link

enkeyz commented Jan 1, 2022

I have the same issue, looks like something broke again. VStudio 2022 marks Monobehaviour methods - Start, Update, etc - as unused methods, after creating a new Monobehaviour inside Unity Editor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
other project This issue is not related to this project
Projects
None yet
Development

No branches or pull requests

4 participants