CodeQL documentation

Expression has no effect

ID: cpp/useless-expression
Kind: problem
Security severity: 
Severity: warning
Precision: high
Tags:
   - maintainability
   - correctness
   - external/cwe/cwe-561
Query suites:
   - cpp-security-and-quality.qls

Click to see the query in the CodeQL repository

This rule finds expressions without side effects (i.e. changing variable values) that are used in a context where their value is ignored. These expressions are most likely intended to be part of a condition but were coded improperly.

In most cases these are defects caused by the unintended use of the comma operator. It is easy to misuse the comma operator (particularly in conditions) and it would be good practice to use it only when the brevity it allows is absolutely necessary (e.g. macro definitions).

Recommendation

Make sure that the flagged expressions are not oversights. To document that the value of the expression is deliberately ignored, you could explicitly cast it to void.

Example

void f(int j) {
	int i=0;
	for(i; i<10, j>0; ++i, --j) { //i < 10 has no effect, since the comma 
	                              //operator only returns the value of the last expression
		/* ... */
	}
}

References

  • © GitHub, Inc.
  • Terms
  • Privacy