Why is only one of my compiler warnings suppressed? - c#

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.

Related

Why are warnings CS0108 & CS0114 *NOT* errors by default in Visual Studio?

Warning CS0108 trips when a variable is declared with the same name as a variable in a base class.
Warning CS0114 is the equivalent for methods.
Both flag up as a warning but not an error. Compilation continues and the executable can be run.
But the risk is a logic error or bug where the wrong one is invoked.
This is extremely dangerous in live production environments.
It's also very "non-obvious" when debugging: the code looks like it's doing the right thing when stepped through but is not. The bug can take forever to identify in heavily abstracted/inherited code-bases.
Surely the "new" keyword exists for a reason and (even more surely) usage should be enforced by a compile error - not just a warning.
But this is not so. You have to set it up by hand on each project in a solution; it's not even possible to enforce solution-wide.
And I haven't yet found a way to make this the default behaviour for new solutions.
Why?
Visual Studio by default considers as an error everything that makes a program unable to compile. Since these considerations do not affect the compilation but only the quality of the code, they are considered warnings. But as Styxxy has pointed out, you can treat any warning you like as an error.
You can actually set the warnings to be errors solution wide (kind of). Only restriction is that the solution has to be in the top level directory.
You can add a Directory.Build.targets file next to your solution to set up the warnings to be treated as errors. This file will automagically be loaded and used in the build process (see more in the docs: https://learn.microsoft.com/en-us/visualstudio/msbuild/customize-your-build?view=vs-2019#directorybuildprops-and-directorybuildtargets)
<Project>
<PropertyGroup>
<WarningsAsErrors>$(WarningsAsErrors);CS0108;CS0114</WarningsAsErrors>
</PropertyGroup>
</Project>
As for why it is by default a warning and not an error (my thoughts): errors are things that break the build and you can't compile the project/solution, while warnings are to warn the user about a potential mistake.

How to disable RECS* warnings in with Roslyn compiler

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.

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.

How to suppress warnings from particular dll

I am adding 3rd party Lotus Notes dll in my project which is not CLS-Compliant. I need to set 'Treat Warning As Error'. How can I suppress all the warnings from that particular dll only.
All warnings are like
Identifier 'Domino.Name_Of_Identifier' is not CLS-compliant
You can't suppress warnings from a particular DLL - but you can turn off specific warnings.
In the message you'll see a number like CS3008 - go into Project Properties, Build Tab, and enter this into the Suppress Warnings field.
You can disable warnings locally with #pragma warning
To disable Common Language Specification warnings, use the following warning numbers:
40025, 40026, 40027, 40028
Alternatively you can mark your class or your assemlby as being non-CLS compliant using the CLSCompliantAttribute, but in this case I guess it would be like shooting a fly with a gun.
After struggle I found perfect solution. Open Output window and build the solution. You will see the warning. Add this warning to the window shown by Bevan. It works.

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