This is a proposed warning to be added to -Wfor-loop-analysis, which is part of -Wall. This warning will catch instances where the condition will be false on the first iteration and the loop body will never be run. The warning will be emitted when:
- The for-loop initializes or sets the value of a single variable to a constant value
- The condition is a simple comparison between the variable and another constant value
- If the initial value of the variable substituted into the comparison would cause the comparison to be false
In order to make step 3 work, the constant value from step 1 may need to be casted to a different type. The casts can be extracted from the comparison operand. This allows the warning to work with both integer and floating point types, as well as mixing types.
Two exceptions to this warning.
- Parentheses around the condition will silence this warning. This is suggested in a note.
- If the variable is used as an array index in the body, and the condition is less than a value the same as the array size.
Example:
https://reviews.llvm.org/D48498
This warning would have caught the issue that @lebedev.ri brought up about the loop not running.