I'd be extremely careful about editing in an example of broken code in a question in this manner, though I applaud the reasoning behind why you might consider doing such a thing.
The main problem is that your example code might not match the actual problem, but just the described one. Often, error messages and debugging results might give the wrong impression, and seeing some code can clarify it.
I can see potential value if a question lacks detail, just describes a problem broadly, but gets a stellar answer that solves the described problem - and the asker has accepted the solution. That does seem to suggest that if you can perfectly produce code that reproduces the problem, it would be helpful to future readers. Your thinking there is excellent, in my opinion. I'm just real leery of doing that kind of edit, though.
I guess I'd just be really careful here, and be ready to get some backlash. Be sure you know what you are doing, but be willing to let it go. Certainly don't get involved in an edit war. Oh, also; you might not want to do this sort of edit until you can do edits without approval; my guess is this kind of edit would have a high rate of being rejected.
Another possible option is to post a stellar question yourself. But of course that leaves the duplicate issue. Perhaps if the original question is lacking, that one could be closed - maybe even as a duplicate of yours. But the issue there is it would sort of seem to 'punish' the user who left the good answer. Then again, they answered a 'bad' question, so maybe they deserve that...
Ugh... this is mostly just random thoughts and opinions!