81

I'm using StyleCop. But there a couple of rules I want to ignore, for instance using this. in front of class members.

How do I turn off a StyleCop rule. I've looked but can't find how to do it.

bdukes
  • 152,002
  • 23
  • 148
  • 175
AJM
  • 32,054
  • 48
  • 155
  • 243

3 Answers3

82

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.

You can also put copies of the settings file into your projects to override the global settings.

If you're using Visual Studio integration and not just MSBuild integration, you should be able to get to the settings editor on a per-project basis by right clicking on your project and choosing StyleCop Settings or something like that.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
bdukes
  • 152,002
  • 23
  • 148
  • 175
  • Where's that settings file? Does that apply if you have resharper installed too? – War Sep 27 '12 at 08:52
  • For version 4.7, it's `C:\Program Files (x86)\StyleCop 4.7\Settings.StyleCop`. The ReSharper integration will also look there (as well as looking at any overriding settings files in your project) – bdukes Sep 27 '12 at 13:42
  • ah thanks :) ...annoying when i have over 100 projects and i need to turn off 1 rule for them all. – War Sep 28 '12 at 08:39
  • 4
    @Wardy If you have all of then in the same solution, you can have the `Settings.StyleCop` file at the same directory as your `.sln` file. – J.A.I.L. May 21 '13 at 11:19
  • @bdukes: I am using StyleCop 4.7 with VS 2012 express and would like to turn off a rule. Can you expand your answer to say what has to be put in the file in order to disable a rule. I'm looking at the XML and can't figure it out. – Harrison Nov 08 '13 at 17:48
  • 4
    @Harrison, have you tried using StyleCopSettingsEditor.exe on your Settings.StyleCop file, instead of editing it by hand? It's in `C:\Program Files (x86)\StyleCop 4.7` – bdukes Nov 11 '13 at 15:23
  • @Wardy: you don't have to copy the file to 100 projects, go with this option: "Sharing StyleCop Settings Across Projects" https://stylecop.codeplex.com/wikipage?title=Sharing%20StyleCop%20Settings%20Across%20Projects&referringTitle=Documentation – Piotr Owsiak Jun 13 '14 at 11:06
  • i dont seem to have a settingseditor.exe (or any exe with a remotely similar name)...what gives? – Jacob Alley Nov 18 '16 at 15:44
  • Extra tip. If you have a directory struture like \root\Pres\Pres.csproj, \root\Bal\Bal.csproj, \root\Dal\Dal.csproj and \root\Solutions\MySolution.sln, you can right click any of the projects.......create the rules......(\root\Bal\Settings.StyleCop for example) BUT you can manually move that file to \root\Settings.StyleCop..........and it will be "global" for \root\, aka, your entire solution. – granadaCoder Dec 12 '19 at 14:04
11

Stylecop is configurable at Project level. Manage rules easily using graphical interface instead of editing settings file.(Some time in some versions mentioned as Stylecop settings)

You should see a search/Find Option once you select the settings option as in the below image.

Ex: If you want to remove a rule where // comments are not allowed and //// are allowed by stylecop, then search for stylecop rule id. In this case it is SA1005. You can see this in the warnings section when you run stylecop in your project. Find with key word SA1005 and you should see that in result section. Just uncheck.

enter image description here

You can manage rules as below

enter image description here

Kurkula
  • 6,386
  • 27
  • 127
  • 202
  • I think you can setup ONE csproj this way, then "move" the Settings.StyleCop file to a higher up folder (..\.. for example) and it will apply to everything at/under that folder. – granadaCoder Dec 20 '18 at 22:47
  • (repeat comment from another answer, but helpful to prevent extra work) ::: Extra tip. If you have a directory struture like \root\Pres\Pres.csproj, \root\Bal\Bal.csproj, \root\Dal\Dal.csproj and \root\Solutions\MySolution.sln, you can right click any of the projects.......create the rules......(\root\Bal\Settings.StyleCop for example) BUT you can manually move that file to \root\Settings.StyleCop..........and it will be "global" for \root\, aka, your entire solution. – granadaCoder Dec 12 '19 at 14:05
6

That specific rule is under Readability Rules - Member Access - SA1101: PrefixLocalCallsWithThis.

If you turn that off it should stop screaming at you... I had to do it. Stylecop has been giving me a headache.

DeadlyChambers
  • 5,217
  • 4
  • 44
  • 61