You just need to rethink your logic.
static const int num_actions_to_skip = 2;
const int loc = action_list.count() - num_actions_to_skip;
if (loc >= 0 && loc < action_list.count()) {
// ...
}
Apparently action_list.count()
is a constant value (at least it won't change as this code is executed), and the compiler is able to figure that out.
Let's simplify this a bit, replacing num_actions_to_skip
by 2
, reducing action_list.count()
to count
. We can then re-express loc
as count - 2
.
Your if
condition becomes:
if (count - 2 >= 0 && count - 2 < count)
which is equivalent (assuming, as the compiler warning said, that no overflow occurs) to:
if (count >= 2 && -2 < 0)
The second half of that, -2 > 0
is obviously true, so you can safely drop it, which leaves us with
if (count >= 2)
Re-substituting the original terms, this gives us:
static const int num_actions_to_skip = 2;
// const int loc = action_list.count() - num_actions_to_skip;
if (action_list.count() >= num_actions_to_skip) {
// ...
}
The compiler warned you that it was performing an optimization that might be invalid if there's an integer overflow (it's permitted to assume that there is no overflow because if there is the behavior is undefined). It was kind enough to warn you about this -- which is lucky for you, because it pointed to the fact that your code is doing something it doesn't need to.
I don't know whether you need to keep the declaration of loc
; it depends on whether you use it later. But if you simplify the code in the way I've suggested it should work the same way and be easier to read and understand.
If you get a warning message from the compiler, your goal should not just be to make the message go away; it should be to drill down and figure out just what the compiler is warning you about, and beyond that why your code causes that problem.
You know the context of this code better than I do. If you look at the revised version, you may well find that it expresses the intent more clearly.