Skip to content

Commit 073404f

Browse files
dulmarodfacebook-github-bot
authored andcommitted
[website] Improve documentation of MIXED_SELF_WEAKSELF
Summary: Adding examples Reviewed By: ezgicicek Differential Revision: D32648754 fbshipit-source-id: c2af3f0d62
1 parent bb5f81a commit 073404f

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed
Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
1-
This happens when an Objective-C block captures both `self` and `weakSelf`, a
2-
weak pointer to `self`. Possibly the developer meant to capture only `weakSelf`
3-
to avoid a retain cycle, but made a typo and used `self` as well in the block,
1+
This check reports an issue when an Objective-C block captures both `self` and `weakSelf`, a weak pointer to `self`.
2+
Possibly the developer meant to capture only `weakSelf` to avoid a retain cycle, but made a typo and used `self`
43
instead of `strongSelf`. In this case, this could cause a retain cycle.
4+
5+
Example:
6+
7+
```objectivec
8+
__weak __typeof(self) weakSelf = self;
9+
int (^my_block)() = ^() {
10+
__strong __typeof(weakSelf) strongSelf = weakSelf;
11+
if (strongSelf) {
12+
[strongSelf foo];
13+
int x = self->x; // typo here
14+
}
15+
return 0;
16+
};
17+
```
18+
19+
**Action**: Fixing the typo is generally the right course of action.
20+
21+
*Limitations:* To keep this check simple and intra-procedural, we rely on names to find `weakSelf`:
22+
we assume that any captured weak pointer whose name contains "self" is a weak reference to `self`.

website/docs/all-issue-types.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,11 +1459,29 @@ As explained by the analysis.
14591459

14601460
Reported as "Mixed Self WeakSelf" by [self-in-block](/docs/next/checker-self-in-block).
14611461

1462-
This happens when an Objective-C block captures both `self` and `weakSelf`, a
1463-
weak pointer to `self`. Possibly the developer meant to capture only `weakSelf`
1464-
to avoid a retain cycle, but made a typo and used `self` as well in the block,
1462+
This check reports an issue when an Objective-C block captures both `self` and `weakSelf`, a weak pointer to `self`.
1463+
Possibly the developer meant to capture only `weakSelf` to avoid a retain cycle, but made a typo and used `self`
14651464
instead of `strongSelf`. In this case, this could cause a retain cycle.
14661465

1466+
Example:
1467+
1468+
```objectivec
1469+
__weak __typeof(self) weakSelf = self;
1470+
int (^my_block)() = ^() {
1471+
__strong __typeof(weakSelf) strongSelf = weakSelf;
1472+
if (strongSelf) {
1473+
[strongSelf foo];
1474+
int x = self->x; // typo here
1475+
}
1476+
return 0;
1477+
};
1478+
```
1479+
1480+
**Action**: Fixing the typo is generally the right course of action.
1481+
1482+
*Limitations:* To keep this check simple and intra-procedural, we rely on names to find `weakSelf`:
1483+
we assume that any captured weak pointer whose name contains "self" is a weak reference to `self`.
1484+
14671485
## MODIFIES_IMMUTABLE
14681486
14691487
Reported as "Modifies Immutable" by [impurity](/docs/next/checker-impurity).

0 commit comments

Comments
 (0)