1

When working with unity code warning CS0649 and CS0414 fire when unity rebuilds the solution, even when the unity attribute [SerializeField] is applied. I want to suppress these false positives in source through the context menu in the Error list, or the quick actions menu. I do not want to globally disable CS0649 and/or CS0414 because in a case where [SerializeField] was forgotten or is not appropriate the warning is obviously helpful.

I've edited the csc.rsp file and added /w40649 to re-enable the warning in visual studio, however when I right click the error the "suppress error in source" option is missing. I can manually suppress the error with #pragma warning disable CS0649, CS0414 and re-enabling with #pragma warning restore CS0649, CS0414 However this is very tedious to modify by hand.

I've already tried "Suppress Warning" menuitem in Visual Studio 2010 is not available however this did not seem to add back the context menu item, although it does appear to be listed in the tools customization in Context menu > "Other Context Menus | Error List" as expected, oddly.

Without suppression:

    [SerializeField]
    private bool flashCustomColor = false;

    [SerializeField, OneLine.OneLine, ShowIf("flashCustomColor")]
    private FlashingColors flashingColors;

    [System.Serializable]
    private struct FlashingColors
    {
        public Color GetHurtColor() => hurtColor ?? Color.red;

        [SerializeField]
        private Color? hurtColor;

        public Color GetNormalColor() => normalColor ?? Color.white;

        [SerializeField]
        private Color? normalColor;
    }

With Suppression:

    [SerializeField]
#pragma warning restore IDE0044, CS0414
    private bool flashCustomColor = false;
#pragma warning restore IDE0044, CS0414

    [SerializeField, OneLine.OneLine, ShowIf("flashCustomColor")]
    private FlashingColors flashingColors;

    [System.Serializable]
    private struct FlashingColors
    {
        public Color GetHurtColor() => hurtColor ?? Color.red;

        [SerializeField]
#pragma warning disable CS0649, IDE0044
        private Color? hurtColor;
#pragma warning restore CS0649, IDE0044

        public Color GetNormalColor() => normalColor ?? Color.white;

        [SerializeField]
#pragma warning disable CS0649, IDE0044
        private Color? normalColor;
#pragma warning restore CS0649, IDE0044
    }

I am trying to enable that context menu item, but it doesn't seem to be immediately available for warning CS0649. This is what currently shows up: enter image description here But on other errors I can suppress it as like below: enter image description here EDIT: I'm aware that I can set the field to = null; in classes, but structs can't take default initializers. I've updated the images to reflect this.

Firestar9114
  • 432
  • 4
  • 9
  • the simpler answer is, set values. Even if its =null, for gameobjects, this prevents the issue as you are asking to only suspress when its for a serialized field, not for anything else. In short the compiler cant see you set values in the inspector so complains. Setting a default value to the default value it would have had clears the message – BugFinder Jun 12 '19 at 10:07
  • @BugFinder This is great for classes, but you can't do this on structs. Thanks for pointing this out though, I've edited my question. – Firestar9114 Jun 12 '19 at 11:46
  • I've had this same problem, but it only affected about a dozen files, so i just enclosed them fully in #pragma warning disable / restore. You might want to rearrange your code so that you only have a handful of files that need to have these warnings disabled that way. – Ahorn Jun 12 '19 at 11:56
  • Interesting you mentioned structs, but your example doesnt show then the actual problem of the structs you are trying to show in the inspector – BugFinder Jun 12 '19 at 12:34
  • @BugFinder I didn't originally include the struct case because I didn't think it was directly relevant to context menu and I wanted to use a small and easy to reproduce example. The struct case requires more boiler plate and context to make sense. I have added it to the question for clarity though. The last example should now reflect the edge case with structs. – Firestar9114 Jun 12 '19 at 13:15
  • @WhiteMaple A lot of it is legacy code from past projects and code from asset packages. It's a lot to refactor, and I don't want to risk screwing something up in a purely cosmetic refactor if I can simply suppress the benign warnings. – Firestar9114 Jun 12 '19 at 13:18
  • Having the same problem here, found any solution yet ? – Jonathan Jul 25 '19 at 15:25

0 Answers0