Configure Rider not to delete some unused imports - c#

Is it possible in Rider to configure a list of namespaces that should not be deleted by executing ‘Remove unused imports’? Ideally this would also not mark them as unused. In Resharper this is possible using «Namespace that should always be imported», but I can’t seem to find an equivalent option in Rider.

Related

Instruct Resharper to ignore folders for C#

I'm using Resharper 2018.3.1 and Entity Framework Core. The remaining Resharper warnings are from my Migrations directory where most of this code is code-generated. I've tried disabling the solution-wide analysis for this directory, yet I still get warnings for the code generated files. Is there some magic sauce I'm missing? I am also using a custom EF Core CSharpHelper, is there a way to annotate the code files using that facility?
I was able to override the CSharpMigrationsGenerator's GenerateMigration, GenerateMetadata and GenerateSnapshot methods and place the // ReSharper disable All comment at the start of all of the files.

Is there a way I can have Resharper ignore an unused reference?

Using Resharper, we can right click on References for a project and select Optimize References. This shows us class libraries that are not in use or required by the compiler.
I have a class library that is only to be used as a reference (won't ever be a need to actually use the code). The dll is setup to inject itself upon start up as long as it is part of the references. In case you are curious why this would ever be done, it handles not found and errors for ASP.NET MVC projects (Nuget package page).
Is there any possible way that I can tell Resharper that this reference is either part of the required by the compiler or a part of the used references? I just want to try and prevent developers from removing my dll on accident.
You can interface Resharper with StyleCop. It allow warning in your code based on StyleCop settings.
For each warning there is a way (using the "Resharper bubble") to disable a warning :
http://tof.canardpc.com/view/49d10973-eb25-4a26-90b2-19d872083285.jpg
it's add a comment line in your code to disable alert on the warning ;
// ReSharper disable once RedundantUsingDirective
using My.Unused.Reference;
After some tests, saldy it seems Resharper doesn't care about that when you trigger the "Optimize Reference"

How do I disable suppressing a warning for one solution in a TFS build

I'm using TFS 2010 and have a TFS build setup to build our software. Everything is working just fine.
But, we are getting the following warning:
CSC: Assembly generation -- Referenced assembly 'mscorlib.dll' targets a different processor
This is because some of our code is marked as x86 only, and it is being built on an x64 platform. We cannot change the target platform because of third party software we link to.
Also we are targeting the 2.0 framework, which also cannot be changed at this point.
So, I want to simply suppress this error. Seems straight forward enough.
I simply edited the Build template, and added /p:NoWarn=1607. That works.
BUT!
We have ONE solution which is written in VB.net, instead of C#. This causes that one solution to fail with the following error:
vbc: warning number '1607' for the option nowarn is either not configurable or not valid
How do I disable suppressing this warning on that one solution in my TFS build?
I tried to use a <customPropertiesForBuild> tag in my TFSBuild.proj file but I'm probably not using it correctly.
I know I could simply add this to my project files, but we have 37 solutions, each with multiple project files, so I really don't want to do that.
I don't think you can control that suppression from TFS since it is MSbuild complaining during build (and TFS simply calls MSBuild and collects the results).
There's a specific property that tells msbuild to ignore this kind of warning. Simply add the following line to your top Propertygroup in the project file for those projects generating the warning:
<PropertyGroup>
...
<ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>None</ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>
</PropertyGroup>
You should be able to use Properties metadata on the VB solution's SolutionToBuild item to set NoWarn to an empty value just for that solution:
<SolutionToBuild Include="$(BuildProjectFolderPath)/../../MyVbSolution.sln">
<Targets></Targets>
<Properties>NoWarn=;</Properties>
</SolutionToBuild>
Try that and see if your VB solution will compile without errors.
You can provide a NoWarn Property to MSbuild in TFS Build. One idea also is to edit the build definition, in the "Process" Tab, explore the Advanced=>MSBuild Arguments, and then you supply this "/p:NoWarn=1607" without the qoutes. When you also queue a build, in Parameters Tab=>Advanced=>MSBuild Arguments, enter/p:NoWarn=1607.

Is there a tool which can automatically clean up GlobalSuppressions.cs?

I am using FxCop and StyleCop for my projects as well as GlobalSuppressions.cs to suppress some rules. Some projects have changed a lot and some exclusion I did in the past don't apply anymore. The GlobalSuppressions.cs haven't been cleaned up of those exclusions. Is there a tool which can automatically clean up GlobalSuppressions.cs?
There is no such tool available. The stand-alone FxCop UI tool can help you detect which suppressions are no longer necessary, but there's nothing available for removing the related SuppressMessage attributes from your source code.

Why is it not possible to remove unused references in C#

Is there any reason for it is not possible with Visual Studio to remove unused references (to projects and assemblies) in C# and C++ projects while it is possible to do so from a Visual Basic project (see here)?
I know you can do it with other tools like Resharper, I was just wondering if there was any technical reason for not being able to do this in C# and C++ projects? Or did Microsoft just choose it to work like that. It seems to be a quite useful feature.
Note that the compiler will automatically drop any unused references from the assembly, so at the assembly metadata level this is redundant. It then just becomes an IDE/tooling issue. Would it be impossible? no (although obviously it would need to keep any that are marked for copy-local, to ensure it gets deployed). We can probably assume, therefore, that it is simply a "time to implement vs utility" (compared to other more useful things that could be done).
I'm sure you could write an IDE extension for it if you wanted ;p
I found this suggestion on Microsoft Connect. It sounds like Microsoft actually thinks it is a good idea but just did not have the "time" (read: priority) to implement it. Too bad!
This functionality is there for VB (via the "Unused References" button on the References property page).But is the case of CSharp, For example, a user could add a reference to an assembly in order for it to get copied to the output directory. They might be using the assembly via reflection instead of compiling against it -- in such cases, there is no way for VS to detect that such an assembly is "used". So designing such algorithm is not 100% successful. But flag is a option that assembly mark as "unused" (however, the user would still have the choice as to whether to remove the assembly from the list of references).
Remove unused namespaces can do a bit work towards this.

Categories