I want to treat Stylecop warnings as errors, but it's not working for me.
My projects are configured to treat warnings as errors, and if I build with a real "compiler warning" it does indeed display a compiler error. But with a "Stylecop warning" it only displays a compiler warning.
As a result of this, my checkin to TeamCity annoyingly does not break the CI build when there are Stylecop warnings.
I am using VS2013 with Stylecop 4.7.49.
Project -> Properties -> Build
Project -> Stylecop Settings -> Options
using System;
namespace CodeUsageTest
{
public class CodeUsage
{
private string fff()
{
int nobodyLovesMe; //CS0168
return "";
}
}
}
Build output:
1>------ Build started: Project: CodeUsageTest, Configuration: Debug Any CPU ------
1>D:\Sandbox\CodeUsageTest\CodeUsage.cs(9,17,9,30): error CS0168: Warning as Error: The variable 'nobodyLovesMe' is declared but never used
========== Build: 0 succeeded, 1 failed, 3 up-to-date, 0 skipped ==========
using System;
namespace CodeUsageTest
{
public class CodeUsage
{
private string fff() //SA1300
{
return ""; //SA1122
}
}
}
Build output:
1>------ Build started: Project: CodeUsageTest, Configuration: Debug Any CPU ------
1>D:\Sandbox\CodeUsageTest\CodeUsage.cs(7,1): warning : SA1300 : CSharp.Naming : method names begin with an upper-case letter: fff.
1>D:\Sandbox\CodeUsageTest\CodeUsage.cs(9,1): warning : SA1122 : CSharp.Readability : Use string.Empty rather than "".
========== Build: 1 succeeded, 0 failed, 3 up-to-date, 0 skipped ==========
Modify your csproj file to add the following configuration:
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
...
<StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
</PropertyGroup>
Also see this answer that explains why some warnings cannot be promoted to errors.