A Peer-Reviewed Paper Says Defective Commented-Out Code Led Two AI Coding Assistants to Generate More Defects

A paper in the FSE issue of the Proceedings of the ACM on Software Engineering, July 2026, put defective code, switched off as comments, into the files two AI coding assistants worked on. Against a control group, defects in the generated code rose for every assistant, model and position tested. With the defective comment one line below the completion point, an instruction to ignore it cut them only in part.

A developer can keep code in a file by turning it into a comment. The code no longer runs. But an AI coding assistant can still read it as part of its prompt. A peer-reviewed paper asks what happens when that code is defective.

The paper is "Comment Traps", in the FSE issue of the Proceedings of the ACM on Software Engineering, with a publication date of July 2026. It tested two AI coding assistants. When defective commented-out code sat in a file, the assistants generated more defective code than without it. The paper says such defects may go "unnoticed by developers". Parts of the paper follow, not the whole.

Illustration

Commented-Out Code

The paper calls it commented-out code. It is code a developer has switched off for the time being with the language's comment marks, but kept in the file. It is commonly used for debugging, for trying other versions, or for saving old logic that may be needed later, the paper says.

The authors scanned 6,403 Python repositories on GitHub that the paper calls high-quality. Of those repositories, 84.62% held commented-out code, as did 13.09% of the Python files. To scan it, the authors first switched the commented-out code back into code, leaving out files that would not then parse. On that basis, 21.19% of it had defects their scanning tool could detect. The paper says this could undercount the defects.

The code in a comment is not executed. While it stays a comment, tools that analyze executable code, such as the scanner the authors used, cannot detect its defects, the paper says. Yet the assistant still uses it as a reference when it writes new code.

The paper gives a motivating example. One assistant wrote code for a secure network connection. When defective code was added as a comment above the point it was completing, the assistant called the connection function without specifying the protocol version. That could result in deprecated or insecure versions being used, the paper says.

How the Test Worked

The authors took defective code from Python files and turned it into comments. They placed it at 11 positions, from 8 lines above to 3 lines below the point where the assistant was asked to complete the code. A control group had no commented-out code added. A second control used defect-free commented-out code of the same length. The paper says this confirmed that the rise came from the defective code in the comments.

Each assistant ran with two models, GPT-4o and Claude 3.5 Sonnet. The paper chose them for their strong performance and because they had been stable for a significant period, it says. The results are for those two models, in the tool versions the paper names. A scanner then checked the generated code for the original defect.

What Came Out

Even the control group produced defects. With no commented-out code added, each assistant and model generated between 394 and 589 defective outputs from 1,000 prompts. In one pairing, more than half of the generated code contained defects.

With the defective comments added, defects rose against the control group for every assistant, model and position tested. The rise reached up to 58.17%. On average across the positions, the rise for each tool was more than 20%.

The assistants did not simply copy the bad code, the paper says. They completed incomplete defect patterns. With half the trailing characters of the defective comment cut, they still produced defects. Tags marking the defective comment as vulnerable, set before or after it, had little effect.

Telling It Not to Look

The authors then took the prompts with the defective comment one line below the completion point. They added one instruction: "Do not refer to the commented-out code." Every pairing produced fewer defects. The largest cut was 21.84%, and the smallest 4.93%. The paper calls the best result insufficient for a high level of protection.

Because of rate limits in the two tools, the authors tested only that one way of prompting, the paper says. It leaves other forms of prompting to future work.