This code produces in Visual Studio 2019 Information Message: *Severity Code Description Project File Line Suppression State Suppression State Detail Description Message IDE0059
Unnecessary assignment of a value to 'i'
Avoid unnecessary value assignments in your code, as these likely indicate redundant value computations. If the value computation is not redundant and you intend to retain the assignment, then change the assignment target to a local variable whose name starts with an underscore and is optionally followed by an integer, such as '_', '_1', '_2', etc. These are treated as special discard symbol names.*
The code snippet works OK, it's the message IDE0059, what bothers me. I don't want to suppress it, if is possible.
private static XmlDocument LoadXmlFromFile(string xmlPath)
{
XmlDocument doc = new XmlDocument();
int i = 2;
while (true)
{
try
{
using (Stream fileStream = System.IO.File.Open(xmlPath, FileMode.Open, FileAccess.Read, FileShare.None))
{
doc.Load(fileStream);
}
return doc;
}
catch (IOException) when (i > 0)
{
i--;
Thread.Sleep(100);
}
}
}
Whats wrong here? Is it false positive or I miss something?
This code also produces warning IDE0059 in VS2019:
private static XmlDocument LoadXmlFromFile(string xmlPath)
{
XmlDocument doc = new XmlDocument();
int i = 2;
while (true)
{
try
{
using (Stream fileStream = File.Open(xmlPath, FileMode.Open, FileAccess.Read, FileShare.None))
{
doc.Load(fileStream);
}
return doc;
}
catch (IOException)
{
if (i == 0)
{
throw;
}
i--;
Thread.Sleep(100);
}
}
}