[SelfInBlock] Take ns_noescape into account when reporting captured strong self.

Summary: When reporting CapturedStrongSelf we shouldn't report it when the block is "no escaping", as this won't lead to a retain cycle, so capturing strongSelf is ok.

Reviewed By: jvillard

Differential Revision: D20224359

fbshipit-source-id: c60dae333
master
Dulma Churchill 5 years ago committed by Facebook Github Bot
parent 19093a2fa0
commit 634a42b619

@ -471,9 +471,12 @@ let report_weakself_multiple_issue summary domain (weakSelf1 : DomainData.t)
Reporting.log_error summary ~ltr ~loc:weakSelf1.loc IssueType.multiple_weakself message Reporting.log_error summary ~ltr ~loc:weakSelf1.loc IssueType.multiple_weakself message
let report_captured_strongself_issue domain summary (capturedStrongSelf : DomainData.t) let report_captured_strongself_issue domain summary attributes (capturedStrongSelf : DomainData.t)
report_captured_strongself = report_captured_strongself =
if not (Pvar.Set.mem capturedStrongSelf.pvar report_captured_strongself) then ( if
Option.is_none attributes.ProcAttributes.passed_as_noescape_block_to
&& not (Pvar.Set.mem capturedStrongSelf.pvar report_captured_strongself)
then (
let report_captured_strongself = let report_captured_strongself =
Pvar.Set.add capturedStrongSelf.pvar report_captured_strongself Pvar.Set.add capturedStrongSelf.pvar report_captured_strongself
in in
@ -496,7 +499,7 @@ let report_issues summary domain attributes =
match domain_data.kind with match domain_data.kind with
| DomainData.CAPTURED_STRONG_SELF -> | DomainData.CAPTURED_STRONG_SELF ->
let reported_captured_strong_self = let reported_captured_strong_self =
report_captured_strongself_issue domain summary domain_data report_captured_strongself_issue domain summary attributes domain_data
result.reported_captured_strong_self result.reported_captured_strong_self
in in
{result with reported_captured_strong_self} {result with reported_captured_strong_self}

@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*/ */
#include <Foundation/NSObject.h> #include <Foundation/Foundation.h>
@class SelfInBlockTest; @class SelfInBlockTest;
@ -31,6 +31,8 @@
- (void)bar; - (void)bar;
- (A*)process:(A*)obj;
@end @end
void m(SelfInBlockTest* obj) {} void m(SelfInBlockTest* obj) {}
@ -47,6 +49,10 @@ void m2(_Nullable SelfInBlockTest* obj) {}
- (void)bar { - (void)bar {
} }
- (A*)process:(A*)obj {
return obj;
}
- (void)mixSelfWeakSelf_bad { - (void)mixSelfWeakSelf_bad {
__weak __typeof(self) weakSelf = self; __weak __typeof(self) weakSelf = self;
int (^my_block)() = ^() { int (^my_block)() = ^() {
@ -218,6 +224,21 @@ void m2(_Nullable SelfInBlockTest* obj) {}
}; };
} }
- (void)capturedStrongSelf_good:(NSArray<A*>*)allResults {
__weak __typeof(self) weakSelf = self;
int (^my_block)() = ^() {
__strong typeof(self) strongSelf = weakSelf;
if (strongSelf) {
[allResults
enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL* stop) {
A* result =
[strongSelf process:obj]; // no bug because of NS_NOESCAPE flag
}];
}
return 0;
};
}
- (void)mixSelfWeakSelf_good:(NSArray*)resources { - (void)mixSelfWeakSelf_good:(NSArray*)resources {
__weak __typeof(self) weakSelf = self; __weak __typeof(self) weakSelf = self;
int (^my_block)() = ^() { int (^my_block)() = ^() {

Loading…
Cancel
Save