C and C++


LANG.STRUCT.LOOP.MFINIT : Malformed for-loop Initialization

Summary

The first ("initialization") clause of a for loop header does not have one of the following forms.

Properties

Class Name Malformed for-loop Initialization
Significance style
Mnemonic LANG.STRUCT.LOOP.MFINIT
Categories
MisraC2023 MisraC2023:14.2 A for loop shall be well-formed
Misra2012 Misra2012:14.2 A for loop shall be well-formed
Misra2004 Misra2004:13.5 The three expressions of a for statement shall be concerned only with loop control
  Misra2004:13.6 Numeric variables being used within a for loop for iteration counting shall not be modified in the body of the loop
AUTOSARC++14 AUTOSARC++14:M6-5-3 The loop-counter shall not be modified within condition or statement.
  AUTOSARC++14:A6-5-4 For-init-statement and expression should not perform actions other than loop-counter initialization and modification.
MisraC++2008 MisraC++2008:6-5-3 The loop-counter shall not be modified within condition or statement.
MisraC++2023 MisraC++2023:9.5.1 Legacy for statements should be simple
JSF++ JSF++:198 The initialization expression in a for loop will perform no actions other than to initialize the value of a single for loop parameter.
Availability Available for C and C++.
Enabling Checks for this warning class are disabled by default, and require the unnormalized C ASTs for the project. To enable them, add the following WARNING_FILTER rule and RETAIN_UNNORMALIZED_C_AST specification to the project configuration file.
RETAIN_UNNORMALIZED_C_AST = Yes
WARNING_FILTER += allow class="Malformed for-loop Initialization"
Note that retaining the unnormalized ASTs will increase the disk space used to store the project representation, and may make the analysis take longer.

Example

void f(void);

int lang_struct_loop_mfinit(void){
  int i=0;
  int j;
  int rv=0;

  for (; i < 10; i++){ rv++; }                                   /* first permitted form */

  for (i = 0; i < 10; i++){ rv++; }                              /* second permitted form */

  for (int k = 0; k < 10; k++){ rv++; }                          /* third permitted form */
  
  for (i=0,j=1; i == j; i++){ rv++; }    /* 'Malformed for-loop Initialization' warning issued here */
  
  for (f(); i < 10; i++){ rv++; }        /* 'Malformed for-loop Initialization' warning issued here */

  return rv;
}

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.