我想把Stylecop警告视为错误,但是对我来说并不奏效.
我的项目配置为将警告视为错误,如果我使用真正的“编译器警告”构建,它确实会显示编译器错误.但是使用“Stylecop警告”它只显示编译器警告.
因此,当有Stylecop警告时,我对TeamCity的登录很麻烦,不会打破CI建立.
我使用VS2013与Stylecop 4.7.49.
我的设置:
>项目 – >属性 – >建立
>项目 – > Stylecop设置 – >选项
>将违规行为视为错误:已检查
示例代码正确打破构建,包含实际的编译器警告:
using System; namespace CodeUsageTest { public class CodeUsage { private string fff() { int nobodyLovesMe; //CS0168 return ""; } } }
构建输出:
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 ==========
不打破构建的示例代码(尽管我想要它),包含stylecop警告:
using System; namespace CodeUsageTest { public class CodeUsage { private string fff() //SA1300 { return ""; //SA1122 } } }
构建输出:
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,0 skipped ==========
解决方法
修改您的csproj文件以添加以下配置:
<PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> ... <StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings> </PropertyGroup>
另见this answer,这解释了为什么有些警告不能被提升为错误.