Consider adding a ContractAnnotation attribute to LinqExtensions.IsNullOrEmpty<T>(this IList<T> list) to avoid a warning

Issue #792 new
Michael Ryan created an issue

I’m using Unity 2018.4.30 with Odin Inspector 3.0.3.0 (Editor Only) and JetBrains Rider 2021.1.5. on Windows 10 20H2.

In the following code sample, the usage of list on line 6 has a Possible ‘System.NullReferenceException’ warning generated by the code inspection.

private static Object TestMethod(IEnumerable<Object> targets)
{
   var list = targets?.ToList();
   if (!list.IsNullOrEmpty())
   {
      var count = list.Count;
   }

   return null;
}

Compare that to the inline test of both conditions where the usage of list on line 6 does not generate any warnings.

private static Object TestMethod(IEnumerable<Object> targets)
{
   var list = targets?.ToList();
   if (!((list == null) || (list.Count == 0)))
   {
      var count = list.Count;
   }

   return null;
}

I believe the Jetbrains.Annotations attributes have been embedded in all versions of Unity since 5.0.

Consider adding [JetBrains.Annotations.ContractAnnotation("null=>true", true)] to Sirenix.Utilities.LinqExtensions.IsNullOrEmpty<T>(this IList<T> list)to avoid the unnecessary “Possible NullReferenceException” warning on usages that follow the extension method call.

https://www.jetbrains.com/help/resharper/Contract_Annotations.html

Comments (2)

  1. Log in to comment