31

Is there a way to disable all Resharper warnings for a file or section of code with a single comment? I'm trying to create some coding exercises for interviewing potential candidates, and the Resharper warnings give away the problem I want the candidate to spot :P Suppressing the specific warning still makes it obvious what the problem is.

I still want to have Resharper available during the interview, I just want the candidate to spot this problem without Resharper spoiling the fun.

edit: Sorry I'll try to be more clear about what I'm after. I don't want to permanently disable a particular Resharper warning, I just want it to not show in one particular file, because the point of the exercise is to see if the developer understands the reason for the warning.

To give one example, there is a Resharper warning to use the .Any extension method instead of Count() > 0, which I want the developer to point out themselves. To disable that warning, you have to use a comment of:

// ReSharper disable UseMethodAny.0

around the code. That kind of gives the game away a little.

I was trying to find something like:

// ReSharper disable all

which I could place at the top of the class, so it won't give away what I want the developer to find. But I can't seem to find a way to do that. Using numbers for the Resharper warnings would be fine as well, but I don't think it works that way?

Niall Connaughton
  • 15,518
  • 10
  • 52
  • 48
  • 7
    Really, even if you put all at the top as numbers? If you put something like `#pragma warning disable 1573` on top of the file and person immediately recognizes what 1573 warning means - just hire on the spot :) – Alexei Levenkov Jan 13 '12 at 17:33
  • Thanks for the suggestion Alexei, but I'm talking about Resharper's warnings, not warnings from the C# compiler – Niall Connaughton Jan 14 '12 at 11:09
  • 1
    In ReSharper pick "disable with pragma" from the light bulb thingie next to warning/error (or something like this - don't have it handy) and it'll use that syntax. Move all to the top, add couple extra and noone will be able to use them as hints. – Alexei Levenkov Jan 14 '12 at 20:17
  • 2
    @AlexeiLevenkov - there's an option to disable with a comment in the light bulb dropdown, but it uses the notation I put in the edits for the post, eg: "//ReSharper disable UseMethodAny.0" which is what I'm trying to avoid because it tells the candidate exactly what I want them to say :P – Niall Connaughton Jan 16 '12 at 15:17
  • My bad - did not realize that this particular suppression method works with only limited subset of warnings (I think the ones that have corresponding CS warning - http://msdn.microsoft.com/en-us/library/01248w2b(v=VS.90).aspx I used). I would just suppress all real warning by hand than and intermix with 10+ extra suppression comments to hide the real ones. – Alexei Levenkov Jan 16 '12 at 18:06
  • No problems, thanks for your answers. This may end up being the only way to go! – Niall Connaughton Jan 18 '12 at 10:24

6 Answers6

44

You can press Ctrl + Shift + Alt + 8 to disable analyses and highlightings in the current file.

Pang
  • 9,564
  • 146
  • 81
  • 122
derigel
  • 3,218
  • 2
  • 19
  • 31
  • 1
    This should be the accepted answer as this will suppress all R# analysis in the current file. You can tell if R# analysis is disabled by looking at the top of the file's scroll bar (when open); the R# square will be gray instead of green/yellow/red. – Metro Smurf Apr 23 '12 at 00:08
39

According to this blog post on the JetBrains blog, in ReSharper 8 there will be a single comment that can disable ReSharper warnings in a file.

It will be

// ReSharper disable All

Note: The "All" is case-sensitive, the rest is not.

Pang
  • 9,564
  • 146
  • 81
  • 122
quip
  • 3,666
  • 3
  • 32
  • 45
  • 2
    And, if you want to disable just one file or section, put the "// ReSharper Disable All" at the beginning and the "// ReSharper Restore All" at the end of the file/section. (works on ReSharper 8+) – vpalmu Aug 25 '14 at 23:20
  • 1
    This worked for me to suppress built-in R# warnings, but did not work for my "Custom Patterns" – Andrej Adamenko Oct 27 '14 at 02:31
  • You can place the same directive in an HTML comment on an HTML page or Razor template to disable warnings there. – dthrasher Mar 06 '15 at 23:33
7

The following worked for me.

  • Add "No Resharper" to Generated Code Regions in R# -> Options -> Code Inspection -> Generated Code
  • Use the following to suppress the warnings:

    #region No Resharper
    
    // All R# warnings are suppressed here
    
    #endregion
    
Andrej Adamenko
  • 1,650
  • 15
  • 31
4

You could also use the SuppressMessageAttribute with ReSharper as the category and All as the checkId on the method or the class as shown below. This is more granular than disabling everything in the file if you need the fine grained control.

Tested with Visual Studio 2015 Update 3 and ReSharper Ultimate 10.0.2

[SuppressMessage("ReSharper", "All")]
private void MethodWithMultipleIssues()
{
    TestClass instance = null;
    // You would get an "Expression is always true" message
    if (instance == null)
    {
        Debug.WriteLine("Handle null");
    }
    else
    {
        // You would get an "Code is unreachable" message
        Debug.WriteLine("Handle instance");
    }
}
Martin Hollingsworth
  • 7,249
  • 7
  • 49
  • 51
2

You can add the file to the list of "Generated Code" in ReSharpers Options Menu:

Options > CodeInspection > Generated Code
huysentruitw
  • 27,376
  • 9
  • 90
  • 133
2

you have to configure the ReSharper Inspections

http://www.jetbrains.com/resharper/webhelp/Code_Analysis__Configuring_Warnings.html

Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
  • 1
    Right. The best way in this case is to press *Alt+Enter* standing on a highlight that you don't want shown, select *Inspection options*, and choose *Do not show*. This would make ReSharper ignore this and all similar code issues – Jura Gorohovsky Jan 13 '12 at 17:34
  • this is one option showed in the article I have posted: ....Press Alt+Enter to open a drop-down list with a quick-fix for which you'd like to change inspection severity.... – Massimiliano Peluso Jan 13 '12 at 17:36
  • Thanks for the suggestion guys, but I was hoping to find a way of just disabling all warnings in one particular file - I want R# to continue to give me these warnings normally, I just want to suppress them for this one file, without making it obvious which ones I'm suppressing. – Niall Connaughton Jan 14 '12 at 11:11