Very confused by the VS2022 code analysis configuration options - c#

When I build my C# solution in VS2022 I see various code analysis build warnings, for example:
IDE0090 'new' expression can be simplified
I'd like to change the severity of some of these to "suggestion", which I assume will continue to display the editor squiggle/light bulb but not the build warning. When I click the light bulb next to the line of code, this set of menus appears:
Could someone explain the difference between the four 'Configure...' menu options please? I.E.
"Configure IDE0090 code style"
"Configure IDE0090 severity"
"Configure severity for all 'Style' analyzers"
"Configure severity for all analyzers
(The first option provides "true" & "false" choices, while the other three provide "None", "Silent", ...)
I don't know if this is relevant but I have the "StyleCop.Analyzers" package installed in each project in my solution.
As an aside, the "change preview pane" seen on the r.h. side of the image shows it wanting to add the new lines to my .editorconfig file, which lives in the solution's root folder (I've confirmed it is this file, based on the line numbers and last existing lines seen in the preview). However whenever I make a selection it never updates the file (and I continue to see IDE0090 appear as a build warning). Am I missing something? The file isn't read-only!
If I add the lines seen in the preview window to .editorconfig myself via Notepad then the warnings disappear.

Related

Suppress Blazor css ::deep warning

In a css file of an asp.net Blazor app when I use ::deep VS emits a warning "Validation (CSS 4.0): "::deep" is not a valid pseudo-element." That might be true for regular CSS, but not in the context of a Blazor app.
Is there a way to suppress it? Right-click -> Suppress -> In File / In Source do nothing.
You can add deep to the vendor specific extensions.
I've added it to the Microsoft extension and it works.
Open with an editor (in Administrator mode) the file:
C:\Program Files (x86)\Microsoft Visual Studio\2019\<your version>\Common7\IDE\Extensions\Microsoft\Web Tools\Languages\Schemas\CSS\1033\css-vendor-ms.xml
Go to line ~69 and add the following code:
<CssPseudo name="::deep"
_locID="ms-browse-pseudo-element"
_locAttrData="description"
version="3.0"
browsers="IE11"
description="Blazor child component support."
standard-reference="https://learn.microsoft.com/en-us/aspnet/core/blazor/components/css-isolation?view=aspnetcore-5.0"/>
Restart Visual Studio.
Now I can see the source code as:
For Resharper users, editing the css-vendor-ms.xml is needed but does not completely solve the issue. Resharper will continue to highlight ::deep elements as errors in your solution. This is currently a known bug, which will hopefully be resolved in a future build.
As a work around, go to Resharper Options --> Code Inspection --> Inspection Severity --> CSS --> Potential Code Quality Issues --> Unknown CSS symbol and drop the severity to Warning (or lower).
This has now been fixed as of the latest preview version of Visual Studio 2022:
https://developercommunity.visualstudio.com/t/Support-::deep-in-razorcss-CSS-isolati/1623976
I just downloaded 17.3 and my project is now down to zero warnings again! Look forward to it being released properly.

How to hide the ... that appeared everywhere in Visual Studio 16.10?

How to hide the ... that appeared everywhere in Visual Studio 16.10?
e.g.
It appears for unused usings, expression value not assigned, and many more.
[I'm NOT asking how to turn off graying out of unused usings.]
Dots like that, along with squiggly underlines, are coming from diagnostics issued by the compiler and analyzers.
Each diagnostic has its own identifier code such as CS1234 or IDE1234, etc.
You can configure the severity of each of these as described here:
https://learn.microsoft.com/en-us/visualstudio/code-quality/use-roslyn-analyzers?view=vs-2019
The easiest way is to add/edit your .editorconfig file and put the following in:
dotnet_diagnostic.<rule ID>.severity = <severity>
Where <rule ID> is the diagnostic ID, and <severity> is one of none, silent, suggestion, warning or error.
This gives you very fine grained control over these kinds of tips in your solution.
If you put the .editorconfig file in the root of your repo, it will apply to all projects in the solution.
Here's an example of using the lightbulb menu (Ctrl+.) to configure the severity of a diagnostic with code VSTHRD002:
There's also a section "Suppress ..." which lets you suppress a specific instance of the diagnostic, in contrast to changing the severity which applies across a broader scope.
Note you have have multiple .editorconfig files. For example, if all your unit tests are under a test folder, you could drop an .editorconfig file in there that has more relaxed rules.

How to show all Suggestions/Warnings/Errors in the Error List windows of Visual Studio 2019

The problem is that not all visual studio suggestions, warnings and/or errors are shown on solution build.
Let's say that on solution build, a total of 22 warnings are shown. After solution build, upon opening .cs files within projects, more warnings are being shown.
Steps:
Rebuild solution
Error List shows 0 Errors, 22 Warnings, 0 Suggestions
Open a .cs file from within a .csproj
Error List shows 0 Errors, 22+ Warnings, 0+ Suggestions
Why aren't these errors/warnings/suggestions shown on build, rather than being shown once a corresponding .cs file is opened?
Note - An .editorconfig file is used within the solution. This .editorconfig specifies a set of rules which define when to show Errors, Warnings, and Suggestions within .cs files.
I faced this issue and, for me, what worked was simply following the below steps:
Analyze -> Run code Analysis -> On Solution
This will give all warnings on solution level.
In Visual Studio 2022 I missed many warnings too - via Tools > Options, tab Text Editor > C# > Advanced you find these settings:
Run background code analysis for:
Show compiler errors and warnings for:
and for both you probably want the option Entire solution (instead of Open documents, Current document or None).
You can enable Source code analysis for a single project through the Project Properties dialog. This will in fact install some NuGet packages into the project, which together will run an analysis on each build of the project.
This is not an exact replica of Intellisense analysis, if only because what it will do depends on the selected Rule Set. For the most part I expect this to be at least as thorough as the Intellisense analysis, and probably even more so.
Here is how to install it, and how to select a Rule Set:
For more info, see
https://learn.microsoft.com/en-us/visualstudio/code-quality/roslyn-analyzers-overview
https://azuredevopslabs.com/labs/devopsserver/codeanalysis/

Disable "Name can be simplified" IDE0003 fix hint

Visual Studio 2017 shows a hint for unnecessary this qualifiers even when the inspection is disabled in the options.
This is how it looks:
(First line is the culprit, second line is how I want this to always look like.)
Hovering over the grayed out this and clicking on the light bulb shows this suggested fix:
I can't find a way of disabling this completely. Sometimes I want to have "unnecessary" this qualifiers and sometimes I don't, so I don't want VS to comment about this at all.
"None" is the least severe option yet it still shows this annoying, different color.
Is there any possibility of complete disabling this inspection?
You can use a ruleset file to disable any analysis if you know its diagnostic id (in this case 'IDE0003')
On the References node of you project right click on Analyzers and select Open Active Rule Set
Once the ruleset editor is open just search for IDE0003 and uncheck the checkbox. Then save the ruleset file. Then save the project.
The following XML will be added to you project file. You can add this ruleset file to any project where you want this rule disabled.
<CodeAnalysisRuleSet>ConsoleApp9.ruleset</CodeAnalysisRuleSet>
Looks like the current process is more complicated for .NET Core and .NET Standard projects.
From MS VS Docs:
If you have a .NET Core or .NET Standard project, the process is a little different because there's no Code Analysis property tab. Follow the steps to copy a predefined rule set to your project and set it as the active rule set. After you've copied over a rule set, you can edit it in the Visual Studio rule set editor by opening it from Solution Explorer. [emphasis mine]
Taking the first link in that quote will eventually take you, after a little sleuthing, to Code style rule options, that finally tells you how to add the file:
In Visual Studio, you can generate this file and save it to a project at Tools > Options > Text Editor > [C# or Basic] > Code Style > General. Then, click the Generate .editorconfig file from settings button.
NOTE: This produces a tiny warning just under your toolbars that an .editorconfig has been added to your solution. Select the "Yes" button to include it in your solution.
And now you can open and edit your new .editorconfig file.
Looks like this is the "offending" section:
# this. and Me. preferences
dotnet_style_qualification_for_event = false:suggestion
dotnet_style_qualification_for_field = false:silent
dotnet_style_qualification_for_method = true:suggestion
dotnet_style_qualification_for_property = false:suggestion
If that dotnet_style_qualification_for_property is changed to = true:suggestion (explanation here), I think you're golden [at least for properties, of course -- make edits as appropriate].

Visual Studio ignores my syntax errors in ASP.net project

I migrated a visual studio solution from using a "website" to a "ASP.net project" to be able to use Web.config transformation.
Now, after having migrated, Visual Studio completely ignores syntax erros when I clean/build/rebuild my solution.
Those syntax errors will be shown in my browser as soon as I open the web application - but seeing them during compile time would be helpful. They used to be shown in my error list - where I can now only see some uninteresting warnings.
I can still run my web application, and everything works well.
How can I configure my solution, so that compile errors will appear during compile time?
edit (in response to answers/comments):
As soon as I open the .cs file (by double clicking on it) the syntax errors are shown (inside the file and inside the error list view).
I'm using Microsoft Visual Studio Professional 2012 (Version 11.0.61030.00 Update 4) with .NET 4.5.50709 (german language version). I'm locally deploying to Visual Studio's IIS.
I'm not using NuGet, all my sources are in one single project
I'm actually only providing a REST-Backend using WCF. I only have c# sources. The syntax errors are in my c# classes.
The syntax errors are in my .cs files in my App_Code folder.
When I migrated my website to a project I manually edited my .csproj file (added missing "Content Include"s etc.). I hope that this did not break my solution...
I am not using the default "DEBUG" and "RELEASE" build configurations, but created my own server-specific configurations (named after the names of each server).
( #Guvante ) When I edit the build configurations, I see one line in the "project context table". The first and only line shows:
the name of my project
the configuration name in a dropdown
the plattform "Any CPU"
a checked checkmark "build"
the empty field "deploy"
My error list is filtered to "current project" and it won't show the syntax errors, no matter which item I select in my solution explorer.
Sometimes (can't tell when exactly) VS shows a warning, when starting debugging, telling my that my module was build with optimizations or without debugging information. Don't know, whether this warning is related to this issue.
Console output of successful build (though sources contain syntax error) is:
1>------ Erstellen gestartet: Projekt: MyProject, Konfiguration: localdev Any CPU ------
1> MyProject -> C:\path\MyProject\bin\MyProject.dll
========== Erstellen: 1 erfolgreich, 0 fehlerhaft, 0 aktuell, 0 übersprungen ==========
I saved, closed VS, rebooted machine, reopened VS, closed eyes, crossed fingers - #chief-two-pencils ;)
In the .csproj file you can change
<Content Include="C:\...\foo.cs" />
back to
<Compile Include="C:\...\foo.cs" />
More info on the MSDN documentation and this stackoverflow question.
You should change it to:
<Compile Include="....." />
The MSDN article on the build action property says:
Compile - The file is compiled into the build output. This setting is used for code files.
Content - The file is not compiled, but is included in the Content output group. For example, this setting is the default value for an .htm or other kind of Web file. Means that it is a deployable project item, it signals that the file needs to be copied to the target machine. Also note that Content will be included when using one-click deploy.
See more about build action here.
if you have Nuget packages run an update-package
try to unload the project and reload it into the solution
This worked for me

Categories

Resources