From 4a6652faef77868772a31f783a78e404e012c774 Mon Sep 17 00:00:00 2001 From: Dulma Churchill Date: Tue, 11 Jul 2017 09:29:58 -0700 Subject: [PATCH] [linters] Do not report mutable local variable in components advice on static locals Reviewed By: jberdine Differential Revision: D5397446 fbshipit-source-id: eeeb9b4 --- infer/src/clang/ComponentKit.ml | 3 ++- infer/src/clang/cAst_utils.ml | 3 +++ infer/src/clang/cAst_utils.mli | 3 +++ .../componentkit/MutableLocalVariablesTest.mm | 1 + .../tests/codetoanalyze/objcpp/linters/issues.exp | 14 +++++++------- 5 files changed, 16 insertions(+), 8 deletions(-) diff --git a/infer/src/clang/ComponentKit.ml b/infer/src/clang/ComponentKit.ml index 93773761a..c326379f4 100644 --- a/infer/src/clang/ComponentKit.ml +++ b/infer/src/clang/ComponentKit.ml @@ -124,7 +124,8 @@ let mutable_local_vars_advice context an = let is_const = qual_type.qt_is_const || is_const_ref in let condition = is_ck_context context an && not (CAst_utils.is_syntactically_global_var decl) - && not is_const && not (is_of_whitelisted_type qual_type) && not decl_info.di_is_implicit + && not (CAst_utils.is_static_local_var decl) && not is_const + && not (is_of_whitelisted_type qual_type) && not decl_info.di_is_implicit in if condition then Some diff --git a/infer/src/clang/cAst_utils.ml b/infer/src/clang/cAst_utils.ml index ff1fc8100..9ef84a2e0 100644 --- a/infer/src/clang/cAst_utils.ml +++ b/infer/src/clang/cAst_utils.ml @@ -235,6 +235,9 @@ let is_syntactically_global_var decl = | _ -> false +let is_static_local_var decl = + match decl with Clang_ast_t.VarDecl (_, _, _, vdi) -> vdi.vdi_is_static_local | _ -> false + let is_const_expr_var decl = match decl with Clang_ast_t.VarDecl (_, _, _, vdi) -> vdi.vdi_is_const_expr | _ -> false diff --git a/infer/src/clang/cAst_utils.mli b/infer/src/clang/cAst_utils.mli index 1f41a25e0..34ee1e897 100644 --- a/infer/src/clang/cAst_utils.mli +++ b/infer/src/clang/cAst_utils.mli @@ -89,6 +89,9 @@ val exists_eventually_st : ('a -> Clang_ast_t.stmt -> bool) -> 'a -> Clang_ast_t val is_syntactically_global_var : Clang_ast_t.decl -> bool (** true if a declaration is a global variable *) +val is_static_local_var : Clang_ast_t.decl -> bool +(** true if a declaration is a static local variable *) + val is_const_expr_var : Clang_ast_t.decl -> bool (** true if a declaration is a constexpr variable *) diff --git a/infer/tests/codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm b/infer/tests/codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm index 4bfc083db..eac766c55 100644 --- a/infer/tests/codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm +++ b/infer/tests/codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm @@ -56,6 +56,7 @@ // Objc types NSString* a = @"lol"; // error NSString* const b = @"lol"; // no error + static NSString* st = @"lol"; // no error const NSString* c = @"lol"; // error const NSString* const d = @"lol"; // no error diff --git a/infer/tests/codetoanalyze/objcpp/linters/issues.exp b/infer/tests/codetoanalyze/objcpp/linters/issues.exp index a6f779383..4c0e827b6 100644 --- a/infer/tests/codetoanalyze/objcpp/linters/issues.exp +++ b/infer/tests/codetoanalyze/objcpp/linters/issues.exp @@ -12,15 +12,15 @@ codetoanalyze/objcpp/linters/componentkit/MultipleFactoryMethodsTest.h, Linters_ codetoanalyze/objcpp/linters/componentkit/MultipleFactoryMethodsTest.h, Linters_dummy_method, 58, COMPONENT_WITH_MULTIPLE_FACTORY_METHODS, [] codetoanalyze/objcpp/linters/componentkit/MultipleFactoryMethodsTest.h, Linters_dummy_method, 59, COMPONENT_WITH_MULTIPLE_FACTORY_METHODS, [] codetoanalyze/objcpp/linters/componentkit/MultipleFactoryMethodsTest.h, Linters_dummy_method, 60, COMPONENT_WITH_MULTIPLE_FACTORY_METHODS, [] -codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm, BarComponent_new, 89, MUTABLE_LOCAL_VARIABLE_IN_COMPONENT_FILE, [] -codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm, BarComponent_new, 91, MUTABLE_LOCAL_VARIABLE_IN_COMPONENT_FILE, [] -codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm, BarComponent_new, 110, MUTABLE_LOCAL_VARIABLE_IN_COMPONENT_FILE, [] -codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm, BarComponent_new, 116, MUTABLE_LOCAL_VARIABLE_IN_COMPONENT_FILE, [] +codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm, BarComponent_new, 90, MUTABLE_LOCAL_VARIABLE_IN_COMPONENT_FILE, [] +codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm, BarComponent_new, 92, MUTABLE_LOCAL_VARIABLE_IN_COMPONENT_FILE, [] +codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm, BarComponent_new, 111, MUTABLE_LOCAL_VARIABLE_IN_COMPONENT_FILE, [] +codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm, BarComponent_new, 117, MUTABLE_LOCAL_VARIABLE_IN_COMPONENT_FILE, [] codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm, FooComponent_newWithString:, 52, MUTABLE_LOCAL_VARIABLE_IN_COMPONENT_FILE, [] codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm, FooComponent_newWithString:, 57, MUTABLE_LOCAL_VARIABLE_IN_COMPONENT_FILE, [] -codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm, FooComponent_newWithString:, 59, MUTABLE_LOCAL_VARIABLE_IN_COMPONENT_FILE, [] -codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm, FooComponent_newWithString:, 63, MUTABLE_LOCAL_VARIABLE_IN_COMPONENT_FILE, [] -codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm, FooComponent_newWithString:, 68, MUTABLE_LOCAL_VARIABLE_IN_COMPONENT_FILE, [] +codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm, FooComponent_newWithString:, 60, MUTABLE_LOCAL_VARIABLE_IN_COMPONENT_FILE, [] +codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm, FooComponent_newWithString:, 64, MUTABLE_LOCAL_VARIABLE_IN_COMPONENT_FILE, [] +codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm, FooComponent_newWithString:, 69, MUTABLE_LOCAL_VARIABLE_IN_COMPONENT_FILE, [] codetoanalyze/objcpp/linters/componentkit/MutableLocalVariablesTest.mm, SomeClass_init, 40, MUTABLE_LOCAL_VARIABLE_IN_COMPONENT_FILE, [] codetoanalyze/objcpp/linters/componentkit/UnconventionalSuperclassTest.h, Linters_dummy_method, 17, COMPONENT_WITH_UNCONVENTIONAL_SUPERCLASS, [] codetoanalyze/objcpp/linters/cxx_reference_in_block/block.mm, A_foo3:param2:, 37, CXX_REFERENCE_CAPTURED_IN_OBJC_BLOCK, []