Microsoft Code Analysis. Does Option Infer exist as a rule?

50 Views Asked by At

I recently ran into an bug caused by the accidental reuse of a variable in a For Each loop in my VB.NET project.

Dim iterator As Integer = 0
For Each iterator In {1, 2, 3}
Next
Console.WriteLine(iterator)

This code will output '3' I should have used a different variable name for the For Each loop. The loop was modifying the variable which was used for other purposes elsewhere.

I can prevent this behaviour by turning Option Infer off. However this introduces a lot (3000 ish) errors in a moderate sized project I'm working on.

What I would prefer, is a code analysis rule that flags up the use of For Each without a type declaration.

I've had a quick look through the Code Analysis rules provided with Visual Studio 2017, but can't see a rule. I fear I may have to create a custom rule.

Can anyone clarify that a rule does not exist before I start getting my hands dirty with custom rules?

0

There are 0 best solutions below