Many times I reuse help coordinate names in tikz pictures.
Normally, they are overwritten.
Now I have found an instance where they are maintained.
\documentclass{article}
\usepackage{tikz}
\begin{document}
Reuse of coordinate names overwrites them:
\begin{tikzpicture}
\foreach \x in {1,...,5}
\node (A) at (\x,0){.};
\draw[red] (A) circle(2pt);
\end{tikzpicture}
When placed in a macro also:
\def\overwritecoords{%
\foreach \x in {1,...,5}
\node (A) at (\x,0){.};}
\begin{tikzpicture}
\overwritecoords
\draw[green] (A) circle(2pt);
\end{tikzpicture}
But when the \verb|\foreach| uses a macro, not:
\def\placecoord{\node (A) at (\x,0){.};}
\def\keepcoords{%
\foreach \x in {1,...,5}
\placecoord}
\begin{tikzpicture}
\keepcoords
\draw[blue] (A) circle(2pt);
\end{tikzpicture}
Although, they can only be used once:
\begin{tikzpicture}
\keepcoords
\draw[blue] (A) circle(2pt);
\draw[->] (A)--++(0,-1);
\end{tikzpicture}
\begin{tikzpicture}
\keepcoords
\draw[->] (A)--++(0,-1);
\draw[blue] (A) circle(2pt);
\end{tikzpicture}
If the same name is reused again, the previous ones are lost, as usual:
\begin{tikzpicture}
\keepcoords
\node (A) at (6,0){.};
\draw[orange] (A) circle(2pt);
\end{tikzpicture}
\end{document}
I could not find a description of this behaviour but, since the manual is so extensive, I might have overlooked it.
I do not understand why and how should tikz maintain a list of coordinates with the same name, when normally they are overwritten.
I am a bit worried that at some point, in such a set-up, tikz might pick the wrong help coordinate, or more than one, for drawing my picture.
Could that happen?
Is there a way to avoid the accumulation of coordinates with the same name?
On the other hand, if I use this behaviour for some purpose, would that be stable in new updates of the package?

\drawcommand in your\keepcoordsis being drawn in to the\foreachloop. I don't know enough about how\foreachshould be delimited to know why this is happening, but placing a semi-colon after\placecoordin\keepcoordsstops this, as does writing\keepcoords;. – Andrew Stacey Feb 16 '21 at 17:12