Skip to content

Latest commit

 

History

History
51 lines (41 loc) · 1.43 KB

File metadata and controls

51 lines (41 loc) · 1.43 KB

PH2127: Avoid changing loop variables

Property Value
Package Philips.CodeAnalysis.MaintainabilityAnalyzers
Diagnostic ID PH2127
Category Maintainability
Analyzer AvoidUsingParametersAsTempVariablesAnalyzer
CodeFix No
Severity Error
Enabled By Default Yes

Introduction

Inside the loop, do not change the loop variables. This makes the loop logic harder to read and can cause unexpected behavior.

How to solve

Instead of changing the loop variable, use continue or break to control the loop flow.

Example

Code that triggers a diagnostic:

public class BadExample {
  public void BadLoop() {
    for (int i = 0; i < 5; i++) {
      if (someCondition) {
        i = 5;
      }
    }
  }
}

And the replacement code:

public class GoodExample {
  public void GoodLoop() {
    for (int i = 0; i < 5; i++) {
      if (someCondition) {
        break;
      }      
    }
  }
}

Configuration

This analyzer does not offer any special configuration. The general ways of suppressing diagnostics apply.