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:
But on other errors I can suppress it as like below:
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.