Suggestion: Add a context action to implicit char == int comparison


I just ran into trouble because while it is impossible in C# to assign int to char, it is perfectly legal to compare char with int:

char c = 1;    // invalid

char c = '1';  // OK

if (c == '1')  // OK (obviously)

if (c == 1)    // also OK!! (no warning nor anything)

Tried on VS2008 and VS2010, target framework 3.5 and 4.

In case of Linq To SQL:

table.Where(row => row.charColumn == 1)
it gives a comparison of char value with the character of ASCII code 1. It is of course legal, but not what one may want, which is probably this:

table.Where(row => row.charColumn == '1')

The compiler doesn't have problem with this, but users probably do. In my case for some half an hour I wondered why I have an empty result while there's row in the database. Finally SQL Profiler demistyfied this. Resharper (5.0) didn't notice this problem.

I think this is good place where Resharper can help save time and avoid confusion. I suggest adding a context action 'Possible unintended type conversion' or something, that would warn user of such problem.

Funny thing is that F# doesn't allow this, for example try the following:

> let c = '1';;

> let eq = c = 1;;  // let 'eq' be the result of comparison

stdin(7,14): error FS0001: This expression was expected to have type
but here has type

Thanks much for great product!!!

Please sign in to leave a comment.