2016-04-19 19:12:47

by Joe Perches

[permalink] [raw]
Subject: coccinelle: bool if (foo) return true; else return false;

There's ~150 of these in the kernel.

Maybe there's use for this conversion to be added
to?scripts/coccinelle/misc/boolreturn.cocci or in
a separate file.

$ cat booltruefalse.cocci
@@
identifier fn;
expression e;
typedef bool;
symbol true;
symbol false;
@@

bool fn ( ... )
{
<...
- if (e) return true; else return false;
+ return e;
...>
}

@@
identifier fn;
expression e;
@@

bool fn ( ... )
{
<...
- if (e) return false; else return true;
+ return !e;
...>
}



2016-04-19 19:15:58

by Julia Lawall

[permalink] [raw]
Subject: Re: coccinelle: bool if (foo) return true; else return false;



On Tue, 19 Apr 2016, Joe Perches wrote:

> There's ~150 of these in the kernel.
>
> Maybe there's use for this conversion to be added
> to?scripts/coccinelle/misc/boolreturn.cocci or in
> a separate file.
>
> $ cat booltruefalse.cocci
> @@
> identifier fn;
> expression e;
> typedef bool;
> symbol true;
> symbol false;
> @@
>
> bool fn ( ... )
> {
> <...
> - if (e) return true; else return false;
> + return e;
> ...>
> }
>
> @@
> identifier fn;
> expression e;
> @@
>
> bool fn ( ... )
> {
> <...
> - if (e) return false; else return true;
> + return !e;
> ...>
> }

Thanks for the suggestion. I will take care of it shortly.

julia

2016-04-20 07:19:25

by Michael Stefaniuc

[permalink] [raw]
Subject: Re: [Cocci] coccinelle: bool if (foo) return true; else return false;

On 04/19/2016 09:15 PM, Julia Lawall wrote:
>
>
> On Tue, 19 Apr 2016, Joe Perches wrote:
>
>> There's ~150 of these in the kernel.
>>
>> Maybe there's use for this conversion to be added
>> to scripts/coccinelle/misc/boolreturn.cocci or in
>> a separate file.
>>
>> $ cat booltruefalse.cocci
>> @@
>> identifier fn;
>> expression e;
>> typedef bool;
>> symbol true;
>> symbol false;
>> @@
>>
>> bool fn ( ... )
>> {
>> <...
>> - if (e) return true; else return false;
>> + return e;
Shouldn't that be:
return !!e
?


>> ...>
>> }
>>
>> @@
>> identifier fn;
>> expression e;
>> @@
>>
>> bool fn ( ... )
>> {
>> <...
>> - if (e) return false; else return true;
>> + return !e;
>> ...>
>> }
>
> Thanks for the suggestion. I will take care of it shortly.


bye
michael

2016-04-20 07:25:54

by Joe Perches

[permalink] [raw]
Subject: Re: [Cocci] coccinelle: bool if (foo) return true; else return false;

On Wed, 2016-04-20 at 09:19 +0200, Michael Stefaniuc wrote:
> On 04/19/2016 09:15 PM, Julia Lawall wrote:
> > On Tue, 19 Apr 2016, Joe Perches wrote:
> > > There's ~150 of these in the kernel.
> > >
> > > Maybe there's use for this conversion to be added
> > > to scripts/coccinelle/misc/boolreturn.cocci or in
> > > a separate file.
> > >
> > > $ cat booltruefalse.cocci
> > > @@
> > > identifier fn;
> > > expression e;
> > > typedef bool;
> > > symbol true;
> > > symbol false;
> > > @@
> > >
> > > bool fn ( ... )
> > > {
> > > <...
> > > - if (e) return true; else return false;
> > > + return e;
> Shouldn't that be:
> ????return !!e
> ?

No, it's not necessary.
The compiler does that because the return type is bool

6.3.1.2 Boolean type

When any scalar value is converted to _Bool, the result is 0 if the value compares equal
to 0; otherwise, the result is 1.