[Misc] Apply SonarCloud rule cpp:S6004
SonarCloud rule fix
Rule name:
"if" and "switch" initializer should be used to reduce scope of variables
Rule code:
cpp:S6004
Rule link:
Other comments