Visual Studio Missing Warnings - c#

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

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.

Can MonoDevelop highlight errors and warnings on-the-fly?

I'm currently testing MonoDevelop for my hobbist projects needs. It seems like i have to recompile my C# project every time to see if i made some errors in my code. Even simple errors like missing using statements aren't shown in the IDE while i'm typing unknown class names. Did i miss some kind of setting/add-in or this feature is really missing from the IDE?
(I know i can use Alt+Enter on the problematic class to quick add the required using statement, but i like to see not just these kinds of errors while i'm typing.)
MonoDevelop does not support background compilation, though it will underline syntax errors.

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