linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Joe Perches <joe@perches.com>
To: Andrew Morton <akpm@linux-foundation.org>,
	Andy Whitcroft <apw@canonical.com>
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH v2] checkpatch: Suggest using min_t or max_t
Date: Thu, 26 May 2011 17:35:02 -0700	[thread overview]
Message-ID: <7f59aa30d3af02ae522dc38105e51107e34826f2.1306455946.git.joe@perches.com> (raw)
In-Reply-To: <20110525165345.5f775c7b.akpm@linux-foundation.org>

A common issue with min() or max() is using a cast on
one or both of the arguments when using min_t/max_t could
be better.

Add cast detection to uses of min/max and suggest an
appropriate use of min_t or max_t instead.

Caveat:  This only works for min() or max() on a single line.
         It does not find min() or max() split across multiple lines.

This does find:
	min((u32)foo, bar);
But it does not find:
	max((unsigned long)foo,
	    bar);

Suggested-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Joe Perches <joe@perches.com>
---

v2: Make $match_balanced_parentheses work in perl 5.8

 scripts/checkpatch.pl |   35 +++++++++++++++++++++++++++++++++++
 1 files changed, 35 insertions(+), 0 deletions(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 8657f99..897aff8 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -268,6 +268,20 @@ sub build_types {
 }
 build_types();
 
+our $match_balanced_parentheses = qr/(\((?:[^\(\)]+|(-1))*\))/;
+
+our $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
+our $LvalOrFunc	= qr{($Lval)\s*($match_balanced_parentheses{0,1})\s*};
+
+sub deparenthesize {
+	my ($string) = @_;
+	return "" if (!defined($string));
+	$string =~ s@^\s*\(\s*@@g;
+	$string =~ s@\s*\)\s*$@@g;
+	$string =~ s@\s+@ @g;
+	return $string;
+}
+
 $chk_signoff = 0 if ($file);
 
 my @dep_includes = ();
@@ -2285,6 +2299,27 @@ sub process {
 			}
 		}
 
+# typecasts on min/max could be min_t/max_t
+		if ($line =~ /^\+(?:.*?)\b(min|max)\s*\($Typecast{0,1}($LvalOrFunc)\s*,\s*$Typecast{0,1}($LvalOrFunc)\s*\)/) {
+			if (defined $2 || defined $8) {
+				my $call = $1;
+				my $cast1 = deparenthesize($2);
+				my $arg1 = $3;
+				my $cast2 = deparenthesize($8);
+				my $arg2 = $9;
+				my $cast;
+
+				if ($cast1 ne "" && $cast2 ne "") {
+					$cast = "$cast1 or $cast2";
+				} elsif ($cast1 ne "") {
+					$cast = $cast1;
+				} else {
+					$cast = $cast2;
+				}
+				WARN("$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . $herecurr);
+			}
+		}
+
 # Need a space before open parenthesis after if, while etc
 		if ($line=~/\b(if|while|for|switch)\(/) {
 			ERROR("space required before the open parenthesis '('\n" . $herecurr);
-- 
1.7.5.2.365.g5cfe4

  reply	other threads:[~2011-05-27  0:35 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-19 12:21 [PATCH v3] w1: Add Maxim/Dallas DS2780 Stand-Alone Fuel Gauge IC support Clifton Barnes
2011-05-19 19:03 ` Andrew Morton
2011-05-19 20:21 ` Ryan Mallon
     [not found] ` <20110519115859.e11a7ca3.akpm@linux-foundation.org>
     [not found]   ` <1305909981.4209.33.camel@Joe-Laptop>
     [not found]     ` <20110520095037.25eadc0a.akpm@linux-foundation.org>
     [not found]       ` <1305912602.4209.41.camel@Joe-Laptop>
     [not found]         ` <20110520103529.1ef3917c.akpm@linux-foundation.org>
     [not found]           ` <1305915161.4209.56.camel@Joe-Laptop>
     [not found]             ` <20110520113830.3faf5230.akpm@linux-foundation.org>
2011-05-20 20:24               ` [PATCH] checkpatch: Suggest using min_t or max_t Joe Perches
2011-05-24 23:35                 ` Andrew Morton
2011-05-25  0:11                   ` Joe Perches
2011-05-25 23:53                     ` Andrew Morton
2011-05-27  0:35                       ` Joe Perches [this message]
2012-09-05 11:21                         ` [PATCH v2] " Philippe De Muyter
2012-09-05 17:07                           ` Joe Perches
2012-09-06  0:16                             ` Philippe De Muyter
2012-09-06  0:35                               ` Joe Perches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7f59aa30d3af02ae522dc38105e51107e34826f2.1306455946.git.joe@perches.com \
    --to=joe@perches.com \
    --cc=akpm@linux-foundation.org \
    --cc=apw@canonical.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).