[0] https://ziglang.org/download/0.9.0/release-notes.html#Compil...
Edit: To be clear, love enforcing the idea for production code, but wish they had embraced a '-dev' mode or equivalent flag that made it easier to experiment.
[0] https://ziglang.org/download/0.9.0/release-notes.html#Compil...
Edit: To be clear, love enforcing the idea for production code, but wish they had embraced a '-dev' mode or equivalent flag that made it easier to experiment.
_ = bla;
_ = blub;
...which have been forgotten during development.So the next thing that's needed is an error if 'bla' or 'blub' are actually used elsewhere ;)
Would catch this:
var a, b
c = a + a // whoops, should have been a + b, compiler complains about unused b
Doesn't catch this: var a, b
foo(a, b)
c = a + a // whoops, should have been a + b, but still compiles
All in all, unused variables being errors is an awful feature that isn't very helpful in practice, at the cost of making experimentation a pain in the arse.There is nothing that kills my state of flow more than having to comment a piece of code that is unreferenced, because the compiler complains, while I'm trying to hack and explore some idea.
Luckily though my Rust setup doesn't fail to compile with unused stuff, it just warns - and then on CI i have it reject all warnings.
I agree it's very frustrating not having a -dev flag or something less restrictive.
Unused variable warnings are a good idea, but making them hard errors is a language design mistake. Warnings are good because they allow programmers to quickly make changes and test things, while providing a reminder to clean things up in the end (which is why the "just use tooling that removes unused variables" response misses the point--when making quick temporary changes for debugging, you want the warnings as reminders to go back and undo the change). Additionally, warnings allow for adding helpful static analyses to the compiler over time without breaking existing code like introducing new errors does. As I recall, there were some cases in which Rust 1.0 accidentally didn't flag unused variables, which was fixable post 1.0 without breaking existing code precisely because it was a warning, not an error.
Does Rust emit warnings for cached compilation units?
There is an interesting thread with community consensus against the use of #[deny(warnings)] at [2]. The most important takeaway for me is that the right place to deny warnings is in CI. You don't want end users who compile your crate to be have their builds fail due to warnings, because they might be using a newer version of the compiler than you were. You don't want to fail developers' builds due to warnings while hacking on code, because of the overhead warnings-as-errors adds to the edit/compile/debug cycle. CI is the right place to deny warnings, because it prevents warnings from getting to the repository while avoiding the other downsides of warnings-as-errors.
[1]: https://rust-lang.github.io/rfcs/1193-cap-lints.html
[2]: https://www.reddit.com/r/rust/comments/f5xpib/psa_denywarnin...
I would agree that forcing uninitialised variables as errors is a design mistake.
foo := someDefaultValue
if someCondition {
foo := someMoreSpecificValue
}
Whoops, accidentally created a fresh, unused variable in the nested scope instead of changing the value of the original variable.I do that frighteningly often.
We found a few cases in TigerBeetle where some nasty bugs would have been detected and prevented by this. Some of these were at the entry point to a function, where the function failed to check all the pre-conditions and was ignoring some of the API interface, and others were where we were calculating some of the derived buffers we would need, but then never use them, using the original buffer incorrectly instead.
test "example" {
var x: i32 = 1234;
_ = x;
}Do discards not fulfill your use case?
Not a deal-breaker (honestly, I write Python most days), but a huge annoyance.
(From my use in Go, I'm definitely overall a fan of the feature. Tho of course how much personal value vs annoyance you get out of it is going to depend on your own code style and behaviors.)