[英]How can I solve unused code error in Visual Studio Code?
我在 Unity 中使用了 Visual Studio Code。
当我打开一个脚本时,Visual Studio Code 突出显示未使用的代码并显示错误。 我该如何解决这个问题?
(推荐:像以前一样标记为绿色下划线或禁用)
// Example code
int a = 3;
int b = 5;
Debug.LogFormat("{0}", b);
声明了变量“a”,但从未使用过 (CS0168)
通过 OP 的屏幕截图,我们可以看到混乱的来源。 CS0168 警告涉及以下行,在beforeItem
下带有红色波浪beforeItem
,这与问题中给出的示例代码明显不同:
ItemData beforeItem, currentItem;
使用原始问题的代码样式,可以使用以下代码生成此警告 - 请注意, a
已声明但从未赋值:
using System.Diagnostics;
namespace StackOverflow
{
public static class Program
{
public static void Main(string[] args)
{
int a;
int b = 5;
Debug.Print("{0}", b);
}
}
}
其中产生:
> csc Program.cs
Microsoft (R) Visual C# Compiler version 3.100.19.26603 (9d80dea7)
Copyright (C) Microsoft Corporation. All rights reserved.
Program.cs(9,17): warning CS0168: The variable 'a' is declared but never used
但是,原始问题中的示例基本上是这样的 - 请注意, a
被分配了值3
但随后未使用:
using System.Diagnostics;
namespace StackOverflow
{
public static class Program
{
public static void Main(string[] args)
{
int a = 3;
int b = 5;
Debug.Print("{0}", b);
}
}
}
其中产生:
> csc Program.cs
Microsoft (R) Visual C# Compiler version 3.100.19.26603 (9d80dea7)
Copyright (C) Microsoft Corporation. All rights reserved.
Program.cs(9,17): warning CS0219: The variable 'a' is assigned but its value is never used
于是混乱。
具有讽刺意味的是,这两个警告都是由Roslyn 编译器中的同一行抛出的:
private void ReportIfUnused(LocalSymbol symbol, bool assigned)
{
if (!_usedVariables.Contains(symbol))
{
if (symbol.DeclarationKind != LocalDeclarationKind.PatternVariable && !string.IsNullOrEmpty(symbol.Name)) // avoid diagnostics for parser-inserted names
{
Diagnostics.Add(assigned && _writtenVariables.Contains(symbol) ? ErrorCode.WRN_UnreferencedVarAssg : ErrorCode.WRN_UnreferencedVar, symbol.Locations[0], symbol.Name);
}
}
}
要修复 CS0168 警告,请删除a
或使用的定义:
#pragma warning disable 168
int a;
#pragma warning restore 168
要修复 CS0219 警告,请删除a
或使用的定义:
#pragma warning disable 219
int a = 3;
#pragma warning restore 219
要修复屏幕截图中给出的 CS0168 警告,请删除beforeItem
的定义或使用:
#pragma warning disable 168
ItemData beforeItem, currentItem;
#pragma warning restore 168
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.