8

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);
            }
        }
    }
Christopher Klein
  • 2,773
  • 4
  • 39
  • 61
Ive
  • 1,321
  • 2
  • 17
  • 25
  • it seems to be moaning because unless there is an error, i is never used. – BugFinder Nov 15 '19 at 07:42
  • What do you expect to happen once `i` is `0` and you get another exception? – Herohtar Nov 15 '19 at 07:42
  • replace the when with an if statement in the catch... The while loop is not necessary, since you are returning the first succesfully loaded file. i think it would be easier to just use a for() loop. – Glenn van Acker Nov 15 '19 at 07:43
  • The reason you're getting that warning is because it's a false positive. The compiler is probably ignoring the i in the catch statement. that's why it's better to just use a for loop, and remove the i from the catch statement. you can still let the thread sleep in that catch statement. – Glenn van Acker Nov 15 '19 at 07:50
  • I want only catch IOExceptions when i>0, all other exceptions schuld be throwen – Ive Nov 15 '19 at 07:50
  • You can just catch IOExceptions first, and add another catch for other exceptions. but that's not even necessary. if you only want to catch them when i > 0, just let it count down to 1, and then end the loop. or are you expecting i to go negative? – Glenn van Acker Nov 15 '19 at 07:52
  • Im expecting when i==0 to throw IOExcetion, all other Exceptions schould be thrown immediately – Ive Nov 15 '19 at 07:58
  • Then you can just use a for loop, with a try catch inside. and when that for loop ended, you just throw an IOException. – Glenn van Acker Nov 15 '19 at 08:01

2 Answers2

3

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;
    }

}
Jack J Jun
  • 5,633
  • 1
  • 9
  • 27
  • I don't have problem with code, it's seems to work OK what I know, but that visual studio produces this message IDE0059 bothers me. I don't want only suppress it, I want to make it right – Ive Nov 18 '19 at 07:15
  • I don't have that warning, so please check it again. – Jack J Jun Nov 18 '19 at 07:38
  • This code does not produces IOException when i == 0, i need to throw Exception when i == 0. – Ive Nov 20 '19 at 07:58
  • I have updated my code, you could have a look to see if it can solve your problem. – Jack J Jun Nov 21 '19 at 01:57
  • Ok thanks, this, works :), you can use any condition which is true and the message is not there. – Ive Nov 21 '19 at 07:55
  • 1
    @Ive Don't do `throw ex` as it will [reset your call stack](https://stackoverflow.com/a/881489/1219414). Your original code was OK. I wouldn't refactor my code just to please a bugged compiler. I'm having a similar issue and applying VS recommended changes breaks my code. – Juan Feb 22 '20 at 17:07
0

Message IDE0059 is indicating the assignment of "int i = 2;" is unnecessary. To save the computation, use "int i;". With i now starting at the default of zero (0) and decreasing with "i--;", also change the comparisons to "(i > -2)" and "(i == -2)" per the code examples.

Rocco
  • 11
  • 1
    `i` is a local variable, so it must be explicitly assigned to before being used, or else there is a compiler error. It does not default to 0. – Joe Sewell Jan 22 '20 at 20:17
  • Bunch of warnings for this and it is so annoying. I can add a suppression file entry for each file, but I can't find the warning to turn this off. I don't want any suggestions for my code. I want it to compile, or not compile. –  Jul 07 '20 at 22:22