1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//! **`assignment-in-condition`**: `=` where the reader almost certainly meant `==`.
//!
//! ```cpp
//! if (x = 1) { … } // assigns, and the condition is then 1 — always true
//! while (line = read()) { } // …and this one is deliberate, which is why the rule is about the *shape* below
//! ```
//!
//! # The shape, and the one that is excused
//!
//! ```text
//! reported `if (x = 1)`, `while (x = y)`, `for (; x = y;)` an assignment directly in the condition
//! not reported `if ((x = 1))` the extra parentheses are the idiom that says "I meant this", and
//! every compiler warns about it in the same way — a second pair is a
//! deliberate choice rather than a typo
//! not reported `if (x == 1)` a comparison
//! not reported `if (f(x = 1))` the assignment is an argument, not the condition
//! not reported `if (x = y, x > 0)` the condition is the comma expression, whose *last* operand is what is
//! tested — and that operand is not an assignment
//! ```
//!
//! # Read as text, and why that is the honest reading here
//!
//! The condition of an `if` is taken between its first `(` and the matching `)`, and the question is whether that
//! text holds a top-level `=`. A tree walk would have to decide which of the recovered nodes is the condition —
//! and a condition with a mismatched parenthesis is exactly where the grammar recovers hardest. The text is what
//! the reader wrote, and the question is about what they wrote.
use ;
use ;
/// **`assignment-in-condition`** — see the module documentation.
;
/// **The condition of an `if`/`while`, taken from the node and never from its text.**
///
/// This was a text scan — the first `(` and its match — and that is exactly how a real session got a false positive:
/// `if (arg == "--port" && i + 1 < argc) { const auto message = readMessage(); … }` was reported as *an assignment
/// in a condition*, with the body's own `=` in the message. Counting parentheses through text means deciding what a
/// `<` is, and a `<` is a comparison far more often than it is a bracket.
///
/// A node's child is what the parser decided the condition is, and this takes **the one expression before the
/// body**: no scanning, no ambiguity. A condition written as `(x = y)` arrives as a parenthesised expression, whose
/// text is then trimmed — the trimming is safe because the node is already the whole condition.
/// **Is this condition nothing but an assignment?**
///
/// Decided from the **tokens**, which is what makes it trustworthy: a string literal is one token, so an `=` in
/// `if (s == "a=b")` cannot be seen at all, and a compound condition is refused before any `=` is looked for.
///
/// **The whole condition has to be the assignment.** A **compound** — `&&`, `||`, a comma — is not this rule's shape
/// whatever it holds inside: `if (x = f() && ok)` is a rarer and different mistake than `if (x = f())`, and
/// reporting the first as the second is what put a finding on a line whose condition is two comparisons.
///
/// # Why the kind alone was wrong, and the position is the fix
///
/// **The lexer splits `>=` into two tokens on purpose**, so that `>>` can collapse in `vector<vector<int>>`:
///
/// ```text
/// CppTokenKind::GreaterEqual => {
/// tokens.push(part(start, CppTokenKind::Greater));
/// tokens.push(part(start + 1, CppTokenKind::Assign)); // ← the "=" of ">="
/// }
/// ```
///
/// So the `=` of a `>=` arrives as [`CppTokenKind::Assign`], exactly as a real assignment's does, and a test for the
/// *kind* reported both. Measured on
///
/// ```cpp
/// if (a >= b) { … } → Greater@32..33 ">" Assign@33..34 "="
/// if (a = b) { … } → Assign@74..75 "="
/// ```
///
/// that was `if (a >= b) assigns rather than compares`, reported from an editor against real code. One `if (x >=
/// size())` — the most ordinary bounds check there is — was a finding on every occurrence.
///
/// The distinction is **positional**, and it is the grammar's own: a real assignment's `=` is the operator of the
/// expression, so it is the **first** token of the condition, while a split `>=` has its `>` before it. Asking for
/// that asks the tree what the operator was rather than reading punctuation.