UseIsOperatorRule

1 view
Skip to first unread message

Sanghyeon Seo

unread,
Jan 9, 2008, 7:28:30 AM1/9/08
to gend...@googlegroups.com
On Mono-dev, Juraj Skripsky posted a patch to cleanup "as" casts.
(CC'ing him...)
http://lists.ximian.com/pipermail/mono-devel-list/2008-January/026508.html

One of the cleanup is this (there were 3 of them in Mono corlib):
- return value as EncoderExceptionFallback != null;
+ return (value is EncoderExceptionFallback);

This looks like a perfect candidate for Gendarme rule. So I quickly
wrote one and confirmed that there are no more of these. The file is
attached.

I meant to write Gendarme rule for a while, but this is my first try.
Please comment!

--
Seo Sanghyeon

Sanghyeon Seo

unread,
Jan 9, 2008, 7:42:58 AM1/9/08
to gend...@googlegroups.com
Actually attaching the file...

--
Seo Sanghyeon

UseIsOperatorRule.cs

Sebastien Pouliot

unread,
Jan 9, 2008, 8:43:59 AM1/9/08
to Gendarme
Hello Seo,

On Jan 9, 7:28 am, "Sanghyeon Seo" <sanx...@gmail.com> wrote:
> On Mono-dev, Juraj Skripsky posted a patch to cleanup "as" casts.
> (CC'ing him...)http://lists.ximian.com/pipermail/mono-devel-list/2008-January/026508...
>
> One of the cleanup is this (there were 3 of them in Mono corlib):
> - return value as EncoderExceptionFallback != null;
> + return (value is EncoderExceptionFallback);
>
> This looks like a perfect candidate for Gendarme rule.

Yes, I also flagged this email. Some recent rules, like
FeatureRequiresRootPrivilegeOnUnixRule and ExitCodeIsLimitedOnUnixRule
(both done by GHOP students), have their origins from the mailing-list
and/or recent bugzilla entries.

> So I quickly
> wrote one and confirmed that there are no more of these. The file is
> attached.

What a pleasant surprise, I wish I could wake up and find one each
day :-)

> I meant to write Gendarme rule for a while, but this is my first try.

If that was a resolution for 2008 then you did it ;-)

> Please comment!

* The file is missing a license and your copyright statement. All
rules that we "ship" we Gendarme (i.e. in SVN) are MIT-licensed.

MessageCollection messages = new MessageCollection();

* Try to delay the collection creation until you need it. Rules are
executed a lot of times (e.g. processing corlib) and creating them
(when not needed) takes time and memory. Future version of Gendarme
will remove the need to have this temporary collection.

Location loc = new
Location(method.DeclaringType.FullName, method.Name, i);

* We have, in SVN, new Location constructors, including one that
accept a MethodDefinition and an offset.

Message msg = new Message(System.String.Empty, loc,
MessageType.Warning);

* Please include a message string about the problem. We'll be moving
away from the xml.in files in the future so those messages will be
more important.

* rules needs documentation. not much, just describe the problem,
solution and give a good and bad example. See
http://groups.google.com/group/gendarme/web/rules-documentation (or
the wiki) for examples.

* rules needs unit tests, both valid and defectives test cases should
be included.

I'm short on time right now but I'll try the rule later today (mostly
to look if, or how much, false positives can be returned).

Thanks a lot!
Sebastien :)

Néstor Salceda

unread,
Jan 9, 2008, 11:28:22 AM1/9/08
to gend...@googlegroups.com
Hey Sanghyeon,

On mié, 2008-01-09 at 21:28 +0900, Sanghyeon Seo wrote:
> On Mono-dev, Juraj Skripsky posted a patch to cleanup "as" casts.
> (CC'ing him...)
> http://lists.ximian.com/pipermail/mono-devel-list/2008-January/026508.html
>
> One of the cleanup is this (there were 3 of them in Mono corlib):
> - return value as EncoderExceptionFallback != null;
> + return (value is EncoderExceptionFallback);
>
> This looks like a perfect candidate for Gendarme rule. So I quickly
> wrote one and confirmed that there are no more of these. The file is
> attached.

Sure :)

> I meant to write Gendarme rule for a while, but this is my first try.
> Please comment!

I have only a little comment (Sebastien commented the others), in this
line:

InstructionCollection instructions = method.Body.Instructions;

Before get the Instructions, you should check if the method has body
with the HasBody property, this is because the Gendarme runner will
check also abstract methods, or PInvoke methods or any other method
without body. If you try to access to a method without instructions you
will get an exception.

Finally, you can see some tips and suggestions in this page:
http://groups.google.com/group/gendarme/web/how-to-write-cool-rules

Good work, and thanks !

Néstor.


Néstor Salceda

unread,
Jan 9, 2008, 11:32:10 AM1/9/08
to Gendarme


On Jan 9, 5:28 pm, Néstor Salceda <nestor.salc...@gmail.com> wrote:
> Hey Sanghyeon,


> > This looks like a perfect candidate for Gendarme rule. So I quickly
> > wrote one and confirmed that there are no more of these. The file is
> > attached.
>
> Sure :)
>
> > I meant to write Gendarme rule for a while, but this is my first try.
> > Please comment!
>
> I have only a little comment (Sebastien commented the others), in this
> line:

Ouch !! Sorry, I have missed the beginning.

if (!method.HasBody)
return runner.RuleSuccess;

Please, ignore then my comment.
Reply all
Reply to author
Forward
0 new messages