代码检查:可能违反 'ValueRange'/'NonNegativeValue' 属性
此检查报告了一个可能违反 [ValueRange] 或 [NonNegativeValue] 约定的整数值。 ReSharper 通过 控制流分析 跟踪整数值范围,并在赋值或参数超出允许区间时发出警告。
解决方法是更改该值或相关逻辑,使所赋值始终在声明的范围内。
示例
using JetBrains.Annotations;
class C
{
[NonNegativeValue]
public int Bar { get; set; }
public void M()
{
Bar = -1;
}
}
using JetBrains.Annotations;
class C
{
[NonNegativeValue]
public int Bar { get; set; }
public void M()
{
Bar = 0;
}
}
2026年 5月 8日