Disable warnings for automatic generated code/folder/namespace - c#

I like to have clean, "0 warnings" - projects in C#. This includes my project having XML comments for every public property and class.
Now I use entity framework with migrations (code first). The migrations are created by using "Add-Migration" which cause automatic code to be generated in the Migrations folder (standard behavior). I might want/need to modify these classes a bit but do not want to add Comments for the public classes created there.
I know I can disable the warnings using #pragma disable but again do not want to have to do this for every Migration-class.
So: Is there a possibility to use #pragma disable (or something similar) on a complete folder or namespace?
I do NOT want to use something like GhostDoc as a workaround.

Add a new .editorconfig file to that specific folder with this content:
[*.cs]
generated_code = true
dotnet_analyzer_diagnostic.severity = none

To suppress warnings for generated code in a project
Right-click the project in Solution Explorer, and then click
Properties.
Click Code Analysis.
Select the Suppress results from generated code check box.
Reference: How to: Suppress Code Analysis Warnings for Generated Code

Related

VS 2022 - Convert to file-scoped namespace in all files

I'm converting my project to .NET 6 and I want to use filescoped namespaces everywhere. But the conversion tool exists only in the editor.
Has anyone found out if there's a way to run this editor function across all files in solution at once? (Looks like Rider has that function)
Adding a rule to use file scoped namespaces in .editorconfig worked for me:
create an .editorconfig file in the solution directory
add following line/content below (docs, code - IDE0161)
Example .editorconfig file content:
[*.cs]
csharp_style_namespace_declarations = file_scoped:warning
After that the preview changes dialog had an option to apply the fix to the whole project/solution:
I always have problems finding files that are supposed to be updated (.editorconfig in this case). I don't even know if I should search for it in the project's, Visual Studio installation's or any folder on the PC. So I like the answer in the link below because it says where in the interface to change the setting.
Best answer in my opinion is here:
https://www.ilkayilknur.com/how-to-convert-block-scoped-namespacees-to-file-scoped-namespaces
It says that you can change the code-style preference (and enable the display of the option to apply this preference in a document / project / solution) by going to Tools => Options => Text Editor => C#=> Code Style and then changing the related preference.
EditorConfig syntax
csharp_style_namespace_declarations = file_scoped:error
dotnet_diagnostic.IDE0161.severity = error
Note
Syntax option = rule:severity will be deprecated, sooner or later.
I strongly recommend to read this article before you start build .editorconfig for your project.
After you have configured the .editorconfig, you can configure a 'Code Cleanup' setting to automatically convert all files to use file-scoped namespace. Go to Tools -> Options -> Text Editor -> Code Cleanup -> Configure Code Cleanup. Then add the 'Apply namespace preferences'. Then go to Analyze -> Code Cleanup (or just search for 'Code cleanup') and run the Code Cleanup to automatically change the namespaces to file-scoped.

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].

How to hide C# warning using project(.csproj) file

One of the C# project uses multiple C++ DLLs. I want to hide below warning in the same project.
ALINK : warning AL1073: Referenced assembly 'mscorlib.dll' targets a different processor
I know it can be use C# code to do this using code file. But in my case I want to use the project file to do this.
In C++ project file, It can be done by
<Link>
<AdditionalOptions> /ignore:xxxx %(AdditionalOptions)</AdditionalOptions>
</Link>
Is there anyway to do this using C# project file?
In the .csproj file
Add the tag <NoWarn>1073</NoWarn> to the desired build configuration in a <PropertyGroup> tag. Suppress multiple warnings by inserting a ;.
In Visual Studio
Right click the project -> property -> build tab
You can explicitly specify the warning you would like to hide or lower the warning level to do that.
Please see the two posts below for reference:
https://msdn.microsoft.com/en-us/library/jj715718.aspx
https://msdn.microsoft.com/en-us/library/13b90fz7(v=vs.120).aspx

Ignore C# compile warnings for specific sub-folders?

How can I selectively ignore compiler warnings at a folder level without altering the source code files (*.cs)? With the toy example below, I want to ignore CS0001 only for Lib1 (reviewed it to be ok) but nowhere else (since they could be unreviewed). Right now I can ignore compiler warnings only at the project level. Or I need to add #pragma to the source code which makes my source different from the library repository upstream which we cannot directly use (or link).
Project
+-LibSrc
+-Lib1 // Ignore CS0001 here
+-Lib2 // Ignore CS0002 here
+-Lib3
I'm hoping for some sort of project setting or some file places in the folder that signals this to the C# tools.
You can select the files to ignore, on File Properties, set Build Action to None. The compiler would ignore warnings from the files and build.

Disable stylecop analysis for specific projects within solution

Is there a way to easily stop StyleCop warnings from being displayed within specific projects in a solution. Or, more pointedly, a way to select which projects StyleCop analysis should be performed on by Visual Studio?
We have a solution with 9 projects in it. There are only 2 projects that I actually want to see StyleCop warnings for, so I've created StyleCop.Settings files within those project root directories. This means that, for the rest of the projects, the default rule set is applied and I get a screen full of warnings every time I open a class.
The only way I can think to remove these warnings is to add another StyleCop.Settings file a folder level above with all the rules switched off and set merge options on the specific Settings files I am interested in to not merge with this parent file. This just feels wrong though. Is there a cleaner option or is this my only one?
UPDATE: I'm specifically looking for a way to stop the warnings from appearing within Visual Studio. I've added a Settings.StyleCop file to the solution folder and disabled all the rules. I run StyleCop analysis across one of my test projects and there are no errors reported. However, opening a test class reveals a raft of StyleCop warnings, which I want to suppress. Could this be the StyleCop for ReSharper plugin? I have a code cleanup profile created and have disabled certain rules within there but that doesn't appear to make any difference within my test classes.
Please have a look at File Lists configuration - they allow to disable rules by default per project:
Enabled Or Disabled By Default
In addition, a new setting allows you to determine whether rules
should be enabled or disabled by default. This can be set either at
the project level or at the SourceFileList level. For example, here’s
how you would set up a project with all rules disabled by default, and
only two rules explicitly enabled:
<StyleCopSettings Version="4.3">
<GlobalSettings>
<BooleanProperty Name="RulesEnabledByDefault">False</BooleanProperty>
</GlobalSettings>

Categories

Resources