From 6d32e45c46cbaa7a5dd1231ad5cc83e92e153c58 Mon Sep 17 00:00:00 2001 From: Dulma Churchill Date: Wed, 30 Oct 2024 04:30:31 -0700 Subject: [PATCH] [self-in-block] Report all the instances of self use in Mixed self weakSelf Summary: We were returning only one instance so far. We want to return all because now we are interested in fixing all instances with autofixes and codemods. Reviewed By: skcho Differential Revision: D65151572 fbshipit-source-id: 8d40c21985ca85a055e67013540b54c06f5dcc6b --- infer/src/checkers/SelfInBlock.ml | 15 +++++++++------ .../codetoanalyze/objc/self-in-block/StrongSelf.m | 1 + .../codetoanalyze/objc/self-in-block/issues.exp | 6 ++++-- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/infer/src/checkers/SelfInBlock.ml b/infer/src/checkers/SelfInBlock.ml index d97cfea5a8..bbc9bdea2b 100644 --- a/infer/src/checkers/SelfInBlock.ml +++ b/infer/src/checkers/SelfInBlock.ml @@ -817,12 +817,15 @@ let report_issues proc_desc err_log domain = let {weakSelfList; selfList} = List.fold_left ~f:process_domain_item ~init:report_issues_result_empty domain_bindings in - let weakSelfList = List.rev weakSelfList in - let selfList = List.rev selfList in - ( match (weakSelfList, selfList) with - | weakSelf :: _, self :: _ -> - report_mix_self_weakself_issues proc_desc err_log domain weakSelf self - | _ -> + let weakSelfList = + List.sort + ~compare:(fun el1 el2 -> Location.compare el1.DomainData.loc el2.DomainData.loc) + weakSelfList + in + ( match List.hd weakSelfList with + | Some weakSelf -> + List.iter ~f:(report_mix_self_weakself_issues proc_desc err_log domain weakSelf) selfList + | None -> () ) ; match weakSelfList with | weakSelf1 :: weakSelf2 :: _ -> diff --git a/infer/tests/codetoanalyze/objc/self-in-block/StrongSelf.m b/infer/tests/codetoanalyze/objc/self-in-block/StrongSelf.m index 41240eeea8..be6499092c 100644 --- a/infer/tests/codetoanalyze/objc/self-in-block/StrongSelf.m +++ b/infer/tests/codetoanalyze/objc/self-in-block/StrongSelf.m @@ -367,6 +367,7 @@ - (void)mixSelfWeakSelf_bad_autofix_implicit_self { if (strongSelf) { [strongSelf foo]; _name = @"Dulma"; + x = 10; } 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 f001ffd608..f50ed05a4c 100644 --- a/infer/tests/codetoanalyze/objc/self-in-block/issues.exp +++ b/infer/tests/codetoanalyze/objc/self-in-block/issues.exp @@ -7,6 +7,7 @@ codetoanalyze/objc/self-in-block/NoescapeBlock.m, objc_block_NoescapeBlock.m:89, 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:106, 6, STRONG_SELF_NOT_CHECKED, no_bucket, ERROR, [&strongSelf assigned here,Using &strongSelf not checked for null,Using &strongSelf not checked for null], +""=>"\n if (!strongSelf) { return; }"@108:1 codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:121, 6, STRONG_SELF_NOT_CHECKED, no_bucket, ERROR, [&strongSelf assigned here,Using &strongSelf not checked for null,Using &strongSelf not checked for null] @@ -20,5 +21,6 @@ codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:288, 4, M codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:303, 1, MIXED_SELF_WEAKSELF, no_bucket, ERROR, [Using &self,Using &weakSelf] codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:339, 3, MIXED_SELF_WEAKSELF, no_bucket, ERROR, [macro expanded here,Using &weakSelf,Using &self], "self"=>"strongSelf"@342:6 codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:353, 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:365, 4, MIXED_SELF_WEAKSELF, no_bucket, ERROR, [Using &weakSelf,Using &self], ""=>"strongSelf->"@369:7 -codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:380, 4, MIXED_SELF_WEAKSELF, no_bucket, ERROR, [Using &weakSelf,Using &self] +codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:365, 4, MIXED_SELF_WEAKSELF, no_bucket, ERROR, [Using &weakSelf,Using &self,Using &self], ""=>"strongSelf->"@369:7 +codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:365, 5, MIXED_SELF_WEAKSELF, no_bucket, ERROR, [Using &weakSelf,Using &self,Using &self], ""=>"strongSelf->"@370:7 +codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:381, 4, MIXED_SELF_WEAKSELF, no_bucket, ERROR, [Using &weakSelf,Using &self]