【问题标题】:ReSharper: several false "PossibleNullReferenceException" warnings with NotNull, CanBeNull in pessimistic modeReSharper:在悲观模式下,NotNull、CanBeNull 出现几个错误的“PossibleNullReferenceException”警告
【发布时间】:2018-11-24 03:39:23
【问题描述】:

我使用 ReSharper Ultimate 2018.1.2,我收到了几个关于“PossibleNullReferenceException”的错误警告。

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

我准备了简化的程序代码,您可以在其中看到这些错误警告(如果您在选项中设置了悲观值分析模式)。在 cmets "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 上发布了我的问题。

【问题讨论】:

    标签: c# resharper static-analysis notnull


    【解决方案1】:

    【讨论】:

    • 这与其他事情有关。这个问题的主题是 ReSharper 的警告:可能的“System.NullReferenceException”,这与命名规则无关。无论如何感谢您的努力、时间和回答。
    猜你喜欢
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-03
    • 1970-01-01
    相关资源
    最近更新 更多