The situation is an asker posts broken code. It gets an answer. (Question may get closed as 'off-topic broken code'). OP fixes their code to make it on-topic.
This is directly addressed in the following meta questions:
Where answers should refrain from answering these questions.
Where is says we close your question to prevent answers. And that you should edit the code to not be broken.
Which says OP shouldn't edit code into the question.
I think that the sentiment of the question Preventing the ability to re-open off-topic questions by explaining how it's broken in an answer can come into play here too. Where Mat's Mug's last paragraph explains why we could allow these edits:
If OP wants a peer review and edits their question with the working code, IMO it's the hasty FGITW (fastest gun in the west) non-review answer that should be removed; OP is entitled to have a chance to fix their post and their standing, without having to create a new question (heck, what if they're Q-banned by then?) - it's a matter of fairness.
How should we deal with questions that are edited to become on-topic, if there is already an answer to the question? As there seems to be conflicting information above, should we allow the asker to edit them?