| | | 1 | | using Microsoft.CodeAnalysis; |
| | | 2 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 3 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 4 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 5 | | using System.Collections.Immutable; |
| | | 6 | | using System.Linq; |
| | | 7 | | |
| | | 8 | | namespace Pozitron.Analyzers; |
| | | 9 | | |
| | | 10 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 11 | | public sealed class PrivateConstructorNullabilitySuppressor : DiagnosticSuppressor |
| | | 12 | | { |
| | 21 | 13 | | public static SuppressionDescriptor SuppressionDescriptor { get; } = new( |
| | 1 | 14 | | id: "POZ0001", |
| | 1 | 15 | | suppressedDiagnosticId: "CS8618", |
| | 1 | 16 | | justification: "The state is set externally by 3rd party libraries (EF Core, mappers, etc)."); |
| | | 17 | | |
| | 30 | 18 | | public override ImmutableArray<SuppressionDescriptor> SupportedSuppressions { get; } = [SuppressionDescriptor]; |
| | | 19 | | |
| | | 20 | | public override void ReportSuppressions(SuppressionAnalysisContext context) |
| | | 21 | | { |
| | 24 | 22 | | foreach (var diagnostic in context.ReportedDiagnostics) |
| | | 23 | | { |
| | 6 | 24 | | if (SuppressionDescriptor.SuppressedDiagnosticId.Equals(diagnostic.Id)) |
| | | 25 | | { |
| | 6 | 26 | | var node = diagnostic.Location.SourceTree? |
| | 6 | 27 | | .GetRoot(context.CancellationToken) |
| | 6 | 28 | | .FindNode(diagnostic.Location.SourceSpan); |
| | | 29 | | |
| | 6 | 30 | | if (node is ConstructorDeclarationSyntax ctorDeclarationSyntax |
| | 13 | 31 | | && ctorDeclarationSyntax.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.PrivateKeyword))) |
| | | 32 | | { |
| | 2 | 33 | | context.ReportSuppression(Suppression.Create(SuppressionDescriptor, diagnostic)); |
| | | 34 | | } |
| | | 35 | | } |
| | | 36 | | } |
| | 6 | 37 | | } |
| | | 38 | | } |