Split warning "Use 'var' keyword when initializer explicitly declares type"

Is it possible to split the warning "Use 'var' keyword when initializer explicitly declares type" based on the types of initializers? There are several possibilities that in my opinion are very different Proposed variants (if possible to differentiate):

Use 'var' keyword when invoking a constructor

DataTable table = new DataTable();

Use 'var' keyword when initializer casts result

DataTable table = (DataTable)someExpression;

Use 'var' keyword with return type is known

int[] ids = DB.GetIds(); // Note: Method returns int[]


My personal opinion is that "var" is not appropriate in the last one. While it's technically acceptable, it's not clear from looking the code what type the variable is unless you already know what the method returns. However, ReSharper doesn't differentiate between these three places where 'var' is possible so I can not turn off the third one without also turning off the first two.

Thanks,

Sam

3 comments
Comment actions Permalink

Hello Samuel,

These are exactly cases we identified, so you should be able to configure
as you want it to be.
"Use 'var' keyword when initializer explicitly declares type" covers new
expression, cast expressions (direct cast and safe cast), as well as call
to generic method when type is specified explicitly, like GetService(). The other analysis, "Use 'var' keyword when possible" suggests var whenever compiler will infer exactly same type, as specified. This ensures code semantics will not change. By default, the first one is set to be suggestion (green squiggly) and the second one to be hint (short straight green line). You can switch off the latter, if you think it is inappropriate. Check ReSharper Options / Code Inspection / Inspection Severity. At any place var keyword is possible, you will have context action available, so you can change it to implicit or explicit form as you wish. Sincerely, Ilya Ryzhenkov JetBrains, Inc http://www.jetbrains.com "Develop with pleasure!" SN> Is it possible to split the warning "Use 'var' keyword when SN> initializer explicitly declares type" based on the types of SN> initializers? There are several possibilities that in my opinion SN> are very different Proposed variants (if possible to SN> differentiate): SN> SN> Use 'var' keyword when invoking a constructor SN> SN> DataTable table = new DataTable(); SN> SN> Use 'var' keyword when initializer casts result SN> SN> DataTable table = (DataTable)someExpression; SN> SN> Use 'var' keyword with return type is known SN> SN> int[] ids = DB.GetIds(); // Note: Method returns int[] SN> SN> My personal opinion is that "var" is not appropriate in the last SN> one. While it's technically acceptable, it's not clear from looking SN> the code what type the variable is unless you already know what the SN> method returns. However, ReSharper doesn't differentiate between SN> these three places where 'var' is possible so I can not turn off the SN> third one without also turning off the first two. SN> SN> Thanks, SN> SN> Sam SN>]]>


0
Comment actions Permalink

Actually, your needs are fully satisfied by CodeCleanup.
As for warning, there are also 2 of them - the first one when the type is
evident from the initializer, and the second when type is not evident

--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Samuel Neff" <no_replay@jetbrains.com> wrote in message
news:29663642.1203455967460.JavaMail.itn@is.intellij.net...

Is it possible to split the warning "Use 'var' keyword when initializer
explicitly declares type" based on the types of initializers? There are
several possibilities that in my opinion are very different Proposed
variants (if possible to differentiate):

>

Use 'var' keyword when invoking a constructor

>

DataTable table = new DataTable();

>

Use 'var' keyword when initializer casts result

>

DataTable table = (DataTable)someExpression;

>

Use 'var' keyword with return type is known

>

int[] ids = DB.GetIds(); // Note: Method returns int[]

>
>

My personal opinion is that "var" is not appropriate in the last one.
While it's technically acceptable, it's not clear from looking the code
what type the variable is unless you already know what the method returns.
However, ReSharper doesn't differentiate between these three places where
'var' is possible so I can not turn off the third one without also turning
off the first two.

>

Thanks,

>

Sam



0
Comment actions Permalink

Thanks for the responses. I understand that ReSharper already has two different categories for 'var' replacement. I guess I was hoping for even more using the categories you already mentioned (new object construction, casting, generic method invocation with an evident type, method call with non-evident type).

Best regards,

Sam

0

Please sign in to leave a comment.