Set resharper to warn on "var x = notNew.someMethod();" statements

Hi,

Resharper is pretty great! We will be buing a couple of licenses. We have once concern though:

We understand using var may increase code readability and manageability in some situations. One example of the former case is when the class names are long, since they won't have to be repeated and it might be easier to read the line without the repitition. One example of the latter is if one class is exchanged for another class, but both classes have an identical method which is the one used. Then code will only have to be changed in one place.

Still, recognizing these advantages we would like it to work this way:

  1. If var is used when the first token after the = sign is not new, give a warning and suggest replacing var with the returned type of the method called.
  2. Never suggest using var.


Examples of lines which gives warnings and a refacatoring suggestion to use the returned type of the called method instead:

var a = something.One();

var b = whatever.Boing(1 + 8, "foo");

var c = Werty.SomeMethod();

Examples of lines which do not result in a warning or refactoring suggestion:

Foo foo = something.One();

int b = whatever.Boing(1 + 8, "foo");

Plasebo c = Werty.SomeMethod();

(As standard Resharper seem to always suggest using var, which we do not agree with.)

Is there a way to do exactly what we want?

We will try to use StyleCop as it could be called from our CI-server Hudson. Is the plugin for resharper which uses StyleCop a recommended installation? And does it replace the var functionality above? Should we solve this using StyleCop instead?

3 comments
Comment actions Permalink

Just change the settings to make "Use var" a hint instead of a suggestion.
Problem solved.


"Kent Larsson" <no_reply@jetbrains.com> wrote in message
news:21269212.65671274357774841.JavaMail.clearspace@app8.labs.intellij.net...

Hi,

>

Resharper is pretty great! We will be buing a couple of licenses. We have
once concern though:

>

We understand using var may increase code readability and manageability
in some situations. One example of the former case is when the class names
are long, since they won't have to be repeated and it might be easier to
read the line without the repitition. One example of the latter is if one
class is exchanged for another class, but both classes have an identical
method which is the one used. Then code will only have to be changed in
one place.

>

Still, recognizing these advantages we would like it to work this way:

>

1. If var is used when the first token after the = sign is not new,
give a warning and suggest replacing var with the returned type of the
method called.
2. Never suggest using var.

>

Examples of lines which gives warnings and a refacatoring suggestion to
use the returned type of the called method instead:
var a = something.One();
var b = whatever.Boing(1 + 8, "foo");
var c = Werty.SomeMethod();

>

Examples of lines which do not result in a warning or refactoring
suggestion:
Foo foo = something.One();
int b = whatever.Boing(1 + 8, "foo");
Plasebo c = Werty.SomeMethod();

>

(As standard Resharper seem to always suggest using var, which we do not
agree with.)

>

Is there a way to do exactly what we want?

>

We will try to use StyleCop as it could be called from our CI-server
Hudson. Is the plugin for resharper which uses StyleCop a recommended
installation? And does it replace the var functionality above? Should we
solve this using StyleCop instead?

>

---
Original message URL:
http://www.jetbrains.net/devnet/message/5263607#5263607


0
Comment actions Permalink

But then I won't get the warning I want. Is it possible to get it exactly as I want it to be?


0
Comment actions Permalink

Have you investigated creating your own warnings/suggestions using the
structural search and replace feature?

Check out details at this link:

http://blogs.jetbrains.com/dotnet/2010/04/introducing-resharper-50-structural-search-and-replace/

So basically, turn the 'var' settings to "hints" (get them out of the way),
and then try and add your own warning for your own pattern.  It should be
possible, but if not, you can suggest improvements/enhancements to the
feature for consideration in a future version of ReSharper.


"Kent Larsson" <no_reply@jetbrains.com> wrote in message
news:27990535.71941274431474020.JavaMail.clearspace@app8.labs.intellij.net...

But then I won't get the warning I want. Is it possible to get it exactly
as I want it to be?

>
>

---
Original message URL:
http://www.jetbrains.net/devnet/message/5263691#5263691


0

Please sign in to leave a comment.