Add support for extra cases of BAD_POINTER_COMPARISON warning

Reviewed By: akotulski

Differential Revision: D3674896

fbshipit-source-id: 48d10bf
master
Martino Luca 8 years ago committed by Facebook Github Bot 3
parent 6555a84409
commit b7d70ff178

@ -277,23 +277,20 @@ let captured_cxx_ref_in_objc_block_warning _ stmt_info captured_vars =
let bad_pointer_comparison_warning _ stmt_info stmts = let bad_pointer_comparison_warning _ stmt_info stmts =
let rec condition stmts = let rec condition stmts =
let condition_aux stmt = let condition_aux stmt =
match stmt with match (stmt: Clang_ast_t.stmt) with
| Clang_ast_t.CallExpr _ | BinaryOperator (_, _, _, boi) when
| Clang_ast_t.CXXMemberCallExpr _ (boi.boi_kind = `EQ) || (boi.boi_kind = `NE) -> false
| Clang_ast_t.CXXOperatorCallExpr _ | BinaryOperator (_, stmts, _, _) -> condition stmts
| Clang_ast_t.ObjCMessageExpr _ -> false | UnaryOperator (_, stmts, _, uoi) when uoi.uoi_kind = `LNot ->
| Clang_ast_t.BinaryOperator (_, _, _, boi) condition stmts
when (boi.boi_kind = `NE) || (boi.boi_kind = `EQ) -> false | ImplicitCastExpr (_, stmts, _, _)
| Clang_ast_t.UnaryOperator (_, stmts, _, uoi) when uoi.uoi_kind = `LNot -> | ExprWithCleanups (_, stmts, _, _) ->
condition stmts condition stmts
| stmt -> | stmt ->
match Clang_ast_proj.get_expr_tuple stmt with match Clang_ast_proj.get_expr_tuple stmt with
| Some (_, stmts, expr_info) -> | Some (_, _, expr_info) ->
let typ = CFrontend_utils.Ast_utils.get_desugared_type expr_info.ei_type_ptr in let typ = CFrontend_utils.Ast_utils.get_desugared_type expr_info.ei_type_ptr in
if CFrontend_utils.Ast_utils.is_ptr_to_objc_class typ "NSNumber" then CFrontend_utils.Ast_utils.is_ptr_to_objc_class typ "NSNumber"
true
else
condition stmts
| _ -> false in | _ -> false in
IList.exists condition_aux stmts in IList.exists condition_aux stmts in
if condition stmts then if condition stmts then

@ -53,3 +53,93 @@ void good2() {
i = 0; i = 0;
} }
} }
int good3(NSNumber* number) {
if (number.doubleValue) {
return 0;
}
return 1;
}
//---
@interface Simple : NSObject
@property(strong, atomic) NSNumber* number;
- (NSNumber*)fooWithCondition:(BOOL)number andNumber:(NSNumber*)number;
@end
@implementation Simple
- (NSNumber*)fooWithCondition:(BOOL)condition andNumber:(NSNumber*)number {
return number;
}
@end
//---
int bad4(NSNumber* number, Simple* simple) {
if ([simple fooWithCondition:true andNumber:number] < 19) {
return 0;
}
return 1;
}
int good4(NSNumber* number, Simple* simple) {
if ([simple fooWithCondition:true andNumber:number].doubleValue) {
return 0;
}
return 1;
}
int bad5(NSNumber* number, Simple* simple) {
if ([simple fooWithCondition:(number ? 1 : 2) andNumber:number] != nil) {
return 0;
}
return 1;
}
int bad6(NSNumber* number, Simple* simple) {
if ([simple fooWithCondition:(number == nil ? 1 : 2) andNumber:number]) {
return 0;
}
return 1;
}
int good5(NSNumber* number, Simple* simple) {
if ([simple fooWithCondition:(number == nil ? 1 : 2) andNumber:number] !=
nil) {
return 0;
}
return 1;
}
int bad7(NSNumber* number) {
if (number < 0) {
return 0;
}
return 1;
}
int bad8(NSNumber* number) {
if (number) {
return 0;
}
return 1;
}
int bad9(NSNumber* number, Simple* simple) {
simple.number = simple.number ? simple.number : number;
}
int bad10(NSNumber* number, Simple* simple) {
if ([simple fooWithCondition:true andNumber:number]) {
return 1;
}
return 0;
}
int good6(NSNumber* number) { return (number.integerValue > 5 ? 1 : 0); }

@ -51,7 +51,7 @@ public class NSNumber2Test {
assertThat( assertThat(
"Results should contain " + BAD_POINTER_COMPARISON, "Results should contain " + BAD_POINTER_COMPARISON,
inferResults, inferResults,
containsOnlyLines(new int[]{17, 26, 33})); containsOnlyLines(new int[]{17, 26, 33, 85, 99, 106, 121, 128, 135, 139}));
} }
} }

Loading…
Cancel
Save