Nasty but correct syntax fools ReSharper

I just came across the following code which, whilst fairly nasty, is valid C#. ReSharper flags it up as a syntax error.

object o= "dog";
string message= o as string + "food"; // ReSharper chokes

The F2 error is:
Unexpected token

The Tooltip error is:
Cannot apply operator '+' to operand of type 'string'

2 comments
Comment actions Permalink

Hello James,

The problem is that it is surely a nasty, but not quite "correct" code. In
fact, if we refer to the specs it is not a valid C#, but the code is accepted
by Microsoft C# parser. MS parser accepts it in a rather confusing way, for
example:

string message= x as string + y as string;

is treated as

string message= ((x as string) + y) as string;

I would recommend avoiding this syntax altogether.

The bug is present in our tracker (http://www.intellij.net/tracker/resharper/viewSCR?publicId=4576).
A bug about the specs and implementation discrepancy is present in Microsoft
feedback system as well. They are going to fix the specs. We are, well, to
be honest, not very actively because it requires considerable changes in
implementation, going to simulate the Microsoft parser behavior.

I just came across the following code which, whilst fairly nasty, is
valid C#. ReSharper flags it up as a syntax error.

object o= "dog";
string message= o as string + "food"; // ReSharper chokes
The F2 error is:
Unexpected token
The Tooltip error is:
Cannot apply operator '+' to operand of type 'string'

Thanks,
Andrey Simanovsky


0
Comment actions Permalink

Oh, I would fire myself if I wrote code like that ;)

I came across it in some sample code from Infragistics...

0

Please sign in to leave a comment.