Main Content

Line with more than one statement

Multiple statements on a line

Description

This defect occurs when, before preprocessing starts, the analysis detects additional text after the semicolon (;) on a line. A defect is not raised for comments, for-loop definitions, braces, or backslashes.

Risk

Use of one statement per line improves readability of the code. Since most statements in your code appear on a new line, use of multiple statements per line in a few cases within this arrangement can make code review difficult.

Fix

Write one statement per line.

If you do not want to fix the issue, add comments to your result or code to avoid another review. See:

Examples

expand all

int multi_init(void){
int abc = 4; int efg = 0; //defect

    return abc*efg;
}

In this example, abc and efg are initialized on the second line of the function as separate statements.

Correction — Comma-Separated Initialization

One possible correction is to use a comma instead of a semicolon to declare multiple variables on the same line.

int multi_init(void){
    int a = 4, b = 0;

    return a*b;
}
Correction — New Line for Each Initialization

One possible correction is to separate each initialization. By putting the initialization of b on the next line, the code longer raises a defect.

int multi_init(void){
    int a = 4;
    int b = 0;

    return a*b;
}
int multi_loop(void){
    int a, b = 0;
    int index = 1;
    int tab[9] = {1,1,2,3,5,8,13,21};

    for(a=0; a < 3; a++) {b+=a;} // no defect

for(b=0; b < 3; b++) {a+=b; index=b;} //defect

while (index < 7) {index++; tab[index] = index * index;} //defect
    return a*b;
}

In this example, there are three loops coded on single lines, each with multiple semicolons.

  • The first for loop has multiple semicolons. Polyspace® does not raise a defect for multiple statements within a for loop declaration.

  • Polyspace does raise a defect on the second for loop because there are multiple statements after the for loop declaration.

  • The while loop also has multiple statements after the loop declaration. Polyspace raises a defect on this line.

Correction — New Line for Each Loop Statement

One possible correction is to use a new line for each statement after the loop declaration.

int multi_loop(void){
    int a, b = 0;
    int index = 1;
    int tab[9] = {1,1,2,3,5,8,13,21};

    for(a=0; a < 3; a++) {b+=a;}

    for(b=0; b < 3; b++){
      a+=b;
      index=b;
    }

    while (index < 7){
      index++;
      tab[index] = index * index;
    }
    return a*b;
}
int multi_if(void){

    int a, b = 1;
    if(a == 0) { a++;} // no defect 
else if(b == 1) {b++; a *= b;} //defect
}

In this example, there are two conditional statements an: if and an else if. The if line does not raise a defect because only one statement follows the condition. The else if statement does raise a defect because two statements follow the condition.

Correction — New Lines for Multi-Statement Conditionals

One possible correction is to use a new line for conditions with multiple statements.

int multi_if(void){
    int a, b = 1;

    if(a == 0) a++;
    else if(b == 1){
      b++; 
      a *= b;
    }
}

Result Information

Group: Good practice
Language: C | C++
Default: Off
Command-Line Syntax: MORE_THAN_ONE_STATEMENT
Impact: Low

Version History

Introduced in R2013b

expand all