Bug in Dead Code Detection?

I have found a possible issue with the dead code detection.  Given the following code:

<code>

using(var connection = new SqlConnection(connectionString))

{

     connection.Open();

     using(var transaction = connection.BeginTransaction())

     {

         SqlHelper.ExecuteNonQuery(transaction, procedureName, parameters);

         transaction.Commit;

         return true;

     }

}

return false;

</code>

the final line is marked as unreachable - which is false.  If any exceptions happen inside of either <c>using</c> expression, then the last line will be reached.  Only in the case where no exceptions happen is it unreachable.

1 comment
Comment actions Permalink

Hello David,

The code sample


using(var connection = new SqlConnection(connectionString))
{
connection.Open();
using(var transaction = connection.BeginTransaction())
{
SqlHelper.ExecuteNonQuery(transaction, procedureName, parameters);
transaction.Commit;
return true;
}
}

return false;



is actually transformed by the compiler into


SqlConnection connection;
try
{
connection = new SqlConnection(connectionString))
connection.Open();
using(var transaction = connection.BeginTransaction())
{
SqlHelper.ExecuteNonQuery(transaction, procedureName, parameters);
transaction.Commit;
return true;
}
}
finally
{
if(connection != null)
connection.Dispose();
}

return false;



According to C# Language Specification the try/finally block re-throws the
exception after executing the 'finally' block, so the execution will never
reach the last return statement. Thank you!


Andrey Serebryansky
Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

I have found a possible issue with the dead code detection.  Given the
following code:

<code>
using(var connection = new SqlConnection(connectionString))
{
connection.Open();
using(var transaction = connection.BeginTransaction())
{
SqlHelper.ExecuteNonQuery(transaction, procedureName,
parameters);
transaction.Commit;
return true;
}
}
return false;
</code>
the final line is marked as unreachable - which is false.  If any
exceptions happen inside of either <c>using</c> expression, then the
last line will be reached.  Only in the case where no exceptions
happen is it unreachable.

---
Original message URL:
http://devnet.jetbrains.net/message/5269228#5269228



0

Please sign in to leave a comment.