From 68a9b7070a0f955bdb47b4b2cb55273606dc701f Mon Sep 17 00:00:00 2001 From: Dan Carpenter Date: Fri, 27 Jan 2017 15:55:08 +0300 Subject: [PATCH] assign_vs_compare: warn about "if (foo = &bar) " This doesn't trigger any warnings in the kernel... These sorts of bugs are really rare because checkpatch.pl complains if you write code like that that it should instead be: foo = &bar; if (foo) { Anyway, I've already written this check and it also has no false positives so I'm going to commit it. Signed-off-by: Dan Carpenter --- check_assign_vs_compare.c | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/check_assign_vs_compare.c b/check_assign_vs_compare.c index 8259d988..59006333 100644 --- a/check_assign_vs_compare.c +++ b/check_assign_vs_compare.c @@ -19,18 +19,44 @@ static int my_id; -static void match_condition(struct expression *expr) +static void check_constant(struct expression *expr) { sval_t val; - if (expr->type != EXPR_ASSIGNMENT || expr->op != '=') - return; - if (!get_value(expr->right, &val)) return; sm_msg("warn: was '== %s' instead of '='", sval_to_str(val)); } +static void check_address(struct expression *expr) +{ + char *str; + struct expression *right = strip_expr(expr->right); + + if (!__cur_stmt || __cur_stmt->type != STMT_IF) + return; + + if (right->type != EXPR_PREOP || + right->op != '&') + return; + + if (get_macro_name(expr->pos)) + return; + + str = expr_to_str(right); + sm_msg("warn: was '== %s' instead of '='", str); + free_string(str); +} + +static void match_condition(struct expression *expr) +{ + if (expr->type != EXPR_ASSIGNMENT || expr->op != '=') + return; + + check_constant(expr); + check_address(expr); +} + void check_assign_vs_compare(int id) { my_id = id; -- 2.11.4.GIT