Block scope var declaration

This warning category is spelled [block-scope-var-declaration] by qmllint.

A variable has been declared with var inside a block scope

What happened?

A JavaScript variable has been declared with var inside of a block scope, for example in the body of a while loop. While it might look like the variable is only usable inside that scope, it can actually be accessed outside of it, as it is hoisted to the scope of the innermost function or QML binding.

Why is this bad?

It can be confusing to track the scope of such a variable, and they might accidentally shadow other variables.

Example

 import QtQuick

 Item {
     id: root
     property int count: 0
     function countItems() {
         let sum = 0
         {
             var count = root.children.length
             sum += count
         }
         // ... more additions to sum
         count = sum // modifies the function local variable, not root.count
     }
 }

To fix this warning, use let or const instead, or declare the variable directly in the function scope.

 import QtQuick

 Item {
     id: root
     property int count: 0
     function countItems() {
         let sum = 0
         {
             let count = root.children.length
             sum += count
         }
         // ... more additions to sum
         count = sum // modifies the function local variable, not root.count
     }
 }