From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933138AbcFCPZu (ORCPT ); Fri, 3 Jun 2016 11:25:50 -0400 Received: from comal.ext.ti.com ([198.47.26.152]:51825 "EHLO comal.ext.ti.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932445AbcFCPZt (ORCPT ); Fri, 3 Jun 2016 11:25:49 -0400 From: Nishanth Menon To: Joe Perches , Andy Whitcroft CC: , Nishanth Menon Subject: [PATCH] checkpatch: Flag code that returns a negative number Date: Fri, 3 Jun 2016 10:25:33 -0500 Message-ID: <1464967533-14634-1-git-send-email-nm@ti.com> X-Mailer: git-send-email 1.9.1 MIME-Version: 1.0 Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In some functions, returning a -ve decimal value is actually a valid return condition when the function is returning a value, however, it can also be misused for returning an error value that should ideally be a valid error code defined in include/uapi/asm-generic/errno-base.h or include/uapi/asm-generic/errno.h Considering typical newbie error of doing the following: int fn(void) { /* ... error condition ... */ return -1; } void fn1(void) { /* some code */ if (fn() < 0) { pr_err("Error occurred\n"); return; } /* other cases... */ } Flag this as a check case for developer verification. Signed-off-by: Nishanth Menon --- scripts/checkpatch.pl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 4904ced676d4..f6fa07fe33a5 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -4351,6 +4351,12 @@ sub process { } } +# return with a value is not usually a good sign, unless the function is supposed to return a value + if (defined($stat) && $stat =~ /^.\s*return\s*-[0-9]+\s*;/s) { + CHK("RETURN_NUMBER", + "Suspect error return with a value, If this is error value, refer to include/uapi/asm-generic/errno-base.h and include/uapi/asm-generic/errno.h\n" . $herecurr); + } + # unnecessary return in a void function # at end-of-function, with the previous line a single leading tab, then return; # and the line before that not a goto label target like "out:" -- 2.8.0