It is clear that the function
$$f : \mathbb R^{m \times n} \to \mathbb N, \qquad X \mapsto \mbox{rank}(X)$$
has no derivative at all $X$ because the image of $f(X)$ assume values in the natural set. On the other hand, we know that the rank of any matrix can be computed by
$$\mbox{rank}(X) = \mbox{tr} \left( X^+ X \right) \tag{1}$$
where $\text{tr}$ is the trace operator and $X^+$ is the Moore-Penrose pseudoinverse of $X$. Notice, however, that the RHS of $(1)$ is differentiable everywhere (the trace operator and the pseudo inverse have derivatives) and it can be computed as
$$f'(X) = (X^T\otimes I_n)\left(-(X^+)^T \otimes X^+ \right) + (I_n \otimes X^+).$$
I am confusing if the rank has or not derivative at all points $X$. Probably I have made some mistake and I am missing something. I need some help. Thanks in advance!
Similar behavior happens with the nuclear norm (see Derivative of nuclear norm).