12

I have the following C# code, I'm using VS 2012 and all my warnings are treated as errors (although this probably doesn't have much relevance).

private static readonly int MAX_RADIUS_KM = 16;

private void Test() {
    int i = 2 * MAX_RADIUS_KM;
    int i2 = 2;
}

"The variable [x] is assigned, but its value is never used" is shown for i2, but not for i. Why? Is this a bug or something's happening behind the scenes? I'd be surprised by the latter, but if so, what's happening that makes i to avoid that warning?

The following also exhibits failure to emit warning for unused i:

private void Test(int foo) {
    int i = 2 * foo;
    int i2 = 2;
}

And another (i2 in this case):

private void Test()
{
  int i = 2 * 3;
  int i2 = i;
}
leppie
  • 115,091
  • 17
  • 196
  • 297
async
  • 1,537
  • 11
  • 28
  • I would say it is a bug. What version of VS are you using? And is it vanilla? My version of VS (11.0.61030.00 Update 4) with Reshaper (7.1.3000.2254) says that both are never used. – Jonny Jul 11 '14 at 10:57
  • same in VS2103 ultimate – Charlie Jul 11 '14 at 10:58
  • @Jonny I'm using the very same version of VS you are using, but without Resharper. – async Jul 11 '14 at 10:58
  • This only fails to warn in the case where `readonly` is present and when a parameter is used. Warns with `const` and other cases. – leppie Jul 11 '14 at 11:07
  • 2
    @PatrickHofman Why do you think that? This question is not about FIELDS. It's about local variables. Am I missing something? (tbh I didn't have enough time to study the other question. There MAY be things in common, but that doesn't make the questions identical) – async Jul 11 '14 at 11:28
  • It describes the same problem and I think the answer is the same too. It isn't an exact duplicate, but I think it is close enough. – Patrick Hofman Jul 11 '14 at 11:29
  • For the record: replacing `static int` by `const` fixes the issue – ken2k Jul 11 '14 at 12:09

0 Answers0