Bug: Incorrect warnings (expression always true/false)

Hi,

if you have a simple loop like this..

object best = null;
foreach( object candidate in candidates )
{
bool isBetter = best == null || candidate.IsBetter( best );
if( isBetter )
{
best = candidate;
}
}

..then R# will incorrectly say that "best == null" is always true.

Yours,
Morten

3 comments
Comment actions Permalink

Hello Morten,

Could you please provide complete example? System.Object doesn't have IsBetter
member, and changing type to some artifical class having this member doesn't
show any "always true" warning.

Please also tell us what version of ReSharper do you use.

Sincerely,
Ilya Ryzhenkov

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


MM> object best = null;
MM> foreach( object candidate in candidates )
MM> {
MM> bool isBetter = best == null || candidate.IsBetter( best );
MM> if( isBetter )
MM> {
MM> best = candidate;
MM> }
MM> }


0
Comment actions Permalink

Surely you don't need the IsBetter implementation. In the first pass through the loop, best==null is true, so candidate.IsBetter is not called and isBetter is true so the best=candidate assignment always occurs in the first pass through the loop.

0
Comment actions Permalink

I cannot reproduce the problem.
Please could you provide small complete example

--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Morten Mertner" <morten@mertner.com> wrote in message
news:elpn0k$kib$1@is.intellij.net...

Hi,

>

if you have a simple loop like this..

>

object best = null;
foreach( object candidate in candidates )
{
bool isBetter = best == null || candidate.IsBetter( best );
if( isBetter )
{
best = candidate;
}
}

>

..then R# will incorrectly say that "best == null" is always true.

>

Yours,
Morten



0

Please sign in to leave a comment.