Main Content

MISRA C++:2008 Rule 0-1-4

A project shall not contain non-volatile POD variables having only one use

Since R2020b

Description

Rule Definition

A project shall not contain non-volatile POD variables having only one use.

Rationale

If you use a non-volatile variable with a Plain Old Data type (int, double, etc.) only once, you can replace the variable with a constant literal. Your use of a variable indicates that you intended more than one use for that variable and might have a programming error in the code. You might have omitted the other uses of the non-volatile variable or incorrectly used other variables at intended points of use.

Polyspace Implementation

The checker flags local and static variables that have a function scope (locally static) and file scope, which are used only once. The checker considers const-qualified global variables without the extern specifier as static variables with file scope.

The checker counts these use cases as one use of the non-volatile variable:

  • An explicit initialization using a constant literal or the return value of a function

  • An assignment

  • A reference to the variable such as a read operation

  • An assignment of the variable address to a pointer

If the variable address is assigned to a pointer, the checker assumes that the pointer might be dereferenced later and does not flag the variable.

Some objects are designed to be used only once by their semantics. Polyspace® does not flag a single use of these objects:

  • lock_guard

  • scoped_lock

  • shared_lock

  • unique_lock

  • thread

  • future

  • shared_future

If you use nonstandard objects that provide similar functionality as the objects in the preceding list, Polyspace might flag single uses of the nonstandard objects. Justify their single uses by using comments.

Troubleshooting

If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

#include <mutex>
int readStatus1();
int readStatus2();
void getReading(int*);
extern std::mutex m;
void foo()
{
	// Initiating lock 'lk'
	std::lock_guard<std::mutex> lk{m};   
	int checkEngineStatus1 = readStatus1();
	int checkEngineStatus2 = readStatus2();//Noncompliant
	int sensorData;//Noncompliant
	getReading(&sensorData);
	if(checkEngineStatus1) {
		//Perform some actions if both statuses are valid   
	}
	// Release lock when 'lk' is deleted at exit point of scope
}

In this example, the variable checkEngineStatus2 is used only once. The single use of this variable might indicate a programming error. For instance, you might have intended to check both checkEngineStatus1 and checkEngineStatus2 in the if condition, but omitted the second check. The variable sensorData is also used only once when its address is passed to the function getReading(). Polyspace flags these single use variables.

The lock_guard object lk is used a single time. Because the semantics of a lock_guard object justifies its single use, Polyspace does not flag it.

Check Information

Group: Language Independent Issues
Category: Required

Version History

Introduced in R2020b

expand all