Warning messages like error messages in VSCode with c# - c#

Warning messages such as "Variable 'e' is declared but never used" may not allow the solution to compile, in visual studio code with c#. Force developers to create cleaner code. It's possible?

Related

Visual Studio Skip certain errors while compiling

Is there a way to have Visual Studio 2010/2012 skip over certain compiler errors and just keep compiling the rest of the code.
I keep getting this error for DNN and I don't need it.
'ControlName' is not a valid value for attribute 'classname'
No, a compiler error means that there is something fundamentally wrong with your code, such that the compiler is unable to determine what executable code to generate. Syntax and semantics errors must be fixed and cannot be skipped to achieve successful compilation.
Warnings, however, can be skipped or suppressed because they indicate a potential mistake or issue, rather than a blocking one.

Print a message in the build output (that is not a warning)

At build time, I'd like to be able to print a message to the build output window without having it be classified as a warning. I.e. In the Visual Studio Error List, I would want it to display only if you had the "Messages" filter turned on.
So, I want identical functionality to
#warning Warning Message
But I don't want it to be classified as a warning.
Context:
I have a lot of projects that currently generate a lot of warnings. What I'd like to do first is prevent new warnings from being introduced. So, I've:
Turned on "Warnings as Errors"
Gone through and used #pragma warning disable/restore to eliminate existing warnings.
However, for the warnings I disabled, I'd like to print out a message indicating that there is a warning here that needs to be investigated (since I haven't yet investigated what needs to be done for these warnings). For example, many of the warnings are "obsolete" type warnings, where we do need to go and do some work at some point. So, I don't want these warnings to disappear.
Ideally, I'd do something like this:
#pragma warning disable 0618
#message Existing Warning: 0618: IObsoleteInterface is obsolete.
class MyClass : IObsoleteInterface
#pragma warning restore 0618
I'm open to other approaches of dealing with the issue.
According to MSDN you cannot extend #pragma: http://msdn.microsoft.com/en-us/library/x74w198a.aspx
But if ErrorList isn't strict requirement, you can work around this: you may use comments to highlight things important to you.
If you add special token at the begining of your comment, you'll be able to track it in TaskList window.
So, your code will look like:
#pragma warning disable 0618
//TODO: Existing Warning: 0618: IObsoleteInterface is obsolete.
class MyClass : IObsoleteInterface
#pragma warning restore 0618
And after that, if you'll open View -> Task List -> select Comments in dropdown, you'll see your comment there.
There are 3 predefined tokens: TODO, HACK and UNDONE - and you can add your own like MESSAGE and change it's priority to make your code look similar to what you initially expect:
#pragma warning disable 0618
//MESSAGE: Existing Warning: 0618: IObsoleteInterface is obsolete.
class MyClass : IObsoleteInterface
#pragma warning restore 0618
More information about tokens you will find here: http://msdn.microsoft.com/en-us/library/zce12xx2(v=vs.100).aspx
Worth to mention that if you use Resharper, it has another tool for TODO's. I'll find it here: ReSharper -> Tools -> To-do Items
You should have a look at the csc task's WarningsNotAsErrors parameter. When the list of the "acceptable" warnings are specified to this parameter with TreatWarningsAsErrors=true you should get your existing warnings logged while new ones will show up as errors.
Ofcourse this is not exactly existing versus new warnings but a way to work with exact warning numbers that you are comfortable with leaving in.
But my first suggestion would be to just fix these. Once hidden outside of build they rarely get addressed.
If you are using "Premium" or "Ultimate" edition of Visual Studio, it has a feature of code analysis using Rule Set.
For any project when you right click & go to properties, select Code Analysis Tab, it has various out of box settings like treating warning as error, disabling some of the warning.
You can configure warning to be ignored or treated as error. You can use out of box Microsoft rule sets or alternatively you can build a custom ruleset as per your requirements (which is not very difficult & worked for my requirement).
To enable code analysis for managed code:
Select a project in Solution Explorer.
On the Project menu, click Properties.
Click Code Analysis.
Select Enable Code Analysis on Build (defines CODE_ANALYSIS constant).
For details on configuring Code Analysis, please visit:
http://seesharper.wordpress.com/2010/04/02/code-analysis-in-team-build-2010/
For building Custom Rulesets:
http://msdn.microsoft.com/en-us/library/dd264974.aspx
http://blogs.msdn.com/b/codeanalysis/archive/2010/03/26/how-to-write-custom-static-code-analysis-rules-and-integrate-them-into-visual-studio-2010.aspx
For Learning about Ruleset & code analysis:
http://msdn.microsoft.com/en-us/library/dd264996.aspx

How can I supress Intellisense warnings such as "variable is never assigned to.."?

I'm playing around with MEF. Problem is, Visual Studio ('12, Pro) thinks that my imported stuff is never assigned to and helpfully highlights a warning:
I know it's only cosmetic, but (since these warnings are usually worth heeding) I find it distracting.
In my research I've found two methods which allege to suppress the warning, but they seem to be referring to warnings at build time, rather than from Intellisense:
The SupressMethod attribute
The #pragma warning disable directive
Is there a way to suppress the IntelliSense warnings?
Just make it a public property, and you won't get the warning in the first place.

Contract.Assert do not throw compilation error

I am trying to add validation for my type at compilation time using Contract but unfortunately it do not work. For example code below do not give compiler error while compiling project. Should I enable something?
Contract.Assert(false, "Invalid state!");
Yes, you need to enable the static checking. Go into the project properties and visit the "Code Contracts" tab. Of course you'll need to have installed the static checker extension first, which IIRC is only available for Visual Studio Ultimate. See the user documentation for details of the exact options available.
Note that this happens after the initial compilation phase, so sometimes you'll see a delay between the first part of the build and the results of the static checker. (It's not really a "compiler" error - it's a "contract checking" error.)

Suppress compiler warnings using aspnet_compiler at the command line

Is there a switch to suppress specific compiler warnings when using the aspnet_compiler.exe warnings at the command line?
I've tried fixing the errors myself, rather than ignoring them - they are for events that are defined by never used. I simply commented out the events, but this caused other problems! It's as if they are used but the compiler doesn't recognise that (which seems unlikely).
I'm calling the compiler from the command line, and I'm using Visual Web Developer Express, which does not have the detailed build options in the Project Properties Build dialog that can be found in the full version of Visual Studio.
In the end I used
#pragma warning disable [warning no]
some code that triggers a warning
#pragma warning enable [warning no]
in my code, as per http://msdn.microsoft.com/en-us/library/441722ys.aspx. This works fine and although needs entering once for each compiler warning, it seems to be the best fit.

Categories