lkml.org 
[lkml]   [2021]   [Aug]   [5]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: [PATCH] checkpatch: prefer = {} initializations to = {0}


On Thu, 5 Aug 2021, Joe Perches wrote:

> On Thu, 2021-08-05 at 20:17 +0200, Julia Lawall wrote:
> > On Thu, 5 Aug 2021, Joe Perches wrote:
> > > On Thu, 2021-08-05 at 05:27 -0700, Joe Perches wrote:
> > > > On Thu, 2021-08-05 at 13:43 +0300, Dan Carpenter wrote:
> > > > > The "= {};" style empty struct initializer is preferred over = {0}.
> > > > > It avoids the situation where the first struct member is a pointer and
> > > > > that generates a Sparse warning about assigning using zero instead of
> > > > > NULL. Also it's just nicer to look at.
> > >
> > > Perhaps a cocci script like the below could help too:
> > >
> > > $ cat zero_init_struct.cocci
> > > @@
> > > identifier name;
> > > identifier t;
> > > @@
> > >
> > > struct name t = {
> > > - 0
> > > };
> > >
> > > @@
> > > identifier name;
> > > identifier t;
> > > identifier member;
> > > @@
> > >
> > > struct name t = {
> > > ...,
> > > .member = {
> > > - 0
> > > },
> > > ...,
> > > };
> >
> > My test turns up over 1900 occurrences. There is the question of whether
> > {} or { } is preferred. The above semantic patch replaces {0} by {} and
> > ( 0 } by { }.
>
> I saw that and I don't recall how to force one style or another
> to be output.

If you remove something and put it back, then Coccinelle takes care of
pretty printing it. So the following produces {} everywhere. Fortunately
Dan seems to prefer that...

@@
identifier name;
identifier t;
@@

struct name t =
- {0}
+ {}
;

@@
identifier name;
identifier t;
identifier member;
@@

struct name t = {
...,
.member =
- {0}
+ {}
,
...,
};

julia

\
 
 \ /
  Last update: 2021-08-05 20:47    [W:0.085 / U:0.012 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site