Include headers required by switches on generic EnumValue.
separate variable assignments from read usage
only wrap captured variables that are actually changed in or after closure (closes #3548)