移除不必要的
| 房產 | 價值觀 |
|---|---|
| 規則識別碼 | IDE0380 |
| 標題 | 刪除不必要的 unsafe 修飾符 |
| 類別 | Style |
| 子類別 | 不必要的程式碼規則 (修飾符偏好設定) |
| 適用語言 | C# |
| 選項 | None |
概觀
此規則會識別程式碼區塊、方法、類型或其他標示 unsafe 為修飾符的宣告,這些宣告實際上不包含任何不安全的作業。
unsafe 修飾符允許使用指標和其他不安全的程式碼特性,但當未使用這些特性時,修飾符是不必要的應移除,以提高程式碼的清晰度。
Example
// Code with violations.
// Unnecessary, no unsafe operations.
unsafe class MyClass
{
public void Method()
{
var x = 5;
}
}
// Unnecessary, no unsafe operations.
unsafe void ProcessData(int value)
{
Console.WriteLine(value);
}
// Fixed code.
class MyClass
{
public void Method()
{
var x = 5;
}
}
void ProcessData(int value)
{
Console.WriteLine(value);
}
// Example where 'unsafe' is needed.
unsafe class ValidUsage
{
int* pointer; // Pointer type requires 'unsafe'.
}
隱藏警告
如果您想要只隱藏單一違規,請將預處理器指示詞新增至原始程式檔以停用,然後重新啟用規則。
#pragma warning disable IDE0380
// The code that's violating the rule is on this line.
#pragma warning restore IDE0380
若要停用檔案、資料夾或項目的規則,請在組態檔
[*.{cs,vb}]
dotnet_diagnostic.IDE0380.severity = none
若要停用所有程式碼樣式規則,請將類別 Style 的嚴重性設定為 組態檔中的 none。
[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-Style.severity = none
如需詳細資訊,請參閱 如何隱藏程式碼分析警告。