"Variable is assigned but its Value is never used" Warning
It’s late, but here’s a little humor for ‘ya.
I’ve been noticing recently that I’ve been getting some odd warnings in Visual Studio for variables that are defined and never used. Check out the following screen shot from C# code editor:
Notice how the compiler is warning about isSelected not being used, but as you can see just a few lines down it is in fact being used in an assignment.
At first I thought that maybe the compiler knows something I don’t know and figures the code in the if block never runs, but even if I add the following code:
bool isSelected = false; if (true) isSelected = false;
I still get the compiler warning which clearly should serve as an assignment. Then I tried the direct route (which should have been step 1, right? <g>):
bool isSelected = false; isSelected = true;
and EVEN THAT still gives me the warning. So WTF is going on here?
I changed the name of the variable to isPageSelected. No change.
Finally I tried replacing the static bool value with some dynamic value and that finally got rid of the warning:
bool isPageSelected = false; isPageSelected = this._Tabs.Count % 1 == 0;
of course that’s non-sensical but it appears that the problem is caused by the compiler getting confused by the constant value assignments to the bool variable.
But then the proverbial light bulb goes off and the hand smacks the forehead: The compiler IS in fact smarter than me! The reason the warning pops up is not because the variable is never assigned, but because it is in fact never used for any sort of comparison in the rest of the method’s code. If I had read the damn message properly in the first place I would have noticed the problem, but no… I got hung up on the first use of the variable.
Technically though the last example I posted really doesn’t change the nature of the behavior and it should also show the same warning, so maybe there is a bug in there after all (yeah I know I’m reaching now).
Been there done that, thank you very much
Anybody else ever done something like this? You are absolutely, 100% sure the compiler is wrong, is out to get you, and you want to – you know do something mean. And then you realize: nope my own fault. As my good pal Peter Bromberg always says – 90% of the time it’s not a problem with the framework or the tool you are working with but rather operator error. Before going off on a ‘bug’ or ‘failure’ take the time to make sure you actually look at it from a few angles.
And yes in case you’re wondering – this post started out as a nag about a problem and a question to ask about why this might be. In this respect blogging can be highly useful to make you re-examine some assumptions and look at problems more closely trying to explain what might be going on, which in this case found my own – ahem failure to read the error message correctly. You win some you lose some…
The Voices of Reason
# re: "Variable is assigned but its Value is never used" Warning
# re: "Variable is assigned but its Value is never used" Warning
# re: "Variable is assigned but its Value is never used" Warning
When I first stumbled across this before, I was also annoyed and thought it must be a bug. Then I read the error message again and realized it was right. Apparently, initializing your variables causes warnings. :)
# re: "Variable is assigned but its Value is never used" Warning
# re: "Variable is assigned but its Value is never used" Warning
"Always believe the error message."
It was amazing how much time and effort is wasted on what I *thought* was the problem, only to go back and re-read the message to find I made an assumption, or mis-read the message, and off I go onto my own tangent that had absolutely nothing to do with the actual problem.
Where as if you follow Rule #1, and internalize the message in your head, you may find yourself grinding the gears less often.
# re: "Variable is assigned but its Value is never used" Warning
I was just talking with a friend of mine about some young developers we know who seem to ALWAYS assume it is the framework, compiler, or whatever..anything but them.
I tend to error on the other end. Or at least I'd like to think I do. For at least the first four hours I assume it is me who is wrong.
Programmers must be the either the craziest people on the planet or the ones with the highest self esteem. Here we are working with an inanimate object who we can't argue with and it tells us all day, "Hey buddy, you did something wrong here."
# re: "Variable is assigned but its Value is never used" Warning
Thank you for this. This interplay exemplifies the point I was trying to get across in my last blog post. (http://pinvoke.wordpress.com/2009/02/28/trust-the-process/)
People are often way to willing to blame something else other than themselves. If you feel like you are working too hard for something... you are probably right. The folks who created the language you program in were not stupid people... but it doesn't mean they get it right all the time. Great faith and great doubt... that's how we learn.
-A
# re: "Variable is assigned but its Value is never used" Warning
It happend to me to spend 10 minutes finding out why a variable was never used... you write a != instead of == or use the wrong variable in a loop / if combination and you're screwed.
But it's very helpfull that those tools can find it... we would never notice it alone before the bug happens in the wild !
# re: "Variable is assigned but its Value is never used" Warning
This warning means You WRITE to variable but NEVER READ from it! In your code screenshot you WRITE to it TWICE but NEVER READ...
# re: "Variable is assigned but its Value is never used" Warning
bool isPageSelected = false; isPageSelected = this._Tabs.Count % 1 == 0;
which isn't flagged with the same warning although it probably should.
# re: "Variable is assigned but its Value is never used" Warning
# re: "Variable is assigned but its Value is never used" Warning
# re: "Variable is assigned but its Value is never used" Warning
[StoredProcedureAttribute("sp_SavePerson")]
void SavePerson(PersonEntity person)
{
orm_savePerson(person);
}
Here my ORM used the attribute from the previous call to tell it which stored procedure to use - essentially allowing the business process tier to tell the ORM to use a replacement stored proc. Now instead of simply passing it as a parameter I decided to get clever and have it as an attribute, because then I could do reflection based dependency analysis between the database and the .net tier for future impact analysis of changes. There are on balance much better ways of doing this.
I forgot about inlining. Caused me days of wondering why, when I got into my orm_savePerson function, the call stack didn't have a function with the StoredProcedureAttribute on it. Of course when you're debugging in the IDE inlining doesn't happen - so took me some while to work out what was going wrong.
We live and learn.
# re: "Variable is assigned but its Value is never used" Warning
# re: "Variable is assigned but its Value is never used" Warning
int a = 0;
int b = a; // you won't get any warning
An ms connect entry where ms suggest to use fxcop...
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=362970
# re: "Variable is assigned but its Value is never used" Warning
The variable 'VariableName' is declared but never used.
... when a variable is never assigned a value. It's sort of nice VS gives these warnings for those rare times when you made a logical error or forgot to do something with a variable.
# re: "Variable is assigned but its Value is never used" Warning
# re: "Variable is assigned but its Value is never used" Warning
Events are also an interesting one - if you declare an event but then fail to internally implement any code that actually invokes the event (which in theory could be the task of a subclass - so this isn't entirely as clear cut as the code above).
Re: The compiler never lies. I wouldn't go that far, but most of the time it's a good idea to blame yourself before the compiler however hard on our egos, eh?
# re: "Variable is assigned but its Value is never used" Warning
# re: "Variable is assigned but its Value is never used" Warning
# re: "Variable is assigned but its Value is never used" Warning
#1. The compiler is giving a spurious warning message that provides no value whatsoever.
#2. This happens in VS2008 intermittently for no obvious or good reason. Sometimes it generates a warning but other similar code elsewhere does NOT generate a warning. It is inconsistent and thus can and should be considered a bug.
#3. This problem wastes people's time. I like my code to compile without any warnings. This helps me develop reliable high quality code. I have to add useless code to get rid of this warning. This is a waste. There's nothing good or useful about this.
#4. The thinking that the compiler is ALWAYS right is defective I think. Compilers can have bugs. I've encountered a few in my time. Yes, most of the time it is a user/developer problem but what people are really trying to say is that they want to believe 100% in their tools, in this case Visual Studio. That they feel comfortable when they can trust 100% in their tools. That's understandable but a complacent attitude. Especially when dealing with buggy Microsoft libraries and tools. I have found bugs in many Microsoft products. Some that Microsoft will not fix. It seems to be getting worse now. To "change your thinking" that all is right with the world (i.e. Visual Studio) when it's not is unrealistic. Just be realistic and call it a stupid bug or at least a quirk.
- Grant
# re: "Variable is assigned but its Value is never used" Warning
Yes, it's possible the compiler can be wrong but it is VERY rare and usually well documented if it is. When it comes down to us or the compiler being wrong I think you will find in most cases it'll be us who are wrong.
Of course, if you're arrogant enough to think otherwise - be my guest. A world of pain awaits you :-}
# re: "Variable is assigned but its Value is never used" Warning
# re: "Variable is assigned but its Value is never used" Warning
# re: "Variable is assigned but its Value is never used" Warning
# re: "Variable is assigned but its Value is never used" Warning
# re: "Variable is assigned but its Value is never used" Warning
#pragma warning disable 169, 414
private string col2 = null;
private string col3 = null;
#pragma warning restore 169, 414
# re: "Variable is assigned but its Value is never used" Warning