Horizontal alignment with align and cases

I have a cases environment inside an align environment, and I'm trying to align part of one line of the align environment with part of the cases environment via an ugly \phantom command. Here's a MWE:

\documentclass{article}
\usepackage{amsmath,mathtools}
\begin{document}
\begin{align}
f(x)&=\mathrlap{2}\phantom{\begin{cases}1&\\0&\end{cases}}\text{if }x>0\\
g(x)&=\begin{cases}1&\text{if }x>0\\0&\text{if }x<0\end{cases}
\end{align}
\end{document}


I'd like the 'if's to be horizontally aligned, and I don't understand why they're not - maybe something to do with the spacing that LaTeX puts around a cases environment.

Can anyone see a fix (other than just doing it 'by hand' with \vspace)?

You're forgetting the \nulldelimiterspace that's added when a null delimiter is used:

\documentclass{article}
\usepackage{amsmath,mathtools}
\begin{document}
\begin{align}
f(x)&=\mathrlap{2}
\hphantom{\begin{cases}1&\\0&\end{cases}}\kern-\nulldelimiterspace
\text{if }x>0\\
g(x)&=\begin{cases}1&\text{if }x>0\\0&\text{if }x<0\end{cases}
\end{align}
\end{document}


You should use \hphantom rather than \phantom, so the vertical size is not taken into account.