[Tex/LaTex] Item numbers with listings

#enumeratelistings

The following MWE

\documentclass{paper}
\usepackage{listings}

\begin{document}

\begin{enumerate}
    \item
    \begin{lstlisting}
Hello, world
    \end{lstlisting}
    \item
    \begin{lstlisting}
Hello, world
    \end{lstlisting}
    \item
    \begin{lstlisting}
Hello, world
    \end{lstlisting}
\end{enumerate}

\end{document}

has the item numbers end up in the wrong position:

enter image description here

Am I doing something wrong with my items? Is this a bug with listings?

Best Answer

Adding a \leavevmode after each lstlisting environment solves the issue (credits). I used etoolbox with \AfterEndEnvironment to have it added automatically:

\documentclass{paper}
\usepackage{listings}
\usepackage{etoolbox}
\AfterEndEnvironment{lstlisting}{\leavevmode}

\begin{document}

\begin{enumerate}
    \item
    \begin{lstlisting}
Hello, world
    \end{lstlisting}
    \item
    \begin{lstlisting}
Hello, world
    \end{lstlisting}
    \item
    \begin{lstlisting}
Hello, world
    \end{lstlisting}
\end{enumerate}

\end{document}

enter image description here