Treat Warnings as Errors has no effect

后端 未结 2 1975
栀梦
栀梦 2021-01-05 01:58

In my project\'s settings in Visual Studio, I have set \'Treat warnings as errors\' to \'All\'. The Warning level is set to 4. I tested this by deliberately introducing code

相关标签:
2条回答
  • 2021-01-05 02:45

    Code Analysis uses a different mechanism to treat warnings as errors. To have Code Analysis warnings treated as such, add a new Code Analysis Ruleset to your solution. To do so, rightclick your solution and choose "Add new item...". Search for "Rule Set" and select to add a new "Code Analysis Rule Set". Give it any name you want.

    Add new Rule Set

    In the Rule Set Editor, select the rules you want to include in your project and set them to Error. You can choose which rules to treat as errors and which as warnings.

    Select your rules and set to error

    Set the name for the rule set in the Code Analysis Ruleset properties window and save it. Then open the Analyze->Configure Code Analysis for Solution menu item.

    Set name and apply to solution

    Select your "As Error" ruleset for your projects and apply.

    Select as error ruleset and apply

    0 讨论(0)
  • 2021-01-05 02:47

    You could use the "CodeAnalysisTreatWarningsAsErrors" property in your csproj-file like described here:

    http://blogs.msdn.com/b/codeanalysis/archive/2007/08/08/_24002800_codeanalysistreatwarningaserrors_2900_-msbuild-property.aspx

    For Visual Studio 2008, we have added a new MSBuild property that allows you to easily treat all Code Analysis warnings as build errors. This can be useful for example, if you want to force that any firing of a Code Analysis rule to break the build during a nightly Team Build without needing to individually set this for every rule.

    To use, simply add the property to your project file (or a common targets file) and set it to true:

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
        <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
        <PropertyGroup>
            [...]
            <!-- either here -->
            <CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
        </PropertyGroup>
        <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
            [...]
            <!-- or here ... -->
            <CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
        </PropertyGroup>
        <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
            [...]
            <!-- and here -->
            <CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
        </PropertyGroup>
        [...]
    </Project>
    

    This can be used with in combination with <StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>, which will have the same effect on StyleCop warnings.

    0 讨论(0)
提交回复
热议问题