如何避免不相关的可为空的警告(无显式抑制) [英] How to avoid irrelevant nullable warning (without explicit suppression)

查看:64
本文介绍了如何避免不相关的可为空的警告(无显式抑制)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使分析仪理解变量 Bar 在以下情况下具有值?

  #nullable enable 
类Foo {
bool GenerateArray => Bar.HasValue;
int?吧{get;组; }
void FooBar(){
var data =(GenerateArray)吗? new int [Bar.Value]:null;
}
}

存在警告可为空的值类型可能为null。对于 Bar.Value ,但这显然不是。


我知道有两种方法可以避免发出警告。两者都有缺点:


  1. 直接使用 Bar.HasValue 而不是属性 GenerateArray 。但是,使用 GenerateArray 可以提高可读性。

  2. 使用 Bar!.Value 代替 Bar.Value 。但是,如果有人更改了代码(例如,将来通过使 GenerateArray 成为自动属性),则警告可能会再次变得相关,但不会出现。

此处的问题与此问题稍有不同,使用局部变量代替属性。如果我正确理解,以下可接受的答案(对于C#9发布)对属性有效,但对局部变量无效。因此,问题不是重复的。

解决方案

将能够使用 MemberNotNullWhen 属性(当前处于预览状态):

  [MemberNotNullWhen(true, Bar)] 
bool GenerateArray => Bar.HasValue;

.Net 5中将存在相关的属性类型:

 命名空间System.Diagnostics.CodeAnalysis 
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property,AllowMultiple = true)]
公共密封类MemberNotNullAttribute:属性
{
public MemberNotNullAttribute(params string [] member){}
public MemberNotNullAttribute(string member){}
}
}

命名空间系统。 Diagnostics.CodeAnalysis
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property,AllowMultiple = true)]
公共密封类MemberNotNullWhenAttribute:属性
{
public MemberNotNullWhenAttribute( bool when,params string [] member){}
public MemberNotNullWhenAttribute(bool when,string member){}
}
}

上sharplab

Is there a way to make the analyzer understand that the variable Bar has a value for the following case?

#nullable enable 
class Foo {
   bool GenerateArray => Bar.HasValue;
   int? Bar { get; set; }
   void FooBar() {
     var data = (GenerateArray) ? new int[Bar.Value] : null;
   }
}

There is the warning "Nullable value type may be null." for Bar.Value but it obviously can't be.

I am aware of two ways to avoid the warning. Both have disadvantages:

  1. Using Bar.HasValue directly instead of the property GenerateArray. However using GenerateArray improves readability.
  2. Using Bar!.Value instead of Bar.Value. However, if someone changes the code, for instance, by making GenerateArray an auto-property in the future, the warning may become relevant again, but won't appear.

The problem here slightly differs from this question, where a local variable was used instead of a property. The accepted answer below works (as soon as C# 9 is released) for the property but not for the local variable, if I understand it correctly. Hence, the question is not a duplicate.

解决方案

Will be be able to use the MemberNotNullWhen attribute in C# 9 (currently in preview):

[MemberNotNullWhen(true, "Bar")]
bool GenerateArray => Bar.HasValue;

The relevant attribute types will exist in .Net 5:

namespace System.Diagnostics.CodeAnalysis
{
    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true)]
    public sealed class MemberNotNullAttribute : Attribute
    {
        public MemberNotNullAttribute(params string[] members) { }
        public MemberNotNullAttribute(string member) { }
    }
}

namespace System.Diagnostics.CodeAnalysis
{
    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true)]
    public sealed class MemberNotNullWhenAttribute : Attribute
    {
        public MemberNotNullWhenAttribute(bool when, params string[] members) { }
        public MemberNotNullWhenAttribute(bool when, string member) { }
    }
}

Illustration on sharplab

这篇关于如何避免不相关的可为空的警告(无显式抑制)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆