This makes them real void expressions, and they can be used anywhere
where a void function call can be used, for example in a conditional
operator.
Details
Diff Detail
- Repository
- rS FreeBSD src repository - subversion
- Lint
Lint Passed - Unit
No Test Coverage - Build Status
Buildable 42408 Build 39296: arc lint + arc unit
Event Timeline
Technically this is fine.
My only request would be to consider the type of the expressions. According to gcc manual,
The last thing in the compound statement should be an expression followed by a semicolon; the value of this subexpression serves as the value of the entire construct. (If you use some other kind of statement last within the braces, the construct has type void, and thus effectively no value.)
It so happen that the 'last thing' in all cases is some if operator, so added expressions all have void type. Can we make this explicit somehow (I do not have a specific proposal)?
The most laconic way to do add explicitness is to add (void)0; to end of each definition.
To me this looks like linguistic/philosophical question on reading the standard. What actually makes an expression explicitly void? Standard doesn't have official definition of what a void expression is. It briefly defines it in 6.3.2.2 in braces - an expression that has type void. Let's go to primary definition of what an expression is:
6.5 Expressions
1 An expression is a sequence of operators and operands that specifies computation of a value, or that designates an object or a function, or that generates side effects, or that performs a combination thereof. The value computations of the operands of an operator are sequenced before the value computation of the result of the operator.
Our if statement is already a void expression statement with side effects (see 6.8.3), so it fully qualifies to "The last thing in the compound statement should be an expression followed by a semicolon; the value of this subexpression serves as the value of the entire construct".
IMHO, adding (void)0; won't make it better, will only raise questions from future readers of code.
Adding explicit void statement at the end make the code future-proof against changes that make the resulted expression type non-void. For instance, if some assignment is added before the closing }), it types the expression, suddenly. I want to avoid this gotcha.
So IMO the ending line like (void)0; /* ensure void type for expression */ is useful.
Note: revision has changed title to workaround bug in git-arc. When committing then plan is to use original title line.