After writing a fair amount of zig, I’ve noticed that it’s almost never** a good idea to have multiple
trys in the same function without
errdefer after all
trys but the last one. I’ve also noticed I often leave off those
errdefers and have to go back and add them later. I would appreciate having some kind of reminder that they should probably be there. Have others run into the same thing or is it just me? And how do people here feel about having some kind of reminder about it, whether it’s baked into the compiler or in an external lint tool?
** I have run into situations where it is not the case but even then I found it is helpful to have a comment about why the
errdefer isn’t necessary
I would actually suggest to put an
errdefer (or think about why you don’t need one) after every
That is easier to make into a habit.
As for linters, I doubt there is such a thing yet. I only know of
ziglint, which doesn’t do much. You could try making one yourself though. Shouldn’t be too hard, unless you really want it to show up in your IDE.
A quick and dirty regex “parser” should do fine.
As you get more comfortable with Zig you will start to improve the simplicity and performance of your resource management by doing batched destruction. One popular example of this is arena allocation. With arena allocation you have many instances of
try with only one cleanup that cleans up all of the allocations at once. It would be a shame to have a warning for doing something that is actually highly recommended, robust, and performant.