DotNetAnalyzers / StyleCopAnalyzers

An implementation of StyleCop rules using the .NET Compiler Platform
MIT License
2.66k stars 508 forks source link

SA1508 SA1502 SA1500 Nullreference Exception & SA1106 false positive #3871

Closed IvanLieckens closed 4 months ago

IvanLieckens commented 4 months ago

The SA1508, SA1502 and SA1500 throw nullreference exceptions: AD0001 Analyzer 'StyleCop.Analyzers.LayoutRules.SA1508ClosingBracesMustNotBePrecededByBlankLine' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'. C:\Code\Mvp.Selections\src\Mvp.Selections.Data\CSC 1
AD0001 Analyzer 'StyleCop.Analyzers.LayoutRules.SA1502ElementMustNotBeOnASingleLine' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'. C:\Code\Mvp.Selections\src\Mvp.Selections.Data\CSC 1
AD0001 Analyzer 'StyleCop.Analyzers.LayoutRules.SA1500BracesForMultiLineStatementsMustNotShareLine' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'. C:\Code\Mvp.Selections\src\Mvp.Selections.Data\CSC 1

I believe this is caused by the classes that use the single line class with primary constructor syntax:

using System.Diagnostics.CodeAnalysis;
using Mvp.Selections.Data.Interfaces;
using Mvp.Selections.Data.Repositories.Interfaces;
using Mvp.Selections.Domain;

namespace Mvp.Selections.Data.Repositories
{
    [SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1106:Code should not contain empty statements", Justification = "New C# primary constructor syntax.")]
    public class CountryRepository(Context context, ICurrentUserNameProvider currentUserNameProvider)
        : BaseRepository<Country, short>(context, currentUserNameProvider), ICountryRepository;
}

SA1106 I'm suppressing but is in this case a false positive I believe? (Might warrant its own separate issue?)

bjornhellander commented 4 months ago

I am assuming that you are using version 1.1.118. There hasn't been any official release in quite some time. To have a good experience when using modern c# feature, you need to use the beta releases.

IvanLieckens commented 4 months ago

That's correct. To use the beta I should pull the StyleCop.Analyzers.Unstable package instead?

bjornhellander commented 4 months ago

If you are using Visual Studio, there is a "Include prerelease" checkbox in the NuGet Package Manager window that you can check to see the betas. Another alternative is to use the Unstable package that you mentioned. The content is the same. The Unstable package is mostly for those that are not allowed to use pre-releases.

IvanLieckens commented 4 months ago

Thank you for your aid, I have swapped to using the beta and indeed no longer see this issue. I'll close my issue.