10 ms·
Not -Werror considered harmful
- steerablesafe 5y agoYou can enable -Werror with a known compiler all you want for development, but if you package and distribute your sources then turn that off by default.
- Cthulhu_ 5y agoIf you're still getting warnings that you think should not be ignored (which is -Werror iirc) in your build by the time you're ready to distribute, you need to take a hard look at your development and merge process, too. Ideally nothing goes into the main branch if it's not done and ready to be deployed. One of the things we finally managed to do at a previous job was "master=live", where a build was automatically triggered that put the product ('just' a website, but still) live in production. While I'm not advocating this per se, you SHOULD put everything in place so that you could do that. (see also: continuous integration vs continuous deployment).
- remram 5y agoCompilers get new diagnostics all the time.
- hzhou321 5y agoFor me, the biggest con is the bad code developers commit in order to suppress warnings, both true positives and false positives. The latter really pushes me in the camp of haters firmly. The more warnings you turn on, the more false positives you will encounter. Any opinions should acknowledge the balance first.
- jwlake 5y agoI think the problem with this article is it makes a ton of assumptions. For an open source dependency used in a ton of projects and platform, -Werror tends to be fairly infeasible. For a specific CI build on a specific platform, it tends to be a good idea. The confounder is always -Wall, which is overkill alot of the time.
- zxcvbn4038 5y ago"X considered harmful" should be a meme at this point. It is basically an opinion piece in someone's personal blog. I wouldn't be surprised if there is a "too much compiler output considered harmful" counter argument in a blog someplace.
- wffurr 5y agoIt obviously is a "meme" as in a bit of knowledge that gets passed around and mutates and spreads. Do you mean something else by that? Perhaps you mean we should disregard titles like that? I think it's a fine title for a polemic. You can interpret and grant as much weight as you like based on the presented arguments and the author's bona fides. Just like any other essay.
- yakubin 5y agoIt's boring. Every week there is a post with a title of this form. It's as if mindless bots were writing the titles, not creative humans.
- yakubin 5y agoAddendum: just today there are two of those on the front page: [1]: <https://news.ycombinator.com/item?id=28746549 https://news.ycombinator.com/item?id=28746549> [2]: <https://news.ycombinator.com/item?id=28745949 https://news.ycombinator.com/item?id=28745949>
- jerf 5y agoIt is a meme: https://meyerweb.com/eric/comment/chech.html https://meyerweb.com/eric/comment/chech.html That doesn't have a date on the page but there's an HN link back at least to 2015 and I think it's older than that.
- bayindirh 5y agoIt all started in 1968. From the page: Edsger W. Dijkstra’s note in the March 1968 Communications of the ACM, “Go To Statement Considered Harmful,”
- Macha 5y agoIt misses the big con for me: It means your code may fail to compile on future compilers using your provided build scripts. Since C doesn't have a method of enforcing a specific compiler version like newer languages, that's a use case you need to prepare for. The most recent example I was bitten for with this was with the vendored version of ring used in the redox project. My system had GCC 11, which added a new warning for mixing array and pointer syntax between a function declaration and function definition.
- bhaak 5y agoAnd for packagers of distributions this would mean your program breaks way more often than it should. I think it's a good idea to have -Werror on your internal CI build but not on by default for released tarballs. Especially not if you enable -Weverything as well. That WILL break with almost every compiler update.
- 4ad 5y agoYeah, build with -Werror when you control the compiler, e.g. in your CI system, or whatever you use to produce build artifacts, but don't hardcode it in your build otherwise.
- twic 5y agoYou should always control the compiler. The compiler is a dependency of your project, just like the libraries you use. The project should specify the version, just as it does for libraries. In Rust, you use the rust-toolchain file, in Gradle, set java.toolchain.languageVersion, etc. In my C++ projects, i use an ugly ball of shell script; the author of this article explains how to do it using Docker. If someone wants to build the project with a different compiler, that's fine - they have to make the change and fix any resulting problems, just as if they were changing the version of a library.
- 4ad 5y agoHave you ever heard of libraries (as opposed to binaries)? Or open source software packaged by distributions?
- outsomnia 5y agoExactly. -Wextra should also be in the same boat as -Werror and -Wall. How to deal with different toolchains having different approaches to what warnings are enabled? Assertively disable the handful of stupid warnings like -Wno-unused-parameter, and have your CI encompass most toolchains such that the union of their warnings is your target and break build if any blow warnings. Then you will have something robust and portable.
- saagarjha 5y agoRobust and portable, until someone tries compiling it with LLVM 13 that you haven't tested on…
- outsomnia 5y ago> Robust and portable, until someone tries compiling it with LLVM 13 that you haven't tested on… Yes, new compilers are able to get new insight into what is broken with your code. You can either: a) ignore that and let your users continue with the brokenness, since that is convenient at build-time (but the consequent failures in the field of whatever that code is integrated in may be extremely inconvenient for all downstream users) b) learn about it, and fix your code upstream so all users can benefit from the fixes
- IshKebab 5y agoI feel like only the last point (your build will fail using different compilers / compiler versions) is the only serious issue. But "just build it using Docker" is a pretty awful solution. Doing stuff in Docker is just extra pain in general, and it doesn't even work on Mac or Windows. I think a better option I've seen is just to have a config flag to disable `-Werror`.
- strenholme 5y agoDocker runs fine on a Mac. Docker kinda sorta runs in Windows, but doesn’t play well with VMware player, to the point I gave up on asking my users to use Docker in Windows (I briefly flirted with the idea until I tried actually installing Docker in Windows). Docker doesn’t work in any of the BSDs, it doesn’t work in Solaris, it doesn’t work in HaikuOS, and it doesn’t work in anything resembling an embedded space. Saying “you must use Docker to compile my software” severely limits where it can actually be used. I say all this as someone who uses a Docker container for my CI testing step (once a day whenever my code is changed, I compile and test it in a Docker container).
- IshKebab 5y agoDocker is always used for running Linux. Even on Mac or Windows Docker is running Linux. That's no help if you're building software for Mac or Windows.
- daemin 5y agoI'm sticking by the principle that warnings should be warnings and errors should be errors, and that you shouldn't "promote" warnings to be errors. I can't remember who said it but I think it was in a conference video. People often claim that without promoting warnings to be errors and thus stopping compilation that you'll just get a lot of warnings. However in reality most of these warnings just get disabled in the code, and often in a way that disables them for the entire project, meaning that you're missing out on good diagnostic warnings. The proper way to fix warnings is to keep them as warnings and have it on the team to make sure they don't get out of hand. By making sure when you implement a new feature or fix a bug that you don't introduce new warnings, and also have people investigate warnings and fix them in the correct and sustainable way. In some cases you would disable that warnings but in general they should be fixed by fixing the code. That way you get the benefit of having good diagnostics along with the flexibility of being able to investigate and fix it at a later date. One final note is that having warnings show up during the compile is a good indicator of how much technical debt there is in the project. If you just end up disabling warnings you're really hiding how bad the code state is.
- adrianN 5y agoIf you disable warnings such that they're disabled for the whole project you're just doing it wrong imho. Warnings need to be fixed by making the compiler happy and only (locally!) disabling them if there is a bug in the compiler.
- daemin 5y agoYes you should only disable them locally, in as small a region as possible. Though in a lot of cases in my experience this isn't so. They're generally bulk disabled before including external third party headers, they're disabled in low level projects, they're disabled in headers so that they can be submitted. The point is that people often disable them in ways which they shouldn't just so they can compile their code and get on with fixing their bug or implementing their feature. Mainly because warnings get treated like errors and prevent the build from succeeding. With warnings as warnings we could take a more caretaker approach to dealing with them and we shouldn't have these situations created.
- CodesInChaos 5y agoI think gating pull-requests on -Werror makes sense, at least for your own code. That way you keep the development branch warning free on the current compiler version. (An override for backported fixes and other exception cases is still a good idea) But I would not use -Werror when building existing releases, so you can still build old versions of your code using newer compilers. This article seems to only consider the pull-request scenario when talking about -Werror being fine, while the open-source people opposing -Werror are mostly concerned with building existing releases. For example rust's stability guarantees do not cover new warnings, so your code can stop building when upgrading the compiler if you use its -Werror equivalent.
- pornel 5y agoC doesn't have a reliable portable way to silence a known warning. Inevitably, some warnings will be irrelevant/imprecise/overzealous (after all, they're only warnings, not errors). To silence warnings in C without vendor-specific pragmas, people fiddle with the code until the warning goes away. This can be messy, cause bugs, and isn't even guaranteed to work on any other compiler, including a newer version of the same compiler. So lack of -Werror is harmful, and -Werror is harmful too. C needs to sort it out.
- unwind 5y agoCan you be more specific, maybe with an example of such code? I feel (rather strongly) that the language's standpoint is pretty clear, and that warnings are a message of joy, the compiler is giving you an opportunity to fix a bug before it happens. Why is not "fixing the code" an option? If people don't understand the warning, and resort to "fiddling" to make it go away, that's a warning that there might be some training needed. :)
- pornel 5y agoA common case is an unused variable. It may be hard to avoid if macros/ifdefs are involved, or if it's an argument of a function that must match a predefined API. To "fix" such warnings people add a cast to void, self-assignment, or some other dummy code that makes compiler think it's "used", until you get a compiler with better data flow analysis that can see through the faux-use code. __attribute__((unused)) solves this properly, but technically it's a GNU extension. And it's just for this warning, not a general solution for hundreds of other warnings you may get. Other cases: • Implicit conversion warnings. This can happen due to platform's headers using slightly different types (e.g. disagreeing on signedness). You add an explicit cast, but now there's a third type involved, and if it wasn't the right type, you're worse off. • Tautological or side-effect-free expressions. Platform-specific code and macros can make them. You add more ifdefs, which is a hit on readability, and more opportunity to mess it up and break a platform you don't have CI for. • Cast to a type with a higher alignment. It is a common pitfall, and could cause UB, so it's good to be warned about. But if you've done required pointer arithmetic or aligned allocs, you need to be able to say "I know what I'm doing". You can't, apart from trying to obfuscate the code to confuse the compiler (and human readers too probably).
- jpegqs 5y agoI'm porting Linux packages that compile for GCC/Clang to an EDG frontend based compiler and I need to remove forced -Werror from build scripts because different compilers have different warnings (and even false warnings due to compiler bugs). So what I want to say to people who force -Werror by default is that your software is no longer portable. Do not do this.
- ulnarkressty 5y agoEven the VC++ team at Microsoft only targets clean /W4 compilation [0]. If one enables /Wall there's hundreds of warnings in the standard library files [1]. Not to mention any dependency one might have (looking at you, Boost)... [0] https://www.reddit.com/r/cpp/comments/bubg7b/msvc_compiler_warnings_that_are_off_by_default/ep9vlk1/ https://www.reddit.com/r/cpp/comments/bubg7b/msvc_compiler_w... [1] https://developercommunity.visualstudio.com/t/c-include-files-dont-compile-cleanly-1/584686 https://developercommunity.visualstudio.com/t/c-include-file...
- ludocode 5y agoI agree with the spirit of this article: -Werror is a very good thing and should be on in any serious codebase. But the rebuttals for the cons are pretty weak. For Con #3, "make I=0" flag is cute, but the final "make" doesn't actually work. Any files that had warnings but still compiled successfully will not get recompiled. The simplest workaround is to "make clean" first but this can be time consuming (ccache helps.) Another workaround is a script that touches any unclean or just-committed git files but this is error-prone. It's possible to have "make" rebuild any files that were built with I=0 but it adds a lot more Makefile complexity. And in any case all of these are manual workarounds that are easy to forget. For Con #4, a docker image doesn't help you build for Windows or macOS. You need full VMs to test on them (and a macOS VM violates the license, so technically you need a full Mac somewhere you can SSH into.) This process can be optimized pretty well but it's still another manual step.
- flohofwoe 5y agoUnfortunately it's not as simple as always enabling -Werror: - new compiler versions regularly introduce new warnings, not a bad thing in general of course, but: - even if your own code is warning-clean (which it really should be!), dependencies are usually full of warnings especially after upgrading compiler versions - some compilers issue different warnings based on compile settings (e.g. optimization level) - there's a threshold where picky warnings stop being all that useful. For instance hardly any C/C++ code I've encountered is -Wsign-compare clean, and some APIs are not designed with this sort of warning in mind, so you have the option of littering your code with explicit casts (which makes it less readable), or just ignore that warning and move on. My own strategy is: enable -Werror for development and keep your own code warning free, while locally supressing warnings in dependencies. For release builds, don't use -Werror (because you can't know what compiler version your users are using). There's also the theoretical feature that headers included with '#include <...>' don't generate warnings, but I haven't gotten this to work reliably across compilers.
- deleted 5y ago[deleted]
- nullc 5y ago> some compilers issue different warnings based on compile settings Also based on which platform they're run on. > new compiler versions regularly introduce new warnings, Much worse, IMO is older compiler versions which emit incorrect warnings. At least when a new version emits a new, correct, warning it's at least something you want to fix (or disabling a new unhelpful warning). The failure to compile may still be a practical problem but at least the direction it creates is a right one. For older compiler versions, there may be no fix-- breaking the code or turning off the warning may be the only options if Werror is used. Outside of heavily controlled environments, I think Werror is just plain toxic. Want to run it on your CI builds where you don't need to worry about the compiler being upgraded or downgraded without warning? Great! Everyone should do that. Being warning clean on your primary target toolchains(s) is an important move. For code distributed to users the use of Werror use creates a pressure against introducing new and helpful warnings for compiler authors. Fortunately, it's such a nuisance for code distributed to others that no one survives enabling it for long and the worse damage it likely does is that the few projects that use it for publicly distributed code refrain from enabling many otherwise useful non-default warnings.
- baybal2 5y agoOne more thing: If you ever decide on to do formal verification of your code, getting it into shape will probably take as much as the verification itself. Being super-duper strict from day 1 of the project makes it so much easier.
- GuB-42 5y ago-Werror is fine as something you enable in your personal development process to make sure you didn't miss a warning. But other than that, it is a bad idea. Compiler warnings are just another static analyzer, on equal footing with cppcheck, clang-tidy, etc... It is just convenient because it is built-in. So you can make a policy that in order to push something, it has to pass a variety of automatic checks including compiler warnings, static analysis, unit tests, documentation, coverage, etc... And -Werror can be used for that, though I think it would be better to let the compilation go though to make a complete report. The big problem with -Werror is that what a compiler considers a warning depends on the version of the compiler. It means you will get new warnings as you upgrade the compiler, so you will be tempted to stay with the old compiler to silence the new warnings, because otherwise, your code stops compiling. Terrible idea. Another con that is not mentioned is a typical problem when you have strict rules you can't ignore. People will do what it takes to make the warning disappear without actually solving the problem. For example, there is a warning if an enum value is not referenced in a switch/case with no default. It is a very useful warning, it tells you that some case is not implemented, and IMHO, you should leave the warning until it is properly done. But let's say you need to move forward and that annoying -Werror won't let you. You just need to add and empty default case, silencing the warning, not fixing the problem, forgetting it, and shipping buggy code. And maybe you won't do these ugly hacks (really?) but someone else will, I've seen such things done in certified code, the kind that flies airplanes...
- jmull 5y agoThe fundamental confusion of the article (and come commenters here) is whether you primarily release code or binaries. If your primary product is a binary (even if you release code too for reference or to be open), by all means, use -Werror (and strictly set the supported compiler). If your product is code (even if you release binaries too as a convenience), -Werror in the release is an impediment to your users because you've introduced a dependency on a specific compiler version. Think of what will happen when a user tries to use another source library using -Werror... but pinned to a different compiler or compiler version. You've created a problem with your project that your users will have to solve. You could dictate the the build environment of your users, but that's simply a roundabout way to actually be in the business of releasing binaries. For some projects, I think you can decide to dictate the use of -Werror (and compiler version) for certain code changes processes, but that's a judgement call, with pros and cons. A strength of open source is that the distinction between user and contributer can be blurry. E.g., imagine a user runs your library on a platform you have neither the time nor expertise to support, finds and fixes some issues and is willing to contribute those changes back... are you sure you want to put them in the position of addressing errors from a toolchain they don't know or understand? Maybe. But maybe not.
- pizlonator 5y agoYou hit the nail on the head! And the same exact critique applies to folks saying “-Werror is harmful”. That statement is false unless you also add the limiting principle: “-Werror is harmful in software shipped as source that users build themselves”. This is a classic case of folks talking past each other. Kinda fun to watch actually.
- jcelerier 5y ago> because you've introduced a dependency on a specific compiler version No, if you get warnings, it means that your code is (and was already) wrong. And if you wrote wrong code you already have an implicit dependency on your compiler version, as more recent versions with more advanced static analysis may turn a semi-harmless unrecognized UB as a very harmful recognized one (and thus warn). I had this case a few times already, see a simple repro : https://gcc.godbolt.org/z/jY69sas8K https://gcc.godbolt.org/z/jY69sas8K -Werror just makes this dependency explicit.
- kazinator 5y ago> Now create the docker container image. Right. If everyone downstream of you replicates your exact environment, then they won't see anything out of -Werror that you don't see. Proof by Docker; QED. For the rest of us who build programs that have to work with a wide range of compiler and compiler versions, we don't want to create work for the downstream packagers. I don't want some downstream packaging person to be wasting their time non-productively adding "compiler shut-up patches" over top of my program because their distro is on a newer compiler, and my package drew attention to itself by suddenly failing to build. Or worse: not satisfied with shut-up patches, but they start digging into the failing code, decide that there are real problems and start fixing things, without contacting upstream. If you take care of your warnings as a matter of habit, then your program builds quietly. When new diagnostics appear in the compiler, they stand out very boldly against your previously quiet build. Warnings don't break the build for your downstream users; they just render it noisy. When you upgrade your compiler (perhaps due to being tipped off by thoser users), you see the noise too, and you can do something about it. You can do something about it in an official way that is committed to your trunk. The downstream people pick that up, and things are quiet again on their end also. -Werror can be useful if you inject it into your builds externally, without committing that into the build system that ships with the program, so that other consumers of the program outside of your CI environment aren't impacted by it. It just says that, "for us, in this shop, with the version of the compiler that we have currently installed on our farm, we are treating all warnings as errors (so that changes which introduce warnings cannot pass review)." Lone FOSS developers working by themselves (or in collaboration with others via patches) don't need that; just the habit of keeping the build clean of all warnings. The commit gatekeeping person builds everything and if they notice warnings, they can push back on the patch; no need for -Werror since a human is involved rather that simple build robot that only goes by the exit status of the build without looking at the output.