4 ms·
Two musings on the design of compiler warnings
- Aardwolf 6y agounused variable is the warning that causes me the most pain it happens so often that I want to test or debug something, disable some code, or replace some value by "5" or what not as a placeholder, and then get the unused variable warning sure, it can be fixed with (void) casts, but sometimes there are multiple unused variables due to commenting out part of code, then you have to hunt all of them and sure it can be fixed with a flag to disable this warning, but that may require a recompile of everything if the build system considers this a change of compiler flags and sure you could disable it always, or only for certain modes, but the warning also has its merits
- marcosdumay 6y agoAre you compiling with -Werror or something like that? Because most people have no problem with warnings appearing while they modify their code.
- throwaway43234 6y agoOr just ignore warnings if you know they're from a temporary change that wont ever be committed?
- quicklime 6y agoSome teams turn on the switch in their compiler or linter that turns warnings into errors. Not many people invoke their compilers directly anymore, so disabling this requires digging into the build system configuration, which is not always simple (and as the OP mentioned, can require a full rebuild). Sometimes this is even baked into the language itself. For example Go can be strict about unused imports and variables. Python is strict about having correct indentation.
- marcosdumay 6y ago> Some teams turn on the switch in their compiler or linter that turns warnings into errors. That's completely self-inflicted. I really don't get why people do that instead of just fixing the warnings... except if one is in some position of power over environment configuration, but not over work results. Even then, why does that person care about work results?
- a_t48 6y agoIn my experience the warnings tend to pile up. Once you have a few warnings in the codebase, what's a few more?
- throwaway43234 6y agoI think a good compromise is warnings aren't allowed in the final committed code (this can be enforced either by convention or with git hooks), but whatever you do locally is between you and your compiler. "What happens in the devbox stays in the devbox"
- a_t48 6y agoI'd tend to agree, though this can be harder to set up than just having a consistent build config for all platforms.
- saagarjha 6y agoGenerally I would hope that your CI build allows you to set this kind of configuration. (At the very least, you must have this as an option, because someone will run into it at some point and will have to turn your -Werror off.)
- beojan 6y agoIn large bodies of code, you might not notice the warnings if they weren't errors.
- nyanpasu64 6y agofor (auto foo : list) assert(foo); The problem is that the assert() macro disappears in release mode, causing foo to trigger an unused variable warning. You need to (void)foo; as well. On a related note, since assert() is removed in release mode, assert(fn_with_side_effects()) will fail to run the function in release mode. You need to separate it into two lines.
- saagarjha 6y agoGo is awful about this, because it will refuse to compile your code if you have an unused import.
- FabHK 6y agoQuestion: Does the compiler still throw the warning about "x and y unused" when, instead of replacing z = x + 2*y + 1; by z = 5; // x + 2*y + 1; you do something like z = (x + 2*y + 1) * 0 + 5;
- PaulHoule 6y agoThere is a set of concerns in this article that are universal, and then there are the specifics of C, which generally leave me horrified that because some rando is free to write if((this=that)) { ... } some hackers can crash the power grid, blow up a chemical plant, etc. Using the double paren to suppress warnings is frankly terrifying to me because the double paren is the kind of thing that comes out of the ten thousand monkeys that almost understand Noam Chomsky that are trying to 'help' me in my IDE or when I type with a touchscreen -- the same accidents that might result in "this=that" could just as well leave double parens... OTOH in more civilized languages the question of error messages is fascinating to me but insufficiently documented, in fact it is part of the "parser generators haven't improved since yacc" syndrome. "Little languages" like the ones yacc was invented to make easy haven't taken off in the way that yacc's siblings did because of a handful of problems that haven't been addressed in one tool -- and the error handling is the worst of them. I told the Jena people that I thought error handling with their RETE engine was great -- I could look at the error and look at the Jena source code and understand what was wrong with my source code. That was just not the case with Drools.
- saagarjha 6y agoSee, I think your problem is specifically with the fact that C provides the same operator for "assignment" and "assignment and I get a value back". The issue is that the latter is useful but generally not what you want, so this is why we warn about it but I don't think it's justified to be horrified about it.
- m463 6y agoI remember very old versions of lint had hints you could add, like: /*NOTREACHED*/ that would suppress warnings. I kind of liked that method.