Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
confusing-pluses.qdoc
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\ingroup qmllint-warnings-and-errors
6
7\title Confusing pluses
8\brief [confusing-pluses] Confusing pluses.
9
10\qmllintwarningcategory confusing-pluses
11
12\section1 Confusing pluses
13
14\section2 What happened?
15JavaScript code uses a combination of operators written with '+' in a confusing
16way. The neighboring '+' operators may be difficult to distinguish. This can be
17made worse by unconventional spacing.
18
19\section2 Why is that bad?
20It makes the code more difficult to read and may cause confusion.
21
22\section2 Example
23\qml
24import QtQuick
25
26Item {
27 function f(a: int, b: int) {
28 let x = a++ + b
29 let y = a + +b
30 let z = a + ++b
31 return x + y + z
32 }
33}
34\endqml
35To fix this warning, rewrite the code so that it doesn't contain similar '+'
36operators next to each other. Simplify expressions where possible. Remove
37redundant unary operators and spacing, and use parentheses to isolate
38subexpressions.
39
40Be mindful that these operators may perform coercions. An expression like
41\c{a + +b} may not be equivalent to \c{a + b} depending on the type of b.
42\qml
43import QtQuick
44
45Item {
46 function f(a: int, b: int) {
47 let x = (a++) + b
48 let y = a + b
49 let z = a + b + 1
50 return x + y + z
51 }
52}
53\endqml
54*/