This is not a bug that should be caught by testing, but by simply looking at the code and thinking about it (and often getting others to do the same for your code.)
The current fashion of "write some code, write some tests, modify either until the tests pass" is not a way towards fewer bugs. All it leads to is the analogy of "throwing things on a wall and seeing what sticks". Tests should be for reducing the remaining uncertainty about the functioning of some code, and not a substitute for actually trying to get it right through careful reasoning.