This is related to Is it okay to code-review and improve OP's code when posting an answer?, but I'm not sure if in certain cases I must change other parts of the code.
When the user is asking a question with some code that doesn't work, and I give a modified version of their code that works, should the code be refactored and errors (more like anti-patterns, the well known wrong way of doing something) that they did not ask about, be solved?
I ask this after noticing some users posting a very modified version of the OP's code, and they also comment on my answer about why I didn't change X to Y or something, which wasn't asked about in the question.
Using this pseudo example:
Hi, my function
code_with_error_user_talked_about
is not working, I tried this and that, and I have the following code:some_random_code() anti_pattern_code_that_can_cause_unexpected_behavior() code_with_error_user_talked_about()
Do I need to address the other issues? Can my answer be directed towards code_with_error_user_talked_about
which is what the user is talking about? My view is that they could have plans for their other code, and their own design decisions aren't my concern. I couldn't find something talking about this.