Fix detection of return type for instancetype when using NS_ASSUME_NONNUL

Reviewed By: jvillard

Differential Revision: D15317686

fbshipit-source-id: a13520499
master
Dino Distefano 6 years ago committed by Facebook Github Bot
parent 12687b2948
commit 10ca0c3269

@ -100,10 +100,11 @@ let rec build_array_type translate_decl tenv (qual_type : Clang_ast_t.qual_type)
and type_desc_of_attr_type translate_decl tenv type_info attr_info =
match type_info.Clang_ast_t.ti_desugared_type with
let open Clang_ast_t in
match type_info.ti_desugared_type with
| Some type_ptr -> (
match CAst_utils.get_type type_ptr with
| Some (Clang_ast_t.ObjCObjectPointerType (_, qual_type)) ->
| Some (ObjCObjectPointerType (_, qual_type)) ->
let typ = qual_type_to_sil_type translate_decl tenv qual_type in
Typ.Tptr (typ, pointer_attribute_of_objc_attribute attr_info)
| _ ->

@ -511,6 +511,12 @@ and c_type_equal c_type abs_ctype =
match (c_type, abs_ctype) with
| BuiltinType (_, bi), BuiltIn abi ->
builtin_equal bi abi
| BuiltinType (_, `ObjCId), TypeName ae when ALVar.compare_str_with_alexp "instancetype" ae ->
(* This is a special case where coming from an AttributedType with {ati_attr_kind=`Nonnull} where the
compiler change 'instancetype' to ObjCId *)
L.(debug Linters Medium)
"@\n Special Case when comaping BuiltInType(ObjcId) and TypeName(instancetype)\n" ;
true
| TypedefType (_, tdi), BuiltIn _ ->
check_type_ptr tdi.tti_child_type.qt_type_ptr abs_ctype
| PointerType _, BuiltIn _ | PointerType _, Pointer _ | ObjCObjectPointerType _, Pointer _ ->
@ -522,6 +528,8 @@ and c_type_equal c_type abs_ctype =
| FunctionProtoType (_, {fti_return_type= qt}, _), TypeName _
| ObjCObjectPointerType (_, qt), _ ->
check_type_ptr qt.qt_type_ptr abs_ctype
| ObjCObjectType (_, ooti), TypeName _ ->
check_type_ptr ooti.ooti_base_type abs_ctype
| ObjCObjectType _, ObjCGenProt _ ->
objc_object_type_equal c_type abs_ctype
| ObjCInterfaceType (_, pointer), TypeName ae ->
@ -554,6 +562,8 @@ and c_type_equal c_type abs_ctype =
| TypedefType (ti, _), ObjCGenProt _
| AttributedType (ti, _), Pointer _ -> (
match ti.ti_desugared_type with Some dt -> check_type_ptr dt abs_ctype | None -> false )
| AttributedType (ti, _), TypeName _ -> (
match ti.ti_desugared_type with Some dt -> check_type_ptr dt abs_ctype | None -> false )
| _, _ ->
display_equality_warning () ; false

@ -241,6 +241,9 @@ codetoanalyze/objc/linters-for-test-only/implicit_cast.c, main, 9, TEST_IMPLICIT
codetoanalyze/objc/linters-for-test-only/implicit_cast.c, main, 9, TEST_VAR_TYPE_CHECK, no_bucket, WARNING, []
codetoanalyze/objc/linters-for-test-only/namespace.mm, Linters_dummy_method, 7, TEST_DEFINE_NAMESPACE, no_bucket, WARNING, []
codetoanalyze/objc/linters-for-test-only/namespace.mm, Linters_dummy_method, 15, TEST_USING_NAMESPACE, no_bucket, WARNING, []
codetoanalyze/objc/linters-for-test-only/ns_assume_nonnull.m, Blah::someMethod, 14, TEST_INSTANCE_TYPE, no_bucket, WARNING, []
codetoanalyze/objc/linters-for-test-only/ns_assume_nonnull.m, Blah::someMethod, 20, TEST_INSTANCE_TYPE, no_bucket, WARNING, []
codetoanalyze/objc/linters-for-test-only/ns_assume_nonnull.m, Blah::someMethod, 20, TEST_IS_METHOD_EXPOSED, no_bucket, WARNING, []
codetoanalyze/objc/linters-for-test-only/protocols.m, Foo::newWithA, 22, TEST_INSTANCE_TYPE, no_bucket, WARNING, []
codetoanalyze/objc/linters-for-test-only/protocols.m, Foo::newWithA, 22, TEST_PROTOCOL_TYPE_INHERITANCE, no_bucket, WARNING, []
codetoanalyze/objc/linters-for-test-only/protocols.m, Foo::newWithB, 23, TEST_INSTANCE_TYPE, no_bucket, WARNING, []

@ -0,0 +1,26 @@
/*
* Copyright (c) 2019-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <Foundation/NSObject.h>
NS_ASSUME_NONNULL_BEGIN
@interface Blah : NSObject
+ (instancetype)someMethod;
@end
@implementation Blah
+ (instancetype)someMethod {
return nil;
}
@end
NS_ASSUME_NONNULL_END
Loading…
Cancel
Save