False Positive w/ Pessimistic Value Analysis

When switching value analysis mode to "pessimistic" (Build 1609), I get a
false warning with the following code:

internal sealed class IndexedEnumerableDebugView collection)
    {
        if (collection == null)
            throw new ArgumentNullException("collection");
        _collection = collection;
    }

   
    public T[] Items
    {
        get
        {
            var items = new T[_collection.Count];
            // On the following line, R# incorrectly warns "_collection" may
be null.
            _collection.CopyTo(items, 0);
            return items;
        }
    }
}

Since "_collection" is a private readonly field in a sealed class, and
"Items" is not accessed before the field is assigned, the field cannot
possibly be null.  Further, a NullReferenceException would have already been
thrown by the previous statement when accessing "_collection.Count".

2 comments
Comment actions Permalink

Addendum: in this case, the "CopyTo" method is an extension method, and
the first parameter (the "this" parameter) is annotated :

public static void CopyTo( this IEnumerable source,
T[] destination, int destIndex) { ... }

"Mike Strobel" <mike.strobel@gmail.com> wrote in message
news:hkf3h2$cbu$1@nntp-server.labs.intellij.net...

When switching value analysis mode to "pessimistic" (Build 1609), I get a
false warning with the following code:

>

internal sealed class IndexedEnumerableDebugView<T>
{
   private readonly IIndexedEnumerable<T> _collection;

>

   public IndexedEnumerableDebugView(IIndexedEnumerable<T> collection)
   {
       if (collection == null)
           throw new ArgumentNullException("collection");
       _collection = collection;
   }

>

  
   public T[] Items
   {
       get
       {
           var items = new T[_collection.Count];
           // On the following line, R# incorrectly warns "_collection"
may be null.
           _collection.CopyTo(items, 0);
           return items;
       }
   }
}

>

Since "_collection" is a private readonly field in a sealed class, and
"Items" is not accessed before the field is assigned, the field cannot
possibly be null.  Further, a NullReferenceException would have already
been thrown by the previous statement when accessing "_collection.Count".


0
Comment actions Permalink

Addendum: in this case, the "CopyTo" method is an extension method, and
the first parameter (the "this" parameter) is annotated :

public static void CopyTo( this IEnumerable source,
T[] destination, int destIndex) { ... }

"Mike Strobel" <mike.strobel@gmail.com> wrote in message
news:hkf3h2$cbu$1@nntp-server.labs.intellij.net...

When switching value analysis mode to "pessimistic" (Build 1609), I get a
false warning with the following code:

>

internal sealed class IndexedEnumerableDebugView<T>
{
   private readonly IIndexedEnumerable<T> _collection;

>

   public IndexedEnumerableDebugView(IIndexedEnumerable<T> collection)
   {
       if (collection == null)
           throw new ArgumentNullException("collection");
       _collection = collection;
   }

>

  
   public T[] Items
   {
       get
       {
           var items = new T[_collection.Count];
           // On the following line, R# incorrectly warns "_collection"
may be null.
           _collection.CopyTo(items, 0);
           return items;
       }
   }
}

>

Since "_collection" is a private readonly field in a sealed class, and
"Items" is not accessed before the field is assigned, the field cannot
possibly be null.  Further, a NullReferenceException would have already
been thrown by the previous statement when accessing "_collection.Count".


0

Please sign in to leave a comment.