First, I think this is a good question, one that I wish more students considered. The function
$$
f(x) = \begin{cases}x+5, & x < 0\\ x, & x > 0 \end{cases}
$$
has a derivative which is always positive ($1$, in fact) when it exists. However, it is clearly not injective. The problem is that the domain is disconnected, coming in two separated pieces as $(-\infty, 0)$ versus $(0, +\infty)$.
The symptom: the fact you wish to use is really the MVT in disguise, but that demands an interval as part of the hypothesis. And, sure, enough, $f$ is injective when restricted to any connected segment in its domain. The same thing happens with your $\tan x$ example.
Finally, the answer to your question: it is safe to do this when the derivative is strictly positive (or negative) across an interval. Also, having a zero in the derivative isn't necessarily bad, but you have to be careful (e.g., $f(x)=x^3$, which is still injective).