Added Sonar analyzers. Fixed some issues (mostly use of new C# features)

This commit is contained in:
Mikael Lindemann 2021-04-01 13:09:02 +02:00
Родитель 5d00558e08
Коммит 5f94e8bbd3
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 28C86AD4F4866E2B
11 изменённых файлов: 39 добавлений и 51 удалений

Просмотреть файл

@ -25,7 +25,7 @@ namespace Piranha.Analyzers
private static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.IncorrectFieldSettingsAnalyzerTitle), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(Resources.IncorrectFieldSettingsAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString Description = new LocalizableResourceString(nameof(Resources.IncorrectFieldSettingsAnalyzerDescription), Resources.ResourceManager, typeof(Resources));
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Error, isEnabledByDefault: true, description: Description);
private static readonly DiagnosticDescriptor Rule = new(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Error, isEnabledByDefault: true, description: Description);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);

Просмотреть файл

@ -25,7 +25,7 @@ namespace Piranha.Analyzers
private static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.InvalidSingleFieldComplexRegionAnalyzerTitle), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(Resources.InvalidSingleFieldComplexRegionAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString Description = new LocalizableResourceString(nameof(Resources.InvalidSingleFieldComplexRegionAnalyzerDescription), Resources.ResourceManager, typeof(Resources));
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Error, isEnabledByDefault: true, description: Description);
private static readonly DiagnosticDescriptor Rule = new(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Error, isEnabledByDefault: true, description: Description);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
@ -39,7 +39,7 @@ namespace Piranha.Analyzers
private static void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context)
{
if (!(context.Node is AttributeSyntax attribute))
if (context.Node is not AttributeSyntax attribute)
{
return;
}
@ -58,12 +58,12 @@ namespace Piranha.Analyzers
return;
}
if (!(attribute.Parent is AttributeListSyntax attributeList))
if (attribute.Parent is not AttributeListSyntax attributeList)
{
return;
}
if (!(attributeList.Parent is PropertyDeclarationSyntax property))
if (attributeList.Parent is not PropertyDeclarationSyntax property)
{
return;
}

Просмотреть файл

@ -84,7 +84,7 @@ namespace Piranha.Analyzers
var node = await reference.GetSyntaxAsync(context.CancellationToken);
if (!(node is ClassDeclarationSyntax regionClassDeclaration))
if (node is not ClassDeclarationSyntax regionClassDeclaration)
{
continue;
}

Просмотреть файл

@ -27,7 +27,7 @@ namespace Piranha.Analyzers
private static readonly LocalizableString Description = new LocalizableResourceString(nameof(Resources.NonSingleFieldRegionAnalyzerDescription), Resources.ResourceManager, typeof(Resources));
private const string Category = "Usage";
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Info, isEnabledByDefault: true, description: Description);
private static readonly DiagnosticDescriptor Rule = new(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Info, isEnabledByDefault: true, description: Description);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
@ -53,7 +53,7 @@ namespace Piranha.Analyzers
private static void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context, string fieldName)
{
if (!(context.Node is PropertyDeclarationSyntax pds))
if (context.Node is not PropertyDeclarationSyntax pds)
{
return;
}

Просмотреть файл

@ -26,7 +26,7 @@ namespace Piranha.Analyzers
private static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.PageTypeAttributeOnlyForClassesInheritingPageAnalyzerTitle), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(Resources.PageTypeAttributeOnlyForClassesInheritingPageAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString Description = new LocalizableResourceString(nameof(Resources.PageTypeAttributeOnlyForClassesInheritingPageAnalyzerDescription), Resources.ResourceManager, typeof(Resources));
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Error, isEnabledByDefault: true, description: Description);
private static readonly DiagnosticDescriptor Rule = new(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Error, isEnabledByDefault: true, description: Description);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
@ -40,7 +40,7 @@ namespace Piranha.Analyzers
private static void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context)
{
if (!(context.Node is AttributeSyntax attribute))
if (context.Node is not AttributeSyntax attribute)
{
return;
}
@ -59,12 +59,12 @@ namespace Piranha.Analyzers
return;
}
if (!(attribute.Parent is AttributeListSyntax attributeList))
if (attribute.Parent is not AttributeListSyntax attributeList)
{
return;
}
if (!(attributeList.Parent is ClassDeclarationSyntax @class))
if (attributeList.Parent is not ClassDeclarationSyntax @class)
{
return;
}
@ -82,7 +82,7 @@ namespace Piranha.Analyzers
{
foreach (var type in @class.BaseList.Types)
{
if (!(context.SemanticModel.GetTypeInfo(type.Type, context.CancellationToken).ConvertedType is INamedTypeSymbol convertedType))
if (context.SemanticModel.GetTypeInfo(type.Type, context.CancellationToken).ConvertedType is not INamedTypeSymbol convertedType)
{
continue;
}

Просмотреть файл

@ -14,6 +14,10 @@
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.9.0" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.20.0.28934">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Compile Update="Resources.Designer.cs" DesignTime="True" AutoGen="True" DependentUpon="Resources.resx" />

Просмотреть файл

@ -26,7 +26,7 @@ namespace Piranha.Analyzers
private static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.PostTypeAttributeOnlyForClassesInheritingPostAnalyzerTitle), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(Resources.PostTypeAttributeOnlyForClassesInheritingPostAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString Description = new LocalizableResourceString(nameof(Resources.PostTypeAttributeOnlyForClassesInheritingPostAnalyzerDescription), Resources.ResourceManager, typeof(Resources));
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Error, isEnabledByDefault: true, description: Description);
private static readonly DiagnosticDescriptor Rule = new(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Error, isEnabledByDefault: true, description: Description);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
@ -40,7 +40,7 @@ namespace Piranha.Analyzers
private static void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context)
{
if (!(context.Node is AttributeSyntax attribute))
if (context.Node is not AttributeSyntax attribute)
{
return;
}
@ -59,12 +59,12 @@ namespace Piranha.Analyzers
return;
}
if (!(attribute.Parent is AttributeListSyntax attributeList))
if (attribute.Parent is not AttributeListSyntax attributeList)
{
return;
}
if (!(attributeList.Parent is ClassDeclarationSyntax @class))
if (attributeList.Parent is not ClassDeclarationSyntax @class)
{
return;
}
@ -82,7 +82,7 @@ namespace Piranha.Analyzers
{
foreach (var type in @class.BaseList.Types)
{
if (!(context.SemanticModel.GetTypeInfo(type.Type, context.CancellationToken).ConvertedType is INamedTypeSymbol convertedType))
if (context.SemanticModel.GetTypeInfo(type.Type, context.CancellationToken).ConvertedType is not INamedTypeSymbol convertedType)
{
continue;
}

Просмотреть файл

@ -26,7 +26,7 @@ namespace Piranha.Analyzers
private static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.SiteTypeAttributeOnlyForClassesInheritingSiteAnalyzerTitle), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(Resources.SiteTypeAttributeOnlyForClassesInheritingSiteAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString Description = new LocalizableResourceString(nameof(Resources.SiteTypeAttributeOnlyForClassesInheritingSiteAnalyzerDescription), Resources.ResourceManager, typeof(Resources));
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Error, isEnabledByDefault: true, description: Description);
private static readonly DiagnosticDescriptor Rule = new(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Error, isEnabledByDefault: true, description: Description);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
@ -40,7 +40,7 @@ namespace Piranha.Analyzers
private static void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context)
{
if (!(context.Node is AttributeSyntax attribute))
if (context.Node is not AttributeSyntax attribute)
{
return;
}
@ -59,12 +59,12 @@ namespace Piranha.Analyzers
return;
}
if (!(attribute.Parent is AttributeListSyntax attributeList))
if (attribute.Parent is not AttributeListSyntax attributeList)
{
return;
}
if (!(attributeList.Parent is ClassDeclarationSyntax @class))
if (attributeList.Parent is not ClassDeclarationSyntax @class)
{
return;
}
@ -82,7 +82,7 @@ namespace Piranha.Analyzers
{
foreach (var type in @class.BaseList.Types)
{
if (!(context.SemanticModel.GetTypeInfo(type.Type, context.CancellationToken).ConvertedType is INamedTypeSymbol convertedType))
if (context.SemanticModel.GetTypeInfo(type.Type, context.CancellationToken).ConvertedType is not INamedTypeSymbol convertedType)
{
continue;
}

Просмотреть файл

@ -28,20 +28,6 @@ namespace TestHelper
/// </summary>
public abstract partial class CodeFixVerifier : DiagnosticVerifier
{
///// <summary>
///// Apply the inputted CodeAction to the inputted document.
///// Meant to be used to apply codefixes.
///// </summary>
///// <param name="document">The Document to apply the fix on</param>
///// <param name="codeAction">A CodeAction that will be applied to the Document.</param>
///// <returns>A Document with the changes from the CodeAction</returns>
//private static Document ApplyFix(Document document, CodeAction codeAction)
//{
// var operations = codeAction.GetOperationsAsync(CancellationToken.None).Result;
// var solution = operations.OfType<ApplyChangesOperation>().Single().ChangedSolution;
// return solution.GetDocument(document.Id);
//}
private static async Task<Project> ApplyFixAsync(Project project, CodeAction codeAction, CancellationToken ct = default)
{
var operations = await codeAction.GetOperationsAsync(ct);

Просмотреть файл

@ -9,6 +9,10 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />
<PackageReference Include="Piranha" Version="9.0.1" />
<PackageReference Include="Piranha.AttributeBuilder" Version="9.0.1" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.20.0.28934">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>

Просмотреть файл

@ -166,25 +166,19 @@ namespace TestHelper
var actualLinePosition = actualSpan.StartLinePosition;
// Only check line position if there is an actual line in the real diagnostic
if (actualLinePosition.Line > 0)
if (actualLinePosition.Line > 0 && actualLinePosition.Line + 1 != expected.Line)
{
if (actualLinePosition.Line + 1 != expected.Line)
{
Assert.True(false,
string.Format("Expected diagnostic to be on line \"{0}\" was actually on line \"{1}\"\r\n\r\nDiagnostic:\r\n {2}\r\n",
expected.Line, actualLinePosition.Line + 1, FormatDiagnostics(analyzer, diagnostic)));
}
Assert.True(false,
string.Format("Expected diagnostic to be on line \"{0}\" was actually on line \"{1}\"\r\n\r\nDiagnostic:\r\n {2}\r\n",
expected.Line, actualLinePosition.Line + 1, FormatDiagnostics(analyzer, diagnostic)));
}
// Only check column position if there is an actual column position in the real diagnostic
if (actualLinePosition.Character > 0)
if (actualLinePosition.Character > 0 && actualLinePosition.Character + 1 != expected.Column)
{
if (actualLinePosition.Character + 1 != expected.Column)
{
Assert.True(false,
string.Format("Expected diagnostic to start at column \"{0}\" was actually at column \"{1}\"\r\n\r\nDiagnostic:\r\n {2}\r\n",
expected.Column, actualLinePosition.Character + 1, FormatDiagnostics(analyzer, diagnostic)));
}
Assert.True(false,
string.Format("Expected diagnostic to start at column \"{0}\" was actually at column \"{1}\"\r\n\r\nDiagnostic:\r\n {2}\r\n",
expected.Column, actualLinePosition.Character + 1, FormatDiagnostics(analyzer, diagnostic)));
}
}
#endregion