Comment Traps: How Defective Commented-out Code Augment Defects in AI-Assisted Code Generation

Yuan Huang, Yukang Zhou, Xiangping Chen, Zibin Zheng · 2025

Read Comment Traps: How Defective Commented-out Code Augment Defects in AI-Assisted Code Generation by Yuan Huang, Yukang Zhou, Xiangping Chen, Zibin Zheng (2025) in the Tower of Babel, a free online library of public-domain works preserved in clean, searchable text. No sign-up. The full text is served as a plain-text file at a predictable URL, where text is present: https://antarctic-labs.com/tower-of-babel/arxiv-2512-20334.txt.

Introduction

Yuan Huang, Yukang Zhou, Xiangping Chen, and Zibin Zheng test a subtle hazard of AI coding assistants. Code that programmers comment out often still sits in the file. When that commented-out code contains defects, does the assistant absorb the flaws into fresh code it generates?

Experiments with GitHub Copilot and Cursor say yes. Defective commented code in the context drove defective output up sharply, in the worst cases past half of generations. The tools did not merely copy the bad code; they reasoned their way to completing its broken patterns. Even explicit instructions to ignore the commented code cut defects only modestly. The authors argue assistants need stronger robustness and security safeguards.

Catalog record

CREATORYuan Huang, Yukang Zhou, Xiangping Chen, Zibin Zheng
YEAR2025
COLLECTIONPAPERS
FORMATTXT
FILE SIZE51 KB
VERSION1.0
LICENSEhttp://creativecommons.org/licenses/by/4.0/
RIGHTSCC BY
SOURCEarXiv

Source: https://arxiv.org/abs/2512-20334

Browse the full library · About the Tower of Babel

Full text

The opening of the work, from the library's hosted text:

Comment Traps: How Defective Commented-out Code Augment Defects in AI-Assisted Code Generation Thanks: This paper has been accepted by FSE 2026 (ACM International Conference on the Foundations of Software Engineering). This is a preprint version and may differ from the final published version. CCS: Software and its engineering Automatic programming CCS: Software and its engineering Software defect analysis Yuan Huang, Yukang Zhou, Xiangping Chen, Zibin Zheng Abstract . With the rapid development of large language models in code generation, AI-powered editors such as GitHub Copilot and Cursor are revolutionizing software development practices. At the same time, studies have identified potential defects in the generated code. Previous research has predominantly examined how code context influences the generation of defective code, often overlooking the impact of defects within commented-out code (CO code). AI coding assistants’ interpretation of CO code in prompts affects the code they generate. This study evaluates how AI coding assistants, GitHub Copilot and Cursor, are influenced by defective CO code. The experimental results show that defective CO code in the context causes AI coding assistants to generate more defective code, reaching up to 58.17 percent. Our findings further demonstrate that the tools do not simply copy the defective code from the context. Instead, they actively reason to complete incomplete defect patterns and continue to produce defective code despite distractions such as incorrect indentation or tags. Even with explicit instructions to ignore the defective CO code, the reduction in defects does not exceed 21.84 percent. These findings underscore the need for improved robustness and security measures in AI coding assistants. 1. Introduction The integration of large language models (LLMs) with Integrated Development Environments (IDEs) marks a transformative advancement in modern software engineering. Tools such as GitHub Copilot ( 17 ) , Cursor ( 10 ) , and related systems offer developers real-time, intelligent code suggestions, significantly enhancing coding efficiency. Empirical studies indicate that these tools can contribute up to 43 additional lines of code per session ( 21 ) , underscoring their effectiveness in collaborative programming contexts. Nonetheless, despite their substantial utility, recent research highlights notable security concerns in the code generated by such tools. A study of GitHub Copilot’s recommendations revealed that 39.33% of top suggestions and 40.73% of all suggestions contained vulnerabilities classified under MITRE’s Common Weakness Enumerations (CWEs) ( 32 ) . Subsequent evaluations show modest improvements in later versions, with the rate of vulnerable Python code suggestions declining from 36.54% to 27.25% ( 29 ) . However, prior research on code defects has mainly examined executable code ( 32 ; 29 ; 26 ) , neglecting the role of non-executable commented-out code (CO code), a factor that AI coding assistants may interpret as part of the prompt, thereby affecting the generated code generation. CO code refers to code segments temporarily disabled using language-specific comment syntax, allowing developers to retain nonexecutable code within source files ( 33 ) . It is commonly used for debugging, experimenting with alternative implementations, or preserving legacy logic for potential future use. Despite its developmental utility, the impact of CO code on AI-powered programming assistants remains underexplored. Since CO code often forms part of the prompt given to AI coding assistants, defects in this code may lead to the generation of additional flaws. We refer to such instances as defective CO code . While recent studies have examined the effects of natural language comments (e.g., TODOs) on code generation performance ( 31 ) , as well as the influence of contextual elements on GitHub Copilot’s effectiveness ( 14 ; 38 ; 2 ) , the role of CO code has not been systematically analyzed. We use a case study to illustrate the impact of defective CO code. Figure 1(a) shows a secure SSL connection code generated by GitHub Copilot. Lines 12 to 15, highlighted with a green background, contain content generated by GitHub Copilot based on the provided code prompt, which explicitly specifies a secure SSL version. Conversely, Fig. 1(b) reveals that inserting defective CO code in the prompt above the completion point (lines 11 to 14) leads GitHub Copilot to generate flawed code in lines 16 to 19. In this flawed segment, GitHub Copilot invokes the function ssl.wrap_socket without specifying the protocol version, potentially resulting in the use of deprecated or insecure TLS versions. This issue is identified as CWE-327 in the CWEs. (a) (b) Figure 1 . A Case Study on the Impact of Defective CO Code on AI Coding Assistants A Case Study on the Impact of Defective CO Code on AI Coding Assistants. To verify the prevalence of CO code in real-world development, we analyzed 6,403 high-quality Python repositories from GitHub, examining their CO code counts and identifying defects in CO code using CodeQL ( 18 ) . We found that 84.62% of repositories and 13.09% of Python files contained CO code, with 21.19% of those CO code having detectable defects. This highlights that a notable number of such issues exist in practice. Our main experimental procedure is illustrated in Fig. 2 . We scanned Python files to identify defective code segments, which were then extracted and commented to create defective CO code. The remaining parts of the files were preserved as code context, forming a targeted dataset. Next, we inserted the defective CO code into different positions within the corresponding code context to generate prompts containing the defective CO code. These prompts were submitted to the AI coding assistants GitHub Copilot and Cursor, with code generation performed using the GPT-4o and Claude 3.5 Sonnet models. We then examined whether the generated code reintroduced the original defect. The results show that when defective CO code is included in the prompts, the number of defects in the generated code increases. We also evaluated the impact of factors such as tagging, truncation strategies, and the sparsity of the surrounding code context. Given that prompt engineering can influence the output of AI coding assistants ( 13 ) , we assessed its effectiveness in mitigating this issue. To guide this study, we formulated four research questions: RQ1: Impact of CO code on defect rates in AI-generated code. We first examined how defective CO code affects the number of defects in code generated by GitHub Copilot and Cursor. After inserting defective CO code into the code context, the number of defects generated by AI coding assistants exceeds that of the control group without inserting CO code and the control group with random CO code. RQ2: Impact of CO code truncation and tagging on defect rates in AI-generated code. Building on RQ1, we modified defective CO code to explore how AI tools interpret them. Removing 50% of trailing characters shows that tools still generate defects. Adding “ < Vulnerable > ” tags before or after the defective CO code has little effect. RQ3: Effectiveness of prompt engineering in mitigating CO code effects. Subsequently, we examined whether explicitly instructing the AI programming tool to avoid referencing defects mentioned in the defective CO code was effective. The practice of prompting the model to ignore defective CO code and regenerate yielded limited success, as only a small fraction of the outputs became defect-free. RQ4: Impact of code context sparsity on defect rates in AI-generated code influenced by

Read the complete text