Rider Help

Code Inspection: Local variable has too wide declaration scope

Rider suggests moving the declaration of a local variable closer to its usage. If a variable is not used outside a block of instructions, it is a good practice to declare it within that block.

In the example, Rider suggests moving the declaration of tmpvar inside the for loop, where tmpvar is used.

Suboptimal codeAfter the quick-fix
private static char[] MyMethod(char[] x) { char tmpvar; for (int i = 0; i < x?.Length / 2; i++) { tmpvar = x[i]; x[i] = x[x.Length - 1 - i]; x[x.Length - 1 - i] = tmpvar; } return x; }
private static char[] MyMethod(char[] x) { for (int i = 0; i < x?.Length / 2; i++) { var tmpvar = x[i]; x[i] = x[x.Length - 1 - i]; x[x.Length - 1 - i] = tmpvar; } return x; }

If possible, Rider will automatically join the declaration and assignment, and according to the default preferences will change the explicit variable type to var when it applies this quick-fix.

Last modified: 11 October 2017

See Also