Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1214287

Re: [PATCH] checkpatch: add --strict "pointer comparison to NULL" test

From Viresh Kumar <viresh.kumar@linaro.org>
Newsgroups linux.kernel
Subject Re: [PATCH] checkpatch: add --strict "pointer comparison to NULL" test
Date 2015-08-27 04:20 +0200
Message-ID <q1UXf-7A4-17@gated-at.bofh.it> (permalink)
References <om7MC-7Ks-7@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


On Fri, Nov 14, 2014 at 12:27 AM, Joe Perches <joe@perches.com> wrote:
> It seems there are more and more uses of "if (!ptr)"
> in preference to "if (ptr == NULL)" so add a --strict
> test to emit a message when using the latter form.
>
> This also finds (ptr != NULL).
>
> Fix it too if desired.
>
> Signed-off-by: Joe Perches <joe@perches.com>
> ---
> Seeing no objections, so submitting it.
>
>  scripts/checkpatch.pl | 14 ++++++++++++++
>  1 file changed, 14 insertions(+)
>
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index 24d6702..d4e08bc 100755
> --- a/scripts/checkpatch.pl
> +++ b/scripts/checkpatch.pl
> @@ -4490,6 +4490,20 @@ sub process {
>                              "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
>                 }
>
> +# check for pointer comparisons to NULL
> +               if ($^V && $^V ge 5.10.0) {
> +                       while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
> +                               my $val = $1;
> +                               my $equal = "!";
> +                               $equal = "" if ($4 eq "!=");
> +                               if (CHK("COMPARISON_TO_NULL",
> +                                       "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
> +                                           $fix) {
> +                                       $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
> +                               }
> +                       }
> +               }
> +

Hi Joe,

Sorry for picking a relatively old thread.

Few colleagues asked me why isn't checkpatch warning for (NULL == ptr)
or (NULL != ptr) checks, as it warns for (ptr == NULL) and (ptr != NULL).

Did you miss it? or was it intentional ?

--
viresh
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Back to linux.kernel | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Re: [PATCH] checkpatch: add --strict "pointer comparison to NULL" test Viresh Kumar <viresh.kumar@linaro.org> - 2015-08-27 04:20 +0200
  Re: [PATCH] checkpatch: add --strict "pointer comparison to NULL"  test Joe Perches <joe@perches.com> - 2015-08-27 05:10 +0200
    Re: [PATCH] checkpatch: add --strict "pointer comparison to NULL"  test Nicolas Morey Chaisemartin <nmorey@kalray.eu> - 2015-08-27 09:20 +0200
      Re: [PATCH] checkpatch: add --strict "pointer comparison to NULL"  test Joe Perches <joe@perches.com> - 2015-08-27 09:30 +0200
        [PATCH] checkpatch: add constant comparison on left side test Joe Perches <joe@perches.com> - 2015-08-27 19:40 +0200

csiph-web