Example 3 If $f(1+\cos x)=\sin ^{2} x+\cos x-1$, then $f(x)=$ $\qquad$ .
Notice that,
$$
\begin{array}{l}
f(1+\cos x)=\sin ^{2} x+\cos x-1 \\
=-\cos ^{2} x+\cos x .
\end{array}
$$

Let $1+\cos x=m$. Then $\cos x=m-1$.
So $f(m)=-(m-1)^{2}+m-1$ $=-m^{2}+3 m-2$.
Therefore, $f(x)=-x^{2}+3 x-2$.