Currently Available: Need a skilled Software Developer for your next project?
Categories
Coding Agents

How to Run Safe Code Cleanup Passes With a Coding Agent

When a coding agent edits a repository, it can remove code that appears unused, consolidate similar code, or apply lint fixes. A linter checks source code against rules for common mistakes and style. Dead code is code that no longer contributes to the program’s behavior. Duplication means similar code appears in more than one place.

These labels help identify code to review, but they do not prove a change is safe. A framework, plugin, runtime lookup, generated registration, or external caller may use code the agent cannot see. Similar code may also handle behavior that needs to stay separate. Ask the agent to explain each candidate before editing. Then have it make one kind of change at a time and check each change against the repository’s tests and review rules.

Give the agent a bounded task

Before editing, check the repository’s working tree and note any existing changes. Git’s status command shows which files have staged or unstaged edits. Run the formatter, linter, tests, and build, and record which commands pass or fail. Compare these results with later runs to see whether a failure existed before cleanup or appeared afterward.

For the first request, choose one directory or subsystem and one kind of change, such as removing unused local variables. Tell the agent which files to leave alone and whether it may edit tests or lint configuration. Provide the project’s build and test commands, coding conventions, locations of generated code, and known ways the project registers code. GitHub’s guidance for Copilot tasks likewise recommends supplying repository-specific build, test, and coding-convention context.

Ask the agent to list candidates before you approve any edits. For each one, it should show where the code appears, why it considers the code unused or duplicated, and what ways of using it indirectly it checked. If the agent cannot tell whether runtime registration or an external user relies on the code, leave it in place until a developer verifies it.

Check how code can be used indirectly

A static analyzer checks the code and configuration available to it. It flags code as unused based on the ways its rules recognize that code being accessed or used. For example, ESLint’s no-unused-vars rule identifies variables and functions that appear unused according to its analysis. ESLint also documents that custom rules can tell the linter when a variable counts as used, illustrating how project-specific behavior can affect a warning.

Before deleting a flagged function, check whether a framework registers it by name, a plugin loads it while the program runs, generated code refers to it, or another package calls it. Reflection, which lets a program find or invoke code at runtime, can hide references from ordinary static analysis. Research on analyzing Java reflection describes why such behavior complicates static analysis. Languages and repositories handle these cases differently. Ask the agent to find evidence in the codebase instead of assuming that no visible reference means no reference exists.

Analyzer findings also have limits. A study of historical issues in PMD, SpotBugs, and SonarQube examined 350 reports of false positives and false negatives, then found additional issues in proof-of-concept testing. These results do not give an overall error rate for current linters or coding agents. They do show why you should review a warning as a candidate finding, not treat it as proof.

Consolidate duplication only when the code should change together

A clone detector finds code fragments that look alike. It cannot decide whether they belong in one function or shared abstraction. Two validation blocks, for example, might look nearly identical while enforcing different rules that should evolve separately. Ask the agent to compare what each fragment does and where it is used. It should point out meaningful differences and explain whether future changes should apply to both.

Research shows why clone counts alone are a poor decision rule. A 2025 study of duplication-related refactoring commits in 128 open-source Java projects found that structural quality metrics did not all reflect developers’ intentions in the same way. Earlier research on duplicate code describes maintenance risks, including the chance that a developer changes one copy and overlooks another. Together, these findings support checking why a specific pair of fragments exists and how developers have changed them. Do this before removing code just because a detector found a match.

Review test code separately. A study of five open-source systems reported twice as many clones in test code as in production code. That finding describes those projects, not every repository. Repeating setup can make each test easier to understand. A shared helper can make the tests harder to follow. Keep duplication when the behavior is intentionally distinct or a shared abstraction would obscure what a test checks.

Apply lint fixes in a separate batch

Run autofixes in a narrow scope and inspect what the linter changed. An autofix makes edits automatically, but you still need to review the diff. Pay close attention when a fix changes the order or conditions of execution, or changes configuration. Keep lint-rule changes separate from code cleanup so reviewers can tell whether a behavior change came from the source edit or a new rule.

Do not let a cleanup request quietly upgrade tools, rewrite lint configuration, or change unrelated files. If a rule warns about code that follows a documented project convention, ask the agent to explain the conflict and suggest a separate configuration change for review. ESLint’s documentation on custom rules describes how a project can encode patterns that the default analysis does not recognize.

Verify each batch before moving on

After each small batch, run the relevant linter, tests, and build commands. Compare their results with the baseline. For changes in shared code, run broader checks that cover its callers. Coverage reports show which statements and branches the tests ran. Coverage.py’s branch coverage documentation explains that a test can run a line without testing every possible outcome of that line. Passing tests provide useful evidence, but they do not show that the tests covered every runtime configuration or external caller.

Inspect the full diff for deleted code and test edits. Review configuration changes separately, and check whether any files outside the requested scope were edited. Git’s diff command lets you compare working-tree or staged changes with a reference. Reject unrelated edits, and do not accept test deletions or weakened assertions as a way to make the cleanup pass.

A task prompt can make the expected limits explicit:

Audit the specified directory for unused code and obvious duplication. First list each candidate, the evidence for it, possible indirect uses, and your confidence. Do not edit files yet. Do not remove or weaken tests, change public APIs, alter lint configuration, update dependencies, or modify generated files. After approval, make only the agreed changes in a small batch. Run the existing lint, test, and build commands, compare results with the recorded baseline, and summarize the full diff and unresolved uncertainty.

Repository instructions can guide an agent to follow local conventions, but they cannot ensure that its changes work correctly. Before merging, require the repository’s usual code review and continuous integration checks. GitHub protected branches can require status checks and approvals; verify that those checks cover the affected code.

What I'm building

Delegate tasks. Get software.

Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.

Take a look at vroni.com

Email updates

Usually a new article and a few links I found interesting.

No spam. Unsubscribe with one click.

Leave a Reply

Your email address will not be published. Required fields are marked *