If $|f(x)-f(y)|\le(x-y)^2$ for all $x,y\in\mathbb R$, then it’s easy to show that $f’=0$ everywhere, and the mean value theorem implies that that means $f$ is constant. If there were a gap in the real line and $f=3$ on one side of the gap and $f=4$ on the other side of the gap, then $f’=0$ everywhere but $f$ is not constant. Gaplessness enters via the mean value theorem.
But I wonder if the proposition holds even in a line with gaps. For example if $f:\mathbb Q\to\mathbb Q$ and for all $x,y\in\mathbb Q$ this inequality holds, does that imply $f$ is constant? And what about other gap-filled lines than $\mathbb Q$? Such as $\mathbb R\setminus A$ where $A$ is a finite set, or a countable set, or a set whose complement is dense, or whatever set it might be of interest to ask this question about?
You only need the set where $f$ is defined to be dense. The theorem is true in metric spaces, and in that generality, density is more than enough, all you really need is the ability to move from point to point in arbitrarily short steps.
Hopping from $x$ to $y$ in $n$ short steps of size $O(1/n)$ bounds $|f(x)-f(y)|$ as $n O(1/n^2)$, which goes to zero for large $n$.
For the general case in a metric space, define the connected components for “hopping” by $x \sim y$ if for every $e > 0$ there is a finite chain of points starting at $x$ and ending at $y$, with consecutive points all at distance $ < e$ (and a bound, independent of $e$, on the total length of the consecutive jumps). Then
a function satisying the inequality is constant on hopping components
any two hopping components are at positive finite distance from each other (a “gap”), if the metric space is compact
the function can have different values for different components.
The exact amount of freedom to define $f$ differently on the components is an interesting and potentially more difficult question when there are infinitely many components or, in the noncompact case, pairs of components that come arbitrarily close to each other.
The only property of the upper bound $(x-y)^2$ that is used here is that it vanishes to order higher than $1$ for small $|x-y|$.
Let $x = x_0 < x_1 < \dots < x_n = y$ be a family of sequences such that $|x_i – x_{i+1}| \rightarrow 0$ Then use triangle inequality and you can make $|f(x) – f(y)|$ arbitrarily small.