From 27a7f2e7988daa99f9ae132898d2cb5ade5d8dbe Mon Sep 17 00:00:00 2001 From: Dulma Churchill Date: Mon, 14 May 2018 05:37:37 -0700 Subject: [PATCH] [linters] Do not report mutable local variable in weakSelf and strongSelf Reviewed By: mbouaziz Differential Revision: D7988399 fbshipit-source-id: aab2d10 --- infer/src/clang/ComponentKit.ml | 8 ++++++++ .../linters/componentkit/MutableLocalVariablesTest.mm | 7 +++++++ infer/tests/codetoanalyze/objcpp/linters/issues.exp | 1 + 3 files changed, 16 insertions(+) diff --git a/infer/src/clang/ComponentKit.ml b/infer/src/clang/ComponentKit.ml index 798ee5724..d472d775a 100644 --- a/infer/src/clang/ComponentKit.ml +++ b/infer/src/clang/ComponentKit.ml @@ -132,11 +132,19 @@ let mutable_local_vars_advice context an = false in let is_const = qual_type.qt_is_const || is_const_ref in + let name_is decl name = + match decl with + | Clang_ast_t.VarDecl (_, named_decl_info, _, _) -> + String.equal name named_decl_info.Clang_ast_t.ni_name + | _ -> + false + in let should_not_report_mutable_local = CAst_utils.is_syntactically_global_var decl || CAst_utils.is_static_local_var decl || is_const || is_of_whitelisted_type qual_type || decl_info.di_is_implicit || context.CLintersContext.in_for_loop_declaration || CAst_utils.is_std_vector qual_type || CAst_utils.has_block_attribute decl + || name_is decl "weakSelf" || name_is decl "strongSelf" in if should_not_report_mutable_local then None else diff --git a/infer/tests/codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm b/infer/tests/codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm index eac766c55..b78ac1b30 100644 --- a/infer/tests/codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm +++ b/infer/tests/codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm @@ -126,4 +126,11 @@ class BarClass { return nil; } + +- (void)no_mutable_local_variable_in_self_aliases { + __weak auto weakSelf = self; // no error + __strong auto strongSelf = self; // no error + auto comp = [BarComponent new]; // error +} + @end diff --git a/infer/tests/codetoanalyze/objcpp/linters/issues.exp b/infer/tests/codetoanalyze/objcpp/linters/issues.exp index 19a3ace1e..221a34d65 100644 --- a/infer/tests/codetoanalyze/objcpp/linters/issues.exp +++ b/infer/tests/codetoanalyze/objcpp/linters/issues.exp @@ -16,6 +16,7 @@ codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm, BarCompo codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm, BarComponent_new, 92, MUTABLE_LOCAL_VARIABLE_IN_COMPONENT_FILE, ADVICE, [] codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm, BarComponent_new, 111, MUTABLE_LOCAL_VARIABLE_IN_COMPONENT_FILE, ADVICE, [] codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm, BarComponent_new, 117, MUTABLE_LOCAL_VARIABLE_IN_COMPONENT_FILE, ADVICE, [] +codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm, BarComponent_no_mutable_local_variable_in_self_aliases, 133, MUTABLE_LOCAL_VARIABLE_IN_COMPONENT_FILE, ADVICE, [] codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm, FooComponent_newWithString, 52, MUTABLE_LOCAL_VARIABLE_IN_COMPONENT_FILE, ADVICE, [] codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm, FooComponent_newWithString, 57, MUTABLE_LOCAL_VARIABLE_IN_COMPONENT_FILE, ADVICE, [] codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm, FooComponent_newWithString, 60, MUTABLE_LOCAL_VARIABLE_IN_COMPONENT_FILE, ADVICE, []