Miscellaneous cppcheck and other fixes

Like #268 but for master, not stable-0.8. Sorry!

I'm happy to squash things if the commit history gets too detailed ;)
Did you verify this is working as expected?