www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - What happens with unused variables?

reply Tiago Gasiba <tiago.gasiba gmail.com> writes:
"dmd -w" on  the following code should produce a warning like "unused variable
ii" or similar.
Usually helps a lot :)

<snip>
int main(){
  int ii;

  return 0;
}
<snip>

Best,
Tiago

-- 
Tiago Gasiba (M.Sc.) - http://www.gasiba.de
Everything should be made as simple as possible, but not simpler.
Dec 05 2005
parent reply "Walter Bright" <newshound digitalmars.com> writes:
"Tiago Gasiba" <tiago.gasiba gmail.com> wrote in message
news:dn1611$grl$1 digitaldaemon.com...
 "dmd -w" on  the following code should produce a warning like "unused
variable ii" or similar.
 Usually helps a lot :)

 <snip>
 int main(){
   int ii;

   return 0;
 }
 <snip>
This isn't a bug, it's by design <g>. Warnings about unused variables can get rather annoying when they're intended.
Dec 05 2005
next sibling parent reply Derek Parnell <derek psych.ward> writes:
On Mon, 5 Dec 2005 10:00:15 -0800, Walter Bright wrote:

 "Tiago Gasiba" <tiago.gasiba gmail.com> wrote in message
 news:dn1611$grl$1 digitaldaemon.com...
 "dmd -w" on  the following code should produce a warning like "unused
variable ii" or similar.
 Usually helps a lot :)

 <snip>
 int main(){
   int ii;

   return 0;
 }
 <snip>
This isn't a bug, it's by design <g>. Warnings about unused variables can get rather annoying when they're intended.
And can get extremely annoying when they're *not* intended. -- Derek (skype: derek.j.parnell) Melbourne, Australia "A learning experience is one of those things that says, 'You know that thing you just did? Don't do that.'" - D.N. Adams 6/12/2005 12:05:08 PM
Dec 05 2005
parent Tiago Gasiba <tiago.gasiba gmail.com> writes:
Derek Parnell schrieb:

 On Mon, 5 Dec 2005 10:00:15 -0800, Walter Bright wrote:
 
 "Tiago Gasiba" <tiago.gasiba gmail.com> wrote in message
 news:dn1611$grl$1 digitaldaemon.com...
 "dmd -w" on  the following code should produce a warning like "unused
variable ii" or similar.
 Usually helps a lot :)

 <snip>
 int main(){
   int ii;

   return 0;
 }
 <snip>
This isn't a bug, it's by design <g>. Warnings about unused variables can get rather annoying when they're intended.
And can get extremely annoying when they're *not* intended.
How about a switch in the compiler? Two proposals: 1) Something like -wv (warn on unused variables) ? IMHO the best solution 2) Several warning levels like -w1 - normal warnings -w2 - normal + unused variables -w3 - something else? Not very clean solution though... Best, Tiago -- Tiago Gasiba (M.Sc.) - http://www.gasiba.de Everything should be made as simple as possible, but not simpler.
Dec 06 2005
prev sibling parent reply Bruno Medeiros <daiphoenixNO SPAMlycos.com> writes:
Walter Bright wrote:
 "Tiago Gasiba" <tiago.gasiba gmail.com> wrote in message
 news:dn1611$grl$1 digitaldaemon.com...
 
"dmd -w" on  the following code should produce a warning like "unused
variable ii" or similar.
Usually helps a lot :)

<snip>
int main(){
  int ii;

  return 0;
}
<snip>
This isn't a bug, it's by design <g>. Warnings about unused variables can get rather annoying when they're intended.
Why is it that Digital Mars compiler's option to enable warnings does not only warning-notification but also error-on-warning? And there isn't even a way to do just the first? etc., and maybe some more I don't recall) the warning semantics are all the same: by default "notify" about warnings, and only with an extra option do the warnings cause a compilation error/stop. This is what makes more sense in terms of nomenclature, because if a "warning" if allways an error, then it is not a "warning", it is a... well, "prohibition" or "order" or something... But more importantly, it is also what makes more sense in terms of programming/development, because it is more useful to have no-error warnings, allways enabled. Otherwise, no wonder one finds DM's warnings about unused variable annoying (as well as other warnings), since they halt compilation in unwanted situations. The end result is the coder rarely compiling with -w , is it not? I do agree that some of the traditional warnings (specially in the C/C++ world), such as use-of-uninitialized-variable, could be treated allways as errors, as they probably signify wrong code. However, some other types of warnings, such as unused-variable, unreachable-code, etc., are pretty much innocuous, and they occur frequently and *naturally* during developing/testing, where often there is incomplete code. That's why it is a royal pain to have error-on-warning enabled as such. -- Bruno Medeiros - CS/E student "Certain aspects of D are a pathway to many abilities some consider to be... unnatural."
Dec 06 2005
next sibling parent reply Derek Parnell <derek psych.ward> writes:
On Wed, 07 Dec 2005 01:43:36 +0000, Bruno Medeiros wrote:

That's why it is a royal pain to have 
 error-on-warning enabled as such.
I believe that Walter is philosophically opposed to the concept of 'warning'. Basically, if something is worth warning you about, it is worth fixing. So in effect there is nothing different between a warning and an error. I do not support this philosophical opinion. I tend to think that giving more freedom of expression to coders is better than nannying them. In other words, I think we should let coders choose the type of dangers they are willing to be responsible for. Give coders warnings, if that's what they ask for, and keep quiet, if that's what they ask for. And only issue an error if the compiler cannot achieve a valid object code output. A compiler is a tool to assist coders. -- Derek (skype: derek.j.parnell) Melbourne, Australia "A learning experience is one of those things that says, 'You know that thing you just did? Don't do that.'" - D.N. Adams 7/12/2005 1:53:05 PM
Dec 06 2005
next sibling parent reply John Reimer <terminal.node gmail.com> writes:
Derek Parnell wrote:
 On Wed, 07 Dec 2005 01:43:36 +0000, Bruno Medeiros wrote:
 
 That's why it is a royal pain to have 
 error-on-warning enabled as such.
I believe that Walter is philosophically opposed to the concept of 'warning'. Basically, if something is worth warning you about, it is worth fixing. So in effect there is nothing different between a warning and an error. I do not support this philosophical opinion. I tend to think that giving more freedom of expression to coders is better than nannying them. In other words, I think we should let coders choose the type of dangers they are willing to be responsible for. Give coders warnings, if that's what they ask for, and keep quiet, if that's what they ask for. And only issue an error if the compiler cannot achieve a valid object code output. A compiler is a tool to assist coders.
I think we're just lucky to have warnings in DMD period. Hell practically DID freeze over when Walter implemented warnings into his compiler. Any further disparagement about the implementation might see them removed altogether. ;-) -JJR
Dec 06 2005
next sibling parent Derek Parnell <derek psych.ward> writes:
On Tue, 06 Dec 2005 19:16:38 -0800, John Reimer wrote:

 I think we're just lucky to have warnings in DMD period.  Hell 
 practically DID freeze over when Walter implemented warnings into his 
 compiler.
 
 Any further disparagement about the implementation might see them 
 removed altogether. ;-)
Totally agree. I always compile with -w on, and clean up my code as I go. It is an indispensable feature for me now. -- Derek (skype: derek.j.parnell) Melbourne, Australia "A learning experience is one of those things that says, 'You know that thing you just did? Don't do that.'" - D.N. Adams 7/12/2005 2:18:10 PM
Dec 06 2005
prev sibling parent "Kris" <fu bar.com> writes:
"John Reimer" <terminal.node gmail.com> wrote
 I think we're just lucky to have warnings in DMD period.  Hell practically 
 DID freeze over when Walter implemented warnings into his compiler.

 Any further disparagement about the implementation might see them removed 
 altogether. ;-)
Perhaps why it's so rare for anyone to complain about it ;-) Who ever said we don't know what side the bread is buttered on?
Dec 06 2005
prev sibling parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Derek Parnell wrote:

 I believe that Walter is philosophically opposed to the concept of
 'warning'. Basically, if something is worth warning you about, it is worth
 fixing. So in effect there is nothing different between a warning and an
 error.
And he doesn't really mumble about it, either... :-) http://www.digitalmars.com/d/overview.html: (No Warnings) "D compilers will not generate warnings for questionable code. Code will either be acceptable to the compiler or it will not be. This will eliminate any debate about which warnings are valid errors and which are not, and any debate about what to do with them. The need for compiler warnings is symptomatic of poor language design." http://www.digitalmars.com/d/warnings.html (Warnings as Lint) "Depending on one's point of view, warnings are either a symptom of broken language design or a useful 'lint' like tool to analyze code and look for potential trouble spots. Most of the time, those trouble spots will be legitimate code intended to be that way. More rarely, it may indicate an unintentional error on the part of the programmer." and "Warnings are not a defined part of the D Programming Language. They exist at the discretion of the compiler vendor, and will most likely vary from vendor to vendor. All constructs for which an implementation may generate a warning message are legal D code." **** I still think DMD's Phobos should compile with -w, though ? I've patched it do so before, and it could be done again... (then again, I tend to use "use strict;" and "-Wall -Werror" when developing... Not for deployment though, to be portable) Last time I used it for Phobos, I found two *real* bugs within. And that's the reason why I put up with the sometimes annoying warnings. It's tedious to do the first time, but then it's done ? i.e. by working around the known warnings, I find the "unknown" - even if it means adding an extra cast or an extra parenthesis... Like with Unit Tests, once you get bitten - it's hard to go back ? --anders
Dec 07 2005
next sibling parent reply Tiago Gasiba <tiago.gasiba gmail.com> writes:
Anders F Björklund schrieb:

 Derek Parnell wrote:
 
 I believe that Walter is philosophically opposed to the concept of
 'warning'. Basically, if something is worth warning you about, it is
 worth fixing. So in effect there is nothing different between a warning
 and an error.
And he doesn't really mumble about it, either... :-) http://www.digitalmars.com/d/overview.html: (No Warnings) "D compilers will not generate warnings for questionable code. Code will either be acceptable to the compiler or it will not be.
Well, my initial question was not about questionable code... It was about variables that are defined but not used - doesn't mean that the program has a bug.
 This will eliminate any debate about which warnings are valid errors and
which are
 not, and any debate about what to do with them. The need for compiler
 warnings is symptomatic of poor language design."
I also don't think this (in particular) has anything to do with language design, which is very fine in this case, but with the practical usage of the language! Everyone forgets something now and then, right?
 http://www.digitalmars.com/d/warnings.html (Warnings as Lint)
 "Depending on one's point of view, warnings are either a symptom of
 broken language design or a useful 'lint' like tool to analyze code and
 look for potential trouble spots. Most of the time, those trouble spots
 will be legitimate code intended to be that way. More rarely, it may
 indicate an unintentional error on the part of the programmer."
In the case of unused variable you could argue that this has happen because the "user" forgot to remove an auxiliary variable that was previously used for debugging! Has zero to do with "broken language design"!
 and
 "Warnings are not a defined part of the D Programming Language.
 They exist at the discretion of the compiler vendor, and will most
 likely vary from vendor to vendor. All constructs for which an
 implementation may generate a warning message are legal D code."
 
It happens many times that, while coding, I forget to remove some variable declarations and the code is so huge that it is difficult to track down and remove them. Well, if the compiler removes the variables anyway, there should be no real problem but... wouldn't it make the code much cleaner not having "junk" in the code? Well, if Walter thinks warnings are useless, then let it be - I'm not an expert in writting compilers anyways. I just thought that in this case it would be a good idea that the compiler could "help" to cleanup the code! :) Best, Tiago -- Tiago Gasiba (M.Sc.) - http://www.gasiba.de Everything should be made as simple as possible, but not simpler.
Dec 07 2005
next sibling parent Don Clugston <dac nospam.com.au> writes:
Tiago Gasiba wrote:
 Well, if Walter thinks warnings are useless, then let it be - I'm not an 
 expert in writting compilers anyways. I just thought that in this case it
 would be a good idea that the compiler could "help" to cleanup the code! :)
I agree. But I think, we don't necessarily want to call it a warning. It's another 'code cleanup' tool, like the new code coverage option, which you can use to improve the quality of your code before you release it. It's yet another action the compiler can easily perform using its built-in parser. (Actually, this one is almost a 'code coverage of declarations). dmd -w -style main.d
Dec 07 2005
prev sibling parent Bruno Medeiros <daiphoenixNO SPAMlycos.com> writes:
Tiago Gasiba wrote:
 Well, if Walter thinks warnings are useless, then let it be - I'm not an 
 expert in writting compilers anyways. I just thought that in this case it
 would be a good idea that the compiler could "help" to cleanup the code! :)
 
I don't think I'd ever tell Walter how to write a compiler, but this is not about writing compilers (as in implementating compilers), it's about using compilers. And in using compilers (and using languages) we are all somewhat experienced and it's something we can discuss about :) -- Bruno Medeiros - CS/E student "Certain aspects of D are a pathway to many abilities some consider to be... unnatural."
Dec 08 2005
prev sibling parent reply Bruno Medeiros <daiphoenixNO SPAMlycos.com> writes:
Anders F Björklund wrote:
 Derek Parnell wrote:
 
 I believe that Walter is philosophically opposed to the concept of
 'warning'. Basically, if something is worth warning you about, it is 
 worth
 fixing. So in effect there is nothing different between a warning and an
 error.
And he doesn't really mumble about it, either... :-) http://www.digitalmars.com/d/overview.html: (No Warnings) "D compilers will not generate warnings for questionable code. Code will either be acceptable to the compiler or it will not be. This will eliminate any debate about which warnings are valid errors and which are not, and any debate about what to do with them. The need for compiler warnings is symptomatic of poor language design." http://www.digitalmars.com/d/warnings.html (Warnings as Lint) "Depending on one's point of view, warnings are either a symptom of broken language design or a useful 'lint' like tool to analyze code and look for potential trouble spots. Most of the time, those trouble spots will be legitimate code intended to be that way. More rarely, it may indicate an unintentional error on the part of the programmer." and "Warnings are not a defined part of the D Programming Language. They exist at the discretion of the compiler vendor, and will most likely vary from vendor to vendor. All constructs for which an implementation may generate a warning message are legal D code." **** I still think DMD's Phobos should compile with -w, though ? I've patched it do so before, and it could be done again... (then again, I tend to use "use strict;" and "-Wall -Werror" when developing... Not for deployment though, to be portable) Last time I used it for Phobos, I found two *real* bugs within. And that's the reason why I put up with the sometimes annoying warnings. It's tedious to do the first time, but then it's done ? i.e. by working around the known warnings, I find the "unknown" - even if it means adding an extra cast or an extra parenthesis... Like with Unit Tests, once you get bitten - it's hard to go back ? --anders
With all those inconsistent statements, I'm not sure exactly what Walter's opinion is now, but anyway let me say this. Again I say that many "warnings" should be treated as errors, but *not* those two I mentioned before. Let me give a practical example. Sometimes I want to run some test runs of my program, each with certain code pieces commented out (or made inacessible by an early /return/). Now, this usually causes unused variable or unreachable code warnings, and that's annoying because you will have to comment out the other parts of the code too (and possibly comment them back in afterwards). I've seen this annoyance first hand in the Java projects I've been in (because Java does threat these conditions as errors, not just as warnings), altough fortunately this did not happen often in those Java projects (perhaps due to their natures). However, in the C/C++ projects I've been in, these cases happened quite often[*] , and I know it would be a major pain to have these conditions treated as errors when I was developing them. [*] hum.. wonder if perhaps due to their nature being graphical-interactive (opengl, etc.). -- Bruno Medeiros - CS/E student "Certain aspects of D are a pathway to many abilities some consider to be... unnatural."
Dec 07 2005
parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Bruno Medeiros wrote:

 With all those inconsistent statements, I'm not sure exactly what 
 Walter's opinion is now, but anyway let me say this. Again I say that 
 many "warnings" should be treated as errors, but *not* those two I 
 mentioned before.
Maybe I should have mentioned this more explicitly, but quite some time did pass between those two quotes. Walter did add some warnings to DMD (due to popular demand?), but the "warning = error" still stands as far as I can tell. The option given is whether to check, or not... ? So it's more of a extra "lint run", than giving extra diagnostic output. (again to emphasise: this is how it works in DMD - not how it "must be") And I agree that having a "continue on error" option available can be highly useful, especially in early development or when porting old code. But it also feels like that we went over all this earlier with regard to how UnitTest-runs work*, without coming to much of a "consensus"... So I don't think that the default behaviour is going to change much. Hopefully it can be added as an *option* (like: "make -k" ?), though. --anders * i.e. whether the first error should stop the testing, or if it should continue with the other tests too and collect and log a test summary ?
Dec 08 2005
parent reply Bruno Medeiros <daiphoenixNO SPAMlycos.com> writes:
Anders F Björklund wrote:
 Bruno Medeiros wrote:
 
 So I don't think that the default behaviour is going to change much.
 Hopefully it can be added as an *option* (like: "make -k" ?), though.
 
I don't really mind much what the default behaviour is, just having the option would be fine by me.
 But it also feels like that we went over all this earlier with regard
 to how UnitTest-runs work*, without coming to much of a "consensus"...
 
 *
 i.e. whether the first error should stop the testing, or if it should
 continue with the other tests too and collect and log a test summary ?
Hum.. indeed, there are quite some things that could be said about this... -- Bruno Medeiros - CS/E student "Certain aspects of D are a pathway to many abilities some consider to be... unnatural."
Dec 08 2005
parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Bruno Medeiros wrote:

 i.e. whether the first error should stop the testing, or if it should
 continue with the other tests too and collect and log a test summary ?
Hum.. indeed, there are quite some things that could be said about this...
Yes, but for the tests one can write a "callback" to change behaviour: http://www.digitalmars.com/techtips/unittests.html Doing the same for DMD requires patching the compiler (at the moment) So in that regard it would be easier to add it to the GDC compiler... Hmmm: Wonder if the rest of the compiler works if one just skips the "exit" ? (in the "error(format, ...)" routine in the root.c DMD file, that was) --anders
Dec 08 2005
prev sibling next sibling parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Bruno Medeiros wrote:

 This isn't a bug, it's by design <g>. Warnings about unused variables can
 get rather annoying when they're intended.
 Why is it that Digital Mars compiler's option to enable warnings does 
 not only warning-notification but also error-on-warning? And there isn't 
 even a way to do just the first?
Another thing that is broken about DMD warnings is that they go stdout ? David has patched this up for GDC, to make them go stderr as they should but it hasn't made it "upstream" into DMD - despite several bug reports. It's all just a bunch of "s/printf\(/fprintf(stderr, /", excuse my Perl. --anders PS. There's a related bug, affecting where the exceptions get printed: http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D.bugs/2001 http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D.bugs/4368
Dec 07 2005
prev sibling parent reply clayasaurus <clayasaurus gmail.com> writes:
This is why I don't use warnings, because I need to 'fix' them if I want 
to see more. It just isn't worth it, unless you feel a need to fix every 
single warning you get.

Bruno Medeiros wrote:
 Walter Bright wrote:
 
 "Tiago Gasiba" <tiago.gasiba gmail.com> wrote in message
 news:dn1611$grl$1 digitaldaemon.com...

 "dmd -w" on  the following code should produce a warning like "unused
variable ii" or similar.
 Usually helps a lot :)

 <snip>
 int main(){
  int ii;

  return 0;
 }
 <snip>
This isn't a bug, it's by design <g>. Warnings about unused variables can get rather annoying when they're intended.
Why is it that Digital Mars compiler's option to enable warnings does not only warning-notification but also error-on-warning? And there isn't even a way to do just the first? etc., and maybe some more I don't recall) the warning semantics are all the same: by default "notify" about warnings, and only with an extra option do the warnings cause a compilation error/stop. This is what makes more sense in terms of nomenclature, because if a "warning" if allways an error, then it is not a "warning", it is a... well, "prohibition" or "order" or something... But more importantly, it is also what makes more sense in terms of programming/development, because it is more useful to have no-error warnings, allways enabled. Otherwise, no wonder one finds DM's warnings about unused variable annoying (as well as other warnings), since they halt compilation in unwanted situations. The end result is the coder rarely compiling with -w , is it not? I do agree that some of the traditional warnings (specially in the C/C++ world), such as use-of-uninitialized-variable, could be treated allways as errors, as they probably signify wrong code. However, some other types of warnings, such as unused-variable, unreachable-code, etc., are pretty much innocuous, and they occur frequently and *naturally* during developing/testing, where often there is incomplete code. That's why it is a royal pain to have error-on-warning enabled as such.
Dec 07 2005
parent reply Derek Parnell <derek psych.ward> writes:
On Wed, 07 Dec 2005 19:45:33 -0500, clayasaurus wrote:

 This is why I don't use warnings, because I need to 'fix' them if I want 
 to see more. It just isn't worth it, unless you feel a need to fix every 
 single warning you get.
And I do. -- Derek (skype: derek.j.parnell) Melbourne, Australia "A learning experience is one of those things that says, 'You know that thing you just did? Don't do that.'" - D.N. Adams 8/12/2005 11:54:51 AM
Dec 07 2005
parent reply Bruno Medeiros <daiphoenixNO SPAMlycos.com> writes:
Derek Parnell wrote:
 On Wed, 07 Dec 2005 19:45:33 -0500, clayasaurus wrote:
 
 
This is why I don't use warnings, because I need to 'fix' them if I want 
to see more. It just isn't worth it, unless you feel a need to fix every 
single warning you get.
And I do.
For a release, yes, definitely. But during development too?, all the time? -- Bruno Medeiros - CS/E student "Certain aspects of D are a pathway to many abilities some consider to be... unnatural."
Dec 08 2005
parent reply Derek Parnell <derek psych.ward> writes:
On Thu, 08 Dec 2005 12:26:42 +0000, Bruno Medeiros wrote:

 Derek Parnell wrote:
 On Wed, 07 Dec 2005 19:45:33 -0500, clayasaurus wrote:
 
 
This is why I don't use warnings, because I need to 'fix' them if I want 
to see more. It just isn't worth it, unless you feel a need to fix every 
single warning you get.
And I do.
For a release, yes, definitely. But during development too?, all the time?
Yep, its one of the standard switches I have in my Build configuration file. The rationale goes something like ... "keeping things tidy at the incremental stage is cheaper and safer than go back to clean up the mess later." It also encourages disciplined coding practice. -- Derek Parnell Melbourne, Australia 9/12/2005 7:46:33 AM
Dec 08 2005
parent Bruno Medeiros <daiphoenixNO SPAMlycos.com> writes:
Derek Parnell wrote:
 On Thu, 08 Dec 2005 12:26:42 +0000, Bruno Medeiros wrote:
 
 
Derek Parnell wrote:

On Wed, 07 Dec 2005 19:45:33 -0500, clayasaurus wrote:



This is why I don't use warnings, because I need to 'fix' them if I want 
to see more. It just isn't worth it, unless you feel a need to fix every 
single warning you get.
And I do.
For a release, yes, definitely. But during development too?, all the time?
Yep, its one of the standard switches I have in my Build configuration file. The rationale goes something like ... "keeping things tidy at the incremental stage is cheaper and safer than go back to clean up the mess later." It also encourages disciplined coding practice.
Well yes, it does, but not for those 2 warning, IMHO. In fact... I wonder what other warnings does DMD have, besides those two, (to see what I'm missing out)? ... *checking source code* ... Hum, seems it's only five: error("implicit conversion of expression (%s) of type %s to %s can cause loss of data", toChars(), type->toChars(), t->toChars()); error("no return at end of function"); error("array 'length' hides other 'length' name in outer scope"); s->error("statement is not reachable"); error("switch statement has no default"); And "unused variable" is not even one of them, which, silly me, was what Tiago started this thread for and I didn't notice until now. :S -- Bruno Medeiros - CS/E student "Certain aspects of D are a pathway to many abilities some consider to be... unnatural."
Dec 09 2005