Resharper 在下面的 InitializeObjects() 中标记了两个“代码冗余”:
public string InitializeObjects()
{
string RetVal = null; // <-- Value assigned is not used in any execution path
try
{
dbConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;User ID=PlatypusBO;Password=PlatypusBO;Data Source="
+ Database + ";Persist Security Info=False;Jet OLEDB:System database=" + Workgroup);
dbConnection.Open();
RetVal = FastLookup(ref dbConnection, "setting_value", "t_settings", "key_setting='TableVersion'").ToString();
if((RetVal == "") || (RetVal == null)) // <- Resharper says, "Expression is always false"
RetVal = "0.0.0.0";
}
catch(Exception)
{
RetVal = null;
}
return RetVal;
}
private object FastLookup(ref System.Data.OleDb.OleDbConnection tConn, string fldName, string tName, string strFilter)
{
if (tConn == null)
{
return "";
}
object RetVal = "";
string sqlCriteria = "";
if (strFilter != "")
{
sqlCriteria = " WHERE " + strFilter;
}
if (tConn.State != System.Data.ConnectionState.Open)
{
tConn.Open();
}
System.Data.OleDb.OleDbCommand catCMD = tConn.CreateCommand();
catCMD.CommandText = "SELECT " + fldName + " FROM " + tName + sqlCriteria;
try
{
using (System.Data.OleDb.OleDbDataReader myReader = catCMD.ExecuteReader())
{
if (myReader != null && myReader.Read())
{
RetVal = myReader.IsDBNull(0) ? "" : myReader.GetValue(0);
}
else
{
RetVal = "";
}
}
//myReader.Close();
}
catch (Exception)
{
RetVal = "";
}
finally
{
catCMD.Dispose();
}
return RetVal;
}
...我不知道为什么;似乎第一个,“分配的值未在任何执行路径中使用”是不正确的,因为有一个后续测试来查看 RetVal 是否为空。
第二个(“表达式始终为假”)对我来说也没有意义,因为 RetVal 可以是这些 val 之一(空字符串或 null),因为它在代码中的两个点都分配了。
我 (99.9999%) 确定 R# 是正确的,但为什么它是正确的?
更新
将其更改为此枪口 R#:
public string InitializeObjects()
{
string RetVal;
try
{
dbConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;User ID=DuckbilledPBO;Password=DuckbilledPBO;Data Source="
+ Database + ";Persist Security Info=False;Jet OLEDB:System database=" + Workgroup);
dbConnection.Open();
RetVal = FastLookup(ref dbConnection, "setting_value", "t_settings", "key_setting='TableVersion'");
if (string.IsNullOrEmpty(RetVal))
RetVal = "0.0.0.0";
}
catch(Exception)
{
RetVal = null;
}
return RetVal;
}
private string FastLookup(ref System.Data.OleDb.OleDbConnection tConn, string fldName, string tName, string strFilter)
{
if (tConn == null)
{
return "";
}
string RetVal;
string sqlCriteria = "";
if (strFilter != "")
{
sqlCriteria = " WHERE " + strFilter;
}
if (tConn.State != System.Data.ConnectionState.Open)
{
tConn.Open();
}
System.Data.OleDb.OleDbCommand catCMD = tConn.CreateCommand();
catCMD.CommandText = "SELECT " + fldName + " FROM " + tName + sqlCriteria;
try
{
using (System.Data.OleDb.OleDbDataReader myReader = catCMD.ExecuteReader())
{
if (myReader != null && myReader.Read())
{
RetVal = myReader.IsDBNull(0) ? "" : myReader.GetValue(0).ToString();
}
else
{
RetVal = "";
}
}
}
catch (Exception)
{
RetVal = "";
}
finally
{
catCMD.Dispose();
}
return RetVal;
}