How to disable RECS* warnings in with Roslyn compiler - c#

After updating to C# 6 and VS2015 in some projects I have started to see a lot of build warnings like these:
RECS0119 'string.Compare' is culture-aware
RECS0017 Possible compare of value type with 'null'
RECS0063 'StartsWith' is culture-aware and missing a StringComparison argument
RECS0060 'IndexOf' is culture-aware and missing a StringComparison argument
RECS0027 Operator 'is' can be used
RECS0133 Parameter name differs in base declaration
Here is the screenshot of one of the suggestions:
These are valid suggestions for code improvements, but these are all coming from a class that is installed by a nuget package (PetaPoco, I'm looking at you!) and I don't want to modify that file in any way. And I don't want to see these warnings.
I have tried using #pragma warning disable RECS0060 and variations, but could not make the warnings disappear. Is there a way to disable these Roslyn code improvements warnings on per-file basis? Don't want to hide them for all classes as these come useful sometimes.

In VS 2017 you can right click the error number in the errors window and find an entry that should read something like "suppress" (sorry, working in german). There you can choose to suppress the message with a pragma in code or project wide via a file named "GlobalSuppressions.cs". Both works fine.

Related

Why is only one of my compiler warnings suppressed?

This is what my Suppress Warnings setting looks like in Project Properties -> Build:
Yet the only warning the compiler seems to be ignoring is the first, CS0168. I still have plenty of CS0414 warnings:
Why is the compiler not ignoring CS0414?
Don't include the prefix of the warning codes; just include the numeric part.
nowarn compiler option documentation
That said, admittedly it works both with or without the prefix for me in VS Enterprise 2017.
Another thought is to ensure you have the codes suppressed in the project that is emitting those warnings.

How to force a compile error when virtual method hiding of base class occurs?

I have a virtual method in a base class which has a full implementation. A developer deriving from this class might mistakenly create a method with the same name without overriding the base class (not knowing there's a proper base class method to be used). The compiler gives a warning that the new method is hiding the base method.
The problem is compiler warnings are easily ignored or not noticed. Is there a way to give a compiler error instead.. or some kind at your face warning? I want developers to consciously override the base method if they want to use their own implementation and not just create one with same name. I can't force other developers' Visual Studio to set warnings as errors. Plus there are already existing warnings which no one is going to fix.
Side question: If current VS2013 and C#5 has nothing to address this, does VS 2015 or C#6 add anything which helps in this issue?
You can change your project properties to treat a specific warning as error.
Go to Project Properties -> Build
There Check Specific warnings: under "Treat warnings as erros' and specify:0114 as warning number.
Now this particular warning will appear as error. This could be helpful but it mainly depends on the developers, if they choose to ignore the warning in first place then they can accidentally remove this warning number from build properties as well.
For your question:
does VS 2015 or C#6 add anything which helps in this issue?
You will get the same behaviour/warning in Visual Studio 2015, for C# 6.0. It is same as previous version.

Why is ReSharper showing unused variables as an error in only one of my projects?

I'm working on a C# solution that I inherited from another developer, and it was last worked on in 2011. He created it in VS2010 Professional, and swears he's never used ReSharper. I'm using VS2012 Pro and ReSharpher 7.1.3.
The solution has 7 projects total (3 winforms, 3 class libraries, and 1 console application), and in the console application, ReSharper is flagging any unused variable as an error, and I'm not sure why. It should be noted that the project compiles just fine, and VS' error output shows nothing in regards to the unused variables.
Initially, I thought that it was linked to the variable type that was not being used, but this proved false. I've tried several different variable types, all with the same effect.
So now I'm thinking that it has to be a project-specific setting, but the only three setting layers I have show that "Unused local variable" should appear as a warning.
I'm at a loss as to what is causing this to show up as an error. The obvious solution is to remove or use the unused variable, but I would also like to be certain that there is nothing else going on behind the scenes that I'm not aware of.
EDIT:
A few examples of the unused variables:
List<string> keys = Dependencies.Keys.ToList();
string testString = "test";
int testInt = 0;
Edit 2:
The above three variables are only declared and instantiated locally within a method, but never used anywhere in the rest of the code. I receive the following if I suppress the warning:
#pragma warning disable 168
List<string> keys = Dependencies.Keys.ToList();
#pragma warning restore 168
Restarting ReSharper did nothing to correct this.
I am guessing your project has warnings set to errors.
Go to the following menu options
Project >Properties > Build
On the right hand side below "Errors and warnings" you will see
"Treat warnings as errors"
This is a project based setting and would not have anything to do with if the previous user had resharper or not. It is something that resharper reads off of though as stated here : ReSharper web help
Note
In Visual Studio project settings, you can choose to treat warnings as
errors. ReSharper is aware of this option and highlights warnings
accordingly: if this setting is on, then issues that correspond to
compiler warnings will be highlighted as errors.
This setting is configurable in the project properties: Project |
[Project Name] Properties | Treat warnings as errors and can be
applied to all warnings if All is selected or to the specified
warnings only.

Visual Studio Missing Warnings

Anyone find where when you open a certain solution (that contains multiple projects) and compile that you're not seen some warnings that your colleagues see when compiling the exact same solution at the exact same state? The code is the same.
I depend highly on the warnings as a shortcut to find unused methods, etc. But I get nothing during compile.. only a couple based on references to user controls, etc.
Just guessing here... When you first build a solution, it has to compile everything. In that case all warnings come up. If you run build for a second time, it will only compile what has changed, using the previously compiled (cached) assemblies whenever it can. In that case the code that doesn't get compiled doesn't show warnings. If you want to see all warnings again, do a Rebuild from the Build menu which will force all the code to recompile and thus show the warnings.
Maybe your compiler warning level is not as strict as your collegues: http://msdn.microsoft.com/en-us/library/13b90fz7%28VS.71%29.aspx

Warning as Error, but not all

I would like to enable Warning as Error on our current project/solution for obvious reasons.
There are several warnings that should NOT be handled as an error, eg Obsolete, and using #warning directives.
Is this possible?
I see that I can make specific warnings behave as errors, but I would really like the 'invert' of that.
The closest I can get is disabling the 2 above mentioned warnings, but then there will be no 'warning' for them either.
Any suggestions?
To clarify:
I want the warnings, just not as an error. So all warning except for the above mentioned exceptions will behave as an error, and the above mentioned will be warnings (ones I can see in the compiler results).
The warnaserror compiler option supports erroring only on specific warnings. You can thus specify all warnings to be shown as an error, then disable the errors for certain warnings. Using the page's example as a guide:
/warnaserror
/warnaserror-:642,649,652
It is possible in VS2005 assuming you are using C#.
From http://blogs.msdn.com/kaevans/archive/2005/11/06/489681.aspx
In Visual Studio 2005, you have a
couple more options to control this.
Now, you have 3 options for treating
warnings as errors: All, None, or
Specific Warnings, where you can
provide a semi-colon separated list of
error numbers.
It is also possible to do it with GCC with the option -Werror=

Categories

Resources