feO2x / Light.GuardClauses

A lightweight .NET library for expressive Guard Clauses.
MIT License
85 stars 8 forks source link

Use with FxCop? #67

Closed rndthoughts closed 4 years ago

rndthoughts commented 5 years ago

In my dotnet core web api project I use FxCop and want to use Light.GuardClauses also. However, FxCop complains about missing ArgumentNull checks when I have added them using the Light.GuardClauses commands. Is this expected behaviour or am I doing something wrong?

feO2x commented 5 years ago

Hey @rndthoughts,

I also stumbled upon this issue in a project that uses FxCop. Unfortunately, I currently have no solution for this issue. What we would have to do is implement a custom Roslyn analyzer that checks if MustNotBeNull or any other assertion guaranteeing that a parameter is not null was called before de-referencing it. This analyzer could be shipped in a separate NuGet package and can then be replaced with the default FxCop Anaylzer (using a ruleset file).

I haven't noticed that yet because, in most projects, I simply use the ReSharper analyzer, which is already supported by Light.GuardClauses.

rndthoughts commented 5 years ago

Yes, I like Resharper too but I cancelled my subscription because it was too expensive considering what Roslyn can do nowadays.

feO2x commented 4 years ago

I will look into this in the upcoming days. The Roslyn analyzer that needs to be replaced is CA1062, the source code of this analyzer is a class named ValidateArgumentsOfPublicMethods in Microsoft.CodeQuality.Analyzers.

I will first have to analyze it thoroughly and then provide an alternative implementation that also checks for MustNotBeNull and other methods that guarantee null-safety.

I will not support Visual Basic - the analyzer to be created will only work with C# projects.

feO2x commented 4 years ago

Hey @rndthoughts, I've just release version 8.0.0 of Light.GuardClauses. It support C# 8 Nullable Reference Types and thus should work with the FxCop Analyzer (if you enable the NRT feature in your projects). Does this solve the issue for you? I looked at a custom Analyzer, but these are pretty complex and I would need a tremendous amount of time to adapt them for the assertions of Light.GuardClauses.

feO2x commented 4 years ago

I will close this issue for now due to inactivity. @rndthoughts please reopen it if we need to investigate this further.