Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warnings as Errors - does not apply to Stylecop warnings

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.

My settings:

  • Project -> Properties -> Build

    • Warning level: 4
    • Suppress warnings: 1591
    • Treat warnings as errors: All
  • Project -> Stylecop Settings -> Options

    • Treat violations as errors: Checked

Example code that breaks the build correctly, containing real compiler warning:

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 ==========

Example code that doesn't break the build (although I want it to), containing stylecop warning:

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 ==========
like image 776
demoncodemonkey Avatar asked Jul 17 '14 12:07

demoncodemonkey


People also ask

Is StyleCop still used?

StyleCop used to be a Visual Studio plugin and a NuGet package. You can still use this in Visual Studio 2019, but the current recommended way to use StyleCop is to use the Roslyn-based analyzers.

How do I turn off StyleCop rules?

In your StyleCop install, there's a Settings. StyleCop file. You can edit this to turn off rules globally. Drag that file onto the Settings Editor executable in that file to edit it.

How do I get rid of StyleCop?

right-click on a project in Project Explorer. select "StyleCop Settings" on the "Rules" tab of the dialog that opens, uncheck the "C#" root of the Enabled rules tree.

What is StyleCop used for?

StyleCop is a C# source code analyzer that allows you to enforce a set of style and consistency rules. You can adapt the rules that you don't want to check depending on your needs. This kind of tools helps you to have a code: Readable.


2 Answers

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.

like image 56
ken2k Avatar answered Oct 01 '22 20:10

ken2k


You can easily configure StyleCop with MSBuild to make warnings appear as errors with the help of StyleCop.MSBuild NuGet package. You have to modify your project file as below.

<PropertyGroup>
  <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
  <StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
</PropertyGroup>

And also to ignore auto generated files you can modify Settings.StyleCop file as below.

<CollectionProperty Name="GeneratedFileFilters">
  <Value>\.g\.cs$</Value>
  <Value>\.generated\.cs$</Value>
  <Value>\.g\.i\.cs$</Value>
  <Value>TemporaryGeneratedFile_.*\.cs$</Value>
</CollectionProperty>

See the complete post here. Configure StyleCop with MSBuild to treat Warnings as Errors

like image 32
Saranga Avatar answered Oct 01 '22 20:10

Saranga