diff --git a/infer/src/checkers/SelfInBlock.ml b/infer/src/checkers/SelfInBlock.ml index c0040ac53f..e2481061df 100644 --- a/infer/src/checkers/SelfInBlock.ml +++ b/infer/src/checkers/SelfInBlock.ml @@ -375,7 +375,7 @@ module TransferFunctions = struct let clear_unchecked_use_args attributes args (astate : Domain.t) = let clear_unchecked_use_non_nullable_arg astate (arg, _) annotation = match arg with - | Exp.Var id when Annotations.ia_is_nullable annotation -> + | Exp.Var id when not (Annotations.ia_is_nonnull annotation) -> Domain.clear_unchecked_use id astate | _ -> astate diff --git a/infer/tests/codetoanalyze/objc/self-in-block/StrongSelf.m b/infer/tests/codetoanalyze/objc/self-in-block/StrongSelf.m index 763cdb40f8..855faa6922 100644 --- a/infer/tests/codetoanalyze/objc/self-in-block/StrongSelf.m +++ b/infer/tests/codetoanalyze/objc/self-in-block/StrongSelf.m @@ -45,6 +45,8 @@ void m(SelfInBlockTest* obj) {} void m2(_Nullable SelfInBlockTest* obj) {} +void m3(_Nonnull SelfInBlockTest* obj) {} + @implementation SelfInBlockTest { int x; NSString* _name; @@ -116,7 +118,7 @@ - (void)strongSelfCheckOnce_bad { }; } -- (void)strongSelfCheck2_bad { +- (void)strongSelfCheckNoAnnot_good { __weak __typeof(self) weakSelf = self; int (^my_block)(BOOL) = ^(BOOL isTapped) { __strong __typeof(weakSelf) strongSelf = weakSelf; @@ -124,8 +126,7 @@ - (void)strongSelfCheck2_bad { [strongSelf foo]; int x = strongSelf->x; } else { - m(strongSelf); // bug here - int x = strongSelf->x; + m(strongSelf); // no bug here } return 0; }; @@ -135,7 +136,16 @@ - (void)strongSelfCheck6_good { __weak __typeof(self) weakSelf = self; int (^my_block)(BOOL) = ^(BOOL isTapped) { __strong __typeof(weakSelf) strongSelf = weakSelf; - m2(strongSelf); // bug here + m2(strongSelf); // no bug here + return 0; + }; +} + +- (void)strongSelfCheckNonnullArg_bad { + __weak __typeof(self) weakSelf = self; + int (^my_block)(BOOL) = ^(BOOL isTapped) { + __strong __typeof(weakSelf) strongSelf = weakSelf; + m3(strongSelf); // bug here return 0; }; } diff --git a/infer/tests/codetoanalyze/objc/self-in-block/issues.exp b/infer/tests/codetoanalyze/objc/self-in-block/issues.exp index 941bf81bf5..60ba0c4309 100644 --- a/infer/tests/codetoanalyze/objc/self-in-block/issues.exp +++ b/infer/tests/codetoanalyze/objc/self-in-block/issues.exp @@ -6,25 +6,25 @@ codetoanalyze/objc/self-in-block/NoescapeBlock.m, objc_block_NoescapeBlock.m:35, codetoanalyze/objc/self-in-block/NoescapeBlock.m, objc_block_NoescapeBlock.m:89, 1, WEAK_SELF_IN_NO_ESCAPE_BLOCK, no_bucket, ERROR, [Using &weakSelf] codetoanalyze/objc/self-in-block/SelfInBlockPassedToInit.m, objc_block_SelfInBlockPassedToInit.m:17, 1, SELF_IN_BLOCK_PASSED_TO_INIT, no_bucket, ERROR, [Using captured &self,Using captured &self] codetoanalyze/objc/self-in-block/SelfInBlockPassedToInit.m, objc_block_SelfInBlockPassedToInit.m:17, 2, SELF_IN_BLOCK_PASSED_TO_INIT, no_bucket, ERROR, [Using captured &self,Using captured &self] -codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:65, 4, MIXED_SELF_WEAKSELF, no_bucket, ERROR, [Using &weakSelf,Using &self,Using &self], "self"=>"strongSelf"@69:15 -codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:65, 5, MIXED_SELF_WEAKSELF, no_bucket, ERROR, [Using &weakSelf,Using &self,Using &self], "self"=>"strongSelf"@70:8 -codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:78, 1, CAPTURED_STRONG_SELF, no_bucket, ERROR, [Using captured &weakSelf] -codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:97, 2, STRONG_SELF_NOT_CHECKED, no_bucket, ERROR, [Using &strongSelf] -codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:106, 6, STRONG_SELF_NOT_CHECKED, no_bucket, ERROR, [Using &strongSelf,Using &strongSelf,Using &strongSelf,Using &strongSelf], +""=>"\n if (!strongSelf) { return; }"@112:1 -codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:121, 6, STRONG_SELF_NOT_CHECKED, no_bucket, ERROR, [Using &strongSelf,Using &strongSelf,Using &strongSelf] -codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:158, 2, STRONG_SELF_NOT_CHECKED, no_bucket, ERROR, [Using &strongSelf,Using &strongSelf] -codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:170, 2, STRONG_SELF_NOT_CHECKED, no_bucket, ERROR, [Using &strongSelf,Using &strongSelf] -codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:179, 3, STRONG_SELF_NOT_CHECKED, no_bucket, ERROR, [Using &strongSelf,Using &strongSelf] -codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:208, 1, MULTIPLE_WEAKSELF, no_bucket, ERROR, [Using &weakSelf,Using &weakSelf] -codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:229, 2, MULTIPLE_WEAKSELF, no_bucket, ERROR, [Using &weakSelf,Using &weakSelf] -codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:243, 1, CAPTURED_STRONG_SELF, no_bucket, ERROR, [Using captured &strongSelf,Using captured &strongSelf] -codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:243, 2, CAPTURED_STRONG_SELF, no_bucket, ERROR, [Using captured &strongSelf,Using captured &strongSelf] -codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:286, 4, MIXED_SELF_WEAKSELF, no_bucket, ERROR, [Using &weakSelf,Using &self] -codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:301, 1, MIXED_SELF_WEAKSELF, no_bucket, ERROR, [Using &self,Using &weakSelf] -codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:337, 3, MIXED_SELF_WEAKSELF, no_bucket, ERROR, [macro expanded here,Using &weakSelf,Using &self], "self"=>"strongSelf"@340:6 -codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:351, 4, MIXED_SELF_WEAKSELF, no_bucket, ERROR, [Using &weakSelf,macro expanded here,Using &self] -codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:363, 4, MIXED_SELF_WEAKSELF, no_bucket, ERROR, [Using &weakSelf,Using &self,Using &self], ""=>"strongSelf->"@367:7 -codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:363, 5, MIXED_SELF_WEAKSELF, no_bucket, ERROR, [Using &weakSelf,Using &self,Using &self], ""=>"strongSelf->"@368:7 -codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:379, 4, MIXED_SELF_WEAKSELF, no_bucket, ERROR, [Using &weakSelf,Using &self] -codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:391, 3, STRONG_SELF_NOT_CHECKED, no_bucket, ERROR, [Using &strongSelf] -codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:404, 3, STRONG_SELF_NOT_CHECKED, no_bucket, ERROR, [Using &strongSelf,Using &strongSelf] +codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:67, 4, MIXED_SELF_WEAKSELF, no_bucket, ERROR, [Using &weakSelf,Using &self,Using &self], "self"=>"strongSelf"@71:15 +codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:67, 5, MIXED_SELF_WEAKSELF, no_bucket, ERROR, [Using &weakSelf,Using &self,Using &self], "self"=>"strongSelf"@72:8 +codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:80, 1, CAPTURED_STRONG_SELF, no_bucket, ERROR, [Using captured &weakSelf] +codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:99, 2, STRONG_SELF_NOT_CHECKED, no_bucket, ERROR, [Using &strongSelf] +codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:108, 6, STRONG_SELF_NOT_CHECKED, no_bucket, ERROR, [Using &strongSelf,Using &strongSelf,Using &strongSelf,Using &strongSelf], +""=>"\n if (!strongSelf) { return; }"@114:1 +codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:146, 2, STRONG_SELF_NOT_CHECKED, no_bucket, ERROR, [Using &strongSelf] +codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:168, 2, STRONG_SELF_NOT_CHECKED, no_bucket, ERROR, [Using &strongSelf,Using &strongSelf] +codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:180, 2, STRONG_SELF_NOT_CHECKED, no_bucket, ERROR, [Using &strongSelf,Using &strongSelf] +codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:189, 3, STRONG_SELF_NOT_CHECKED, no_bucket, ERROR, [Using &strongSelf,Using &strongSelf] +codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:218, 1, MULTIPLE_WEAKSELF, no_bucket, ERROR, [Using &weakSelf,Using &weakSelf] +codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:239, 2, MULTIPLE_WEAKSELF, no_bucket, ERROR, [Using &weakSelf,Using &weakSelf] +codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:253, 1, CAPTURED_STRONG_SELF, no_bucket, ERROR, [Using captured &strongSelf,Using captured &strongSelf] +codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:253, 2, CAPTURED_STRONG_SELF, no_bucket, ERROR, [Using captured &strongSelf,Using captured &strongSelf] +codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:296, 4, MIXED_SELF_WEAKSELF, no_bucket, ERROR, [Using &weakSelf,Using &self] +codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:311, 1, MIXED_SELF_WEAKSELF, no_bucket, ERROR, [Using &self,Using &weakSelf] +codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:347, 3, MIXED_SELF_WEAKSELF, no_bucket, ERROR, [macro expanded here,Using &weakSelf,Using &self], "self"=>"strongSelf"@350:6 +codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:361, 4, MIXED_SELF_WEAKSELF, no_bucket, ERROR, [Using &weakSelf,macro expanded here,Using &self] +codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:373, 4, MIXED_SELF_WEAKSELF, no_bucket, ERROR, [Using &weakSelf,Using &self,Using &self], ""=>"strongSelf->"@377:7 +codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:373, 5, MIXED_SELF_WEAKSELF, no_bucket, ERROR, [Using &weakSelf,Using &self,Using &self], ""=>"strongSelf->"@378:7 +codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:389, 4, MIXED_SELF_WEAKSELF, no_bucket, ERROR, [Using &weakSelf,Using &self] +codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:401, 3, STRONG_SELF_NOT_CHECKED, no_bucket, ERROR, [Using &strongSelf] +codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:414, 3, STRONG_SELF_NOT_CHECKED, no_bucket, ERROR, [Using &strongSelf,Using &strongSelf]