// TL;DR

When you edit existing code: change only what the task requires. Don't "improve" adjacent code, comments, or formatting. When your changes leave imports or variables unused, clean them up — but only after your changes, not after others'.

Why the rule exists

A PR that both fixes a bug and refactors 10 unrelated lines is impossible to peer-review. The reviewer can't tell the important change from the noise. And if the bug regresses, git bisect is now worse because the commit touches too many files.

Drive-by improvements aren't free. They cost reviewer time, increase merge conflicts, and bury the actual change in a flood of irrelevant diff lines.

When you change existing code

When your changes leave orphaned imports

If your changes leave an import, variable, or function unused — remove them. That's still "clean up your own mess."

But: don't remove pre-existing dead code unless explicitly asked. It's not your task in this change.

// Example

You remove a method that called logger.warn(). If the logger import is now unused — remove it. It became orphaned because of your change.

But if you see a totally unrelated unused import at the top of the file — leave it. It was already there.

When exceptions apply

The user can explicitly ask to "clean up" or "modernize" — then the rule is suspended for that scope. But it has to be an explicit request, not an assumption from your side.

If you stumble onto genuinely dangerous problems (security hole, race condition) in adjacent code — flag it to the user. Don't fix it silently. That's a new task, not part of your current one.

Do / Don't

// DO

  • Read the existing code style and match it.
  • Remove imports/vars that your changes made unused.
  • Mention dead code you see, in a comment or separate message.
  • Keep the diff as small as possible.

// DON'T

  • Don't refactor "while I'm here."
  • Don't reformat files (newlines, indentation) beyond where you change something.
  • Don't delete pre-existing dead code without being asked.
  • Don't rewrite comments because you don't like the tone.

Related rules