ReSharper:几个false" PossibleNullReferenceException" NotNull警告,CanBeNull处于悲观模式

时间:2018-06-14 11:09:21

标签: c# resharper static-analysis notnull

我使用ReSharper Ultimate 2018.1.2并且我有几个关于" PossibleNullReferenceException"的错误警告。

我使用悲观价值分析模式(在选项'代码检查'设置' /'价值分析模式':悲观),我定义[NotNull] ,只要我能,就可以[CanBeNull],[ItemNotNull]和[ItemCanBeNull]。

我准备了简化的程序代码,您可以在其中看到这些错误警告(如果您在选项中设置了悲观值分析模式)。在评论中搜索字符串" NotOK"。

using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;

namespace TestNotNull
{
    [NotNull] internal delegate Info FuncNotNull();
    [NotNull] internal delegate TResult FuncNotNull<out TResult>();
    internal delegate void ActionNotNull<in T>([NotNull] T x);

    internal class Info
    {
        [NotNull] public string Name => "TestApp";
    }

    internal class Program
    {
        [NotNull] public static FuncNotNull<Info> InfoFunc { get; } = () => Info;
        [NotNull] public static FuncNotNull InfoFunc2 { get; } = () => Info;
        [NotNull] public static Info Info { get; } = new Info();
        [NotNull, ItemNotNull] public static List<Info> InfoList { get; } = new List<Info>();
        [NotNull, ItemCanBeNull] public static List<Info> InfoCanBeNullList { get; } = new List<Info>();
        // NotOK: AnnotationRedundancyAtValueType of "ItemNotNull"
        [NotNull, ItemNotNull] public static IReadOnlyDictionary<Guid, Info> InfoDict { get; } = new Dictionary<Guid, Info>();
        [NotNull, ItemNotNull] public static Info[] InfoArray { get; } = new []{ new Info(), new Info() };

        private static void Main()
        {
            new Program().Run();
        }

        private void Run()
        {
            // NotOK: PossibleNullReferenceException of "InfoFunc()"
            Console.WriteLine("" + InfoFunc().Name);

            // NotOK: PossibleNullReferenceException of "InfoFunc2()"
            Console.WriteLine("" + InfoFunc2().Name);

            // NotOK: PossibleNullReferenceException of "InfoList.First()"
            Console.WriteLine("" + InfoList.First().Name);

            // NotOK: PossibleNullReferenceException of "x_"
            Console.WriteLine("" + InfoList.FindIndex(x_ => x_.Name == "found"));

            // NotOK: PossibleNullReferenceException of "InfoDict.Values.First()"
            Console.WriteLine("" + InfoDict.Values.First().Name);

            // NotOK: PossibleNullReferenceException of "InfoDict.First().Value"
            Console.WriteLine("" + InfoDict.First().Value.Name);

            // NotOK: PossibleNullReferenceException of "InfoList.ToDictionary(x_ => x_.Name, x_ => x_)["found"]"
            Console.WriteLine("" + InfoList.ToDictionary(x_ => x_.Name, x_ => x_)["found"].Name);

            // NotOK: PossibleNullReferenceException of "y_"
            Console.WriteLine("" + InfoList.Select(x_ => x_.Name).ToList().Select(y_ => y_.ToLower()));
            // This is OK: Console.WriteLine("" + InfoList.Select(x_ => x_.Name).Select(y_ => y_.ToLower()));

            // NotOK: PossibleNullReferenceException of "First(x_ => null != x_)"
            Console.WriteLine("" + InfoCanBeNullList.First(x_ => null != x_).Name);

            // NotOK: PossibleNullReferenceException of "ToArray()[0]"
            Console.WriteLine("" + InfoCanBeNullList.Where(x_ => null != x_).ToArray()[0].Name);

            // NotOK: PossibleNullReferenceException of "InfoArray[0]"
            Console.WriteLine("" + InfoArray.Where(x_ => x_.Name == InfoArray[0].Name));

            // NotOK: PossibleNullReferenceException of "InfoArray[0]"
            Console.WriteLine("" + InfoArray[0].Name);

            // NotOK: PossibleNullReferenceException of "System.Net.IPAddress.Any"
            Console.WriteLine("" + System.Net.IPAddress.Any.GetHashCode());

            Console.ReadLine();
        }
    }
}

请问,我怎样才能在悲观模式中避免这些错误警告?

我还在JetBrains支持论坛here中发布了我的问题。

1 个答案:

答案 0 :(得分:0)