Confusing minuses

This warning category is spelled [confusing-minuses] by qmllint.

Confusing minuses

What happened?

JavaScript code uses a combination of operators written with '-' in a confusing way. The neighboring '-' operators may be difficult to distinguish. This can be made worse by unconventional spacing.

Why is that bad?

It makes the code more difficult to read and may cause confusion.

Example

import QtQuick

Item {
    function f(a: int, b: int) {
        let x = a-- - b
        let y = a - -b
        let z = a - --b
        return x + y + z
    }
}

To fix this warning, rewrite the code so that it doesn't contain similar '-' operators next to each other. Simplify expressions where possible. Remove redundant unary operators and spacing, and use parentheses to isolate subexpressions.

Be mindful that these operators may perform coercions. An expression like a - -b may not be equivalent to a + b depending on the type of b.

import QtQuick

Item {
    function f(a: int, b: int) {
        let x = (a--) - b
        let y = a + b
        let z = a - b + 1
        return x + y + z
    }
}

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.