fdo#39440 reduce scope of local variables

This addresses some cppcheck warnings.

Change-Id: I390607e002e93cf7a6babc26d9be084d9f185058