Where is locality used in CHSH/Bell's inequality? A very similar question is asked here, but I'm still confused :(
From Bell, in a hidden variable model, $A = A(\lambda, a)=\pm 1$ is the observed spin of the first particle around axis $a$, and $B = B(\lambda, b)=\pm 1$ is the same for the 2nd particle around $b$. The CHSH proof is then
$E(AB)+E(A'B)+E(AB')-E(A'B')=E(AB+A'B+AB'-A'B')\leq 2$, since $|AB+A'B+AB'-A'B'|=|A(B+B')+A'(B-B')|\leq 2$. 
But we could do the same trick if $A$ depends on $b$ too, so where is locality used? The link says that $E(AB)+E(A'B)+E(AB')-E(A'B')=E(AB+A'B+AB'-A'B')$ is unjustified, but aren't expectations always linear?
 A: For better clarity I will here be using the notation $A_0$ and $A_1$, instead of $A$ and $A'$, to denote the outcomes for different measurement setups, and same with $B$.
The expectation values are defined as
$$
E(A_xB_y)\equiv\int d\lambda\, q(\lambda)\, E_\lambda(A_xB_y)
=\int d\lambda\, q(\lambda)\sum_{ab} ab \,p(ab|xy,\lambda),
$$
where $q(\lambda)$ is the probability of the hidden variable having the value $\lambda$, and $p(ab|xy,\lambda)$ is the (joint) probability of getting the outcomes $a$ and $b$ given the measurement settings $x$ and $y$ and hidden variable $\lambda$.
The locality assumption (plus assumption of independence of measurement choices) is embedded in the following factorization for $p$:
$$p(ab|xy,\lambda)=p(a|x,\lambda)p(b|y,\lambda).\tag A$$
This relation is needed to have $E_\lambda(A_x B_y)=E_\lambda(A_x)E_\lambda(B_y)$, so that
\begin{aligned}
E(A_0B_0)+E(A_0B_1)
&=\int d\lambda \,q(\lambda)[E_\lambda(A_0)E_\lambda(B_0)+E_\lambda(A_0)E_\lambda(B_1)] \\
&= \int d\lambda \,q(\lambda)E_\lambda(A_0)[E_\lambda(B_0)+E_\lambda(B_1)],
\end{aligned}
where
$$E_\lambda(A_x)\equiv\sum_a a \,p(a|x,\lambda),\quad
E_\lambda(B_y)\equiv\sum_b b \,p(b|y,\lambda).$$
Without locality assumption, the above would not hold.
An analogous argument leads to
$$E_\lambda(A_1B_0)-E_\lambda(A_1B_1) = E_\lambda(A_1)[E_\lambda(B_0)-E_\lambda(B_1)].$$
The conclusion is now straightforward from here. Define
$$S_\lambda\equiv E_\lambda(A_0B_0)+E_\lambda(A_0B_1)+E_\lambda(A_1B_0)-E_\lambda(A_1B_1).$$
Then, using the above equalities, we have
$$S_\lambda= E_\lambda(A_0)[E_\lambda(B_0)+E_\lambda(B_1)]
+ E_\lambda(A_1)[E_\lambda(B_0)-E_\lambda(B_1)].$$
The triangle inequality, together with the fact that, by definition of the numbers we are attaching to the possible outputs, we have $0\le E_\lambda(A_i)\le 1$, now gives
$$\lvert S_\lambda\rvert\le \lvert E_\lambda(B_0)+E_\lambda(B_1)\rvert
+ \lvert E_\lambda(B_0) - E_\lambda(B_1)\rvert = 2 \max(E_\lambda(B_1),E_\lambda(B_2)).$$
Using again that by definition $0\le E_\lambda(B_i)\le 1$, we conclude that $\lvert S_\lambda\rvert \le 2$.
The full $S$ is now defined by averaging $S_\lambda$ over the hidden variable $\lambda$, and because a convex mixture of numbers in $[-2,2]$ remains in $[-2,2]$, we reach the conclusion:
$$\lvert S\rvert\le 2.$$

But we could do the same trick if A depends on b too, so where is locality used?

No, you could not.
If the outcome of $A$ directly depends on $B$, then (A) does not hold, thus the probabilities do not factorize ($E(AB)\neq E(A)E(B)$), thus $E(A_0B_0)+E(A_0B_1)\neq E(A_0(B_0+B_1))$, thus the CHSH argument cannot be applied.

Aren't expectations always linear?

Indeed they are. The locality assumption is needed not for the linearity but to factorize the probabilities/expectation values, in order to put them into the form $E_\lambda(A_0)[E_\lambda(B_0)+E_\lambda(B_1)]$, at which point the CHSH argument applies.
