Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IDE0059 Unnecessary assignment of a value to 'i'

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);
            }
        }
    }
like image 295
Ive Avatar asked Nov 15 '19 07:11

Ive


1 Answers

According to your description, it seems that you want to end the sleep when you go through

two exceptions without throwing the warning.

I suggest that you can use if sentence to do it.

class Program
{
    static void Main(string[] args)
    {
        string path = "D:\\teest1.xml";
        var doc = LoadXmlFromFile(path);
    }
    private static XmlDocument LoadXmlFromFile(string xmlPath)
    {
        XmlDocument doc = new XmlDocument();
        int i = 2;
        while (i>=0)              // change the while sentence
        {
            try
            {
                using (Stream fileStream = System.IO.File.Open(xmlPath, FileMode.Open, FileAccess.Read, FileShare.None))
                {
                    doc.Load(fileStream);
                }

                return doc;
            }
            catch (IOException ex)
            {             
                if (i == 0)
                {
                    throw ex;
                }

                i--;
                Thread.Sleep(200);
            }

        }
        return doc;
    }

}
like image 77
Jack J Jun - MSFT Avatar answered Nov 14 '22 19:11

Jack J Jun - MSFT