@@ -441,9 +441,14 @@ impl PreferConst {
441441 return false ;
442442 }
443443
444- // For variables without initializers, check if there's exactly one write-only reference
445- // (not read+write like `a = a + 1`)
446- // The write must be in the same scope and not inside any control flow or loops
444+ // For variables without initializers, there must be exactly one write in total.
445+ // Then, that single write must be write-only (not read+write like `a += 1`).
446+ // The write must be in the same scope and, in general, not inside control flow or loops
447+ // (with explicit exceptions handled below, such as variables declared in for-in/of bodies).
448+ if write_count != 1 {
449+ return false ;
450+ }
451+
447452 let mut write_only_refs = references. iter ( ) . filter ( |r| r. is_write ( ) && !r. is_read ( ) ) ;
448453
449454 let Some ( write_ref) = write_only_refs. next ( ) else {
@@ -684,6 +689,8 @@ fn test() {
684689 ( "var x = 0;" , None ) ,
685690 ( "let x;" , None ) ,
686691 ( "let x; { x = 0; } foo(x);" , None ) ,
692+ ( "let x; x = 0; x += 1;" , None ) ,
693+ ( "let x; x = 0; x = x + 1;" , None ) ,
687694 ( "let x = 0; x = 1;" , None ) ,
688695 ( "using resource = fn();" , None ) , // { "sourceType": "module", "ecmaVersion": 2026, },
689696 ( "await using resource = fn();" , None ) , // { "sourceType": "module", "ecmaVersion": 2026, },
0 commit comments