[301] Incorrect Possible System.NullReferenceException warning

In the following example s2 is flagged with Possible
System.NullReferenceException. Hard as I tried I couldn't figure out the code
path that leads to s2 being null. Bug? I'm using build 301.

using System;

namespace ConsoleApplication
{
internal class MyClass {}

internal class Program
{
private static void Main( string[] args )
{
TestMethod( new MyClass(), false );
}

private static void TestMethod( object o, bool flag )
{
MyClass s2;

if ( flag )
{
s2 = new MyClass();
}
else
{
if ( o == null )
{
s2 = new MyClass();
}
else if ( (s2 = o as MyClass) == null )
{
throw new Exception( "exception" );
}
}

Console.WriteLine( s2.ToString() ); // s2 underlined as possible
NullReferenceException
}
}
}

3 comments
Comment actions Permalink

reply.to.usenet@nospam.com schrieb:

In the following example s2 is flagged with Possible
System.NullReferenceException. Hard as I tried I couldn't figure out the code
path that leads to s2 being null. Bug? I'm using build 301.

private static void TestMethod( object o, bool flag )
{
MyClass s2;

if ( flag )
{
s2 = new MyClass();
}
else
{
if ( o == null )
{
s2 = new MyClass();
}
else if ( (s2 = o as MyClass) == null )
{
throw new Exception( "exception" );
}
}

Console.WriteLine( s2.ToString() ); // s2 underlined as possible
NullReferenceException
}
}
}


Yes, but it's a complicated code, even hard to readfor humans.

Keep is Simple.

private static void TestMethod(object o, bool flag)
{
MyClass s2;

if (flag)
{
s2 = new Label();
}
else
{
s2 = (o ?? new MyClass ()) as MyClass;
if (s2 == null)
throw new Exception("exception");
}

Console.WriteLine(s2.ToString());
}

Same function, IMHO easier to read, and no warning.

Regards

Albert
http://der-albert.com

0
Comment actions Permalink

On Thu, 16 Nov 2006 23:16:38 +0100, Albert Weinert <newsH2-06@der-albert.com>
wrote:

>
>Yes, but it's a complicated code, even hard to readfor humans.
>
>Keep is Simple.

I think you are missing the point. This was not example of how to code right,
but shortest snippet of code that illustrates the bug. It allows Resharper
developers to load it into Visual Studio, reliably replicate bug and fix it.

0
Comment actions Permalink

Thank you for the report.
This issue will be fixed shortly

--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
<reply.to.usenet@nospam.com> wrote in message
news:gonpl2t8lcdomqf5fuo217lep63elnpk5k@4ax.com...

In the following example s2 is flagged with Possible
System.NullReferenceException. Hard as I tried I couldn't figure out the
code
path that leads to s2 being null. Bug? I'm using build 301.

>

using System;

>

namespace ConsoleApplication
{
internal class MyClass {}

>

internal class Program
{
private static void Main( string[] args )
{
TestMethod( new MyClass(), false );
}

>

private static void TestMethod( object o, bool flag )
{
MyClass s2;

>

if ( flag )
{
s2 = new MyClass();
}
else
{
if ( o == null )
{
s2 = new MyClass();
}
else if ( (s2 = o as MyClass) == null )
{
throw new Exception( "exception" );
}
}

>

Console.WriteLine( s2.ToString() ); // s2 underlined as
possible
NullReferenceException
}
}
}



0

Please sign in to leave a comment.