fix cppcheck warnings about reducing scope of variables

This is mostly cosmetic, but it actually seems to have made cppcheck find
more useful problems, like the commit before this one.