Maybe the problem falls under the ‘missing steadfastness’ umbrella.
I don’t think the compiler should bother about this case (too easy ), because there are so many more difficult points when it come to coroutining (attributed variables), so extensively used today.
From this thread, there should be a ‘lint like’ tool, sorry I haven’t tried it, and crashed swi-prolog.org while trying to follow a Google search suggestion
(https://www.swi-prolog.org/pack/file_details/logtalk/logtalk-3.41.0/manuals/_sources/devtools/linter.rst.txt)