While working on porting StructureMap to Mono I stumbled across some bugs in the Mono C# compiler. I did expected this, fixed or worked around the bugs and reported them. What I didn't expected, was to find some trivial bugs in Microsoft's C# compiler as well. It's nothing critical, but annoying. Consider this code:
using System;
namespace Test
{
public class BuggyClass
{
private string foo = String.Empty;
private int _bar = 5;
private int _wrongBar = 5;
public int Bar
{
get { return _bar; }
set { _wrongBar = value; }
}
public void DoSomething()
{
string msg = "foo" + 1.ToString();
}
}
}
It contains three variables and fields which are never used, so I would expect the compiler to emit three warnings. But csc does nothing - in none of three different versions:
c:\tmp\--->C:\WINDOWS\Microsoft.NET\Framework\v3.5\csc.exe /warn:4 /target:library test.cs
Microsoft (R) Visual C# 2008 Compiler Version 3.5.30729.1
, fr Microsoft (R) .NET Framework Version 3.5
Copyright (C) Microsoft Corporation. Alle Rechte vorbehalten.
c:\tmp\--->C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\csc.exe /warn:4 /target:library test.cs
Microsoft (R) Visual C# 2005, Compilerversion 8.00.50727.3053
fr Microsoft (R) Windows (R) 2005 Framework, Version 2.0.50727
Copyright (C) Microsoft Corporation 2001-2005. Alle Rechte vorbehalten.
c:\tmp\--->C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\csc.exe /warn:4 /target:library test.cs
Microsoft (R) Visual C# .NET-Compilerversion 7.10.6001.4
fr Microsoft (R) .NET Framework, Version 1.1.4322
Copyright (C) Microsoft Corporation 2001-2002. Alle Rechte vorbehalten.
This point goes to the Mono C# compiler, for correctly identifying these warnings:
c:\tmp\--->C:\Programme\Mono-1.9.1\lib\mono\1.0\mcs.exe /warn:4 /target:library test.cs
test.cs(19,20): warning CS0219: The variable `msg' is assigned but its value is never used
test.cs(7,24): warning CS0169: The private field `Test.BuggyClass.foo' is assigned but its value is never used
test.cs(9,21): warning CS0169: The private field `Test.BuggyClass._wrongBar' is assigned but its value is never used
Compilation succeeded - 3 warning(s)
You might say, that this is just a trivial warning and usually it doesn't hurt to have some unused variables around. But look at the Bar property above. It sets a different value than the one it returns. Looks like a serious bug to me - maybe caused by wrongly renaming the field. If this isn't covered by a unit test and the compiler doesn't even give a warning, chances are good, that this bug will find it's way into production code.
I've also reported this to Microsoft's Bug Nirvana BugTracker (FeedbackID 362970).