Search code examples
visual-studio-2010code-analysiscompiler-warningsfxcop

Treat Warnings as Errors has no effect


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 that violates CA1305, but it builds (and rebuilds) successfully, returning a Warning. What I expected was that the build would fail and an Error would be returned. Is my understanding wrong?


Solution

  • 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