8 changed files with 74 additions and 4 deletions
@ -0,0 +1,8 @@ |
|||||
|
// This file is used by Code Analysis to maintain SuppressMessage
|
||||
|
// attributes that are applied to this project.
|
||||
|
// Project-level suppressions either have no target or are given
|
||||
|
// a specific target and scoped to a namespace, type, member, etc.
|
||||
|
|
||||
|
using System.Diagnostics.CodeAnalysis; |
||||
|
|
||||
|
[assembly: SuppressMessage("MicrosoftCodeAnalysisReleaseTracking", "RS2008:Enable analyzer release tracking")] |
||||
@ -0,0 +1,59 @@ |
|||||
|
using System.Collections.Immutable; |
||||
|
using System.Linq; |
||||
|
using Microsoft.CodeAnalysis; |
||||
|
using Microsoft.CodeAnalysis.CSharp; |
||||
|
using Microsoft.CodeAnalysis.CSharp.Syntax; |
||||
|
using Microsoft.CodeAnalysis.Diagnostics; |
||||
|
|
||||
|
namespace Avalonia.Analyzers; |
||||
|
|
||||
|
[DiagnosticAnalyzer(LanguageNames.CSharp)] |
||||
|
public class OnPropertyChangedOverrideAnalyzer : DiagnosticAnalyzer |
||||
|
{ |
||||
|
public const string DiagnosticId = "AVA2001"; |
||||
|
|
||||
|
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor( |
||||
|
DiagnosticId, |
||||
|
"Missing invoke base.OnPropertyChanged", |
||||
|
"Method '{0}' do not invoke base.{0}", |
||||
|
"Potential issue", |
||||
|
DiagnosticSeverity.Warning, |
||||
|
isEnabledByDefault: true, |
||||
|
description: "The OnPropertyChanged of the base class was not invoked in the override method declaration, which could lead to unwanted behavior."); |
||||
|
|
||||
|
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule); |
||||
|
|
||||
|
public override void Initialize(AnalysisContext context) |
||||
|
{ |
||||
|
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
||||
|
context.EnableConcurrentExecution(); |
||||
|
context.RegisterSyntaxNodeAction(AnalyzeMethod, SyntaxKind.MethodDeclaration); |
||||
|
} |
||||
|
|
||||
|
private static void AnalyzeMethod(SyntaxNodeAnalysisContext context) |
||||
|
{ |
||||
|
var method = (MethodDeclarationSyntax)context.Node; |
||||
|
if (context.SemanticModel.GetDeclaredSymbol(method, context.CancellationToken) is IMethodSymbol currentMethod |
||||
|
&& currentMethod.Name == "OnPropertyChanged" |
||||
|
&& currentMethod.OverriddenMethod is IMethodSymbol originalMethod) |
||||
|
{ |
||||
|
var baseInvocations = method.Body?.DescendantNodes().OfType<BaseExpressionSyntax>(); |
||||
|
if (baseInvocations?.Any() == true) |
||||
|
{ |
||||
|
foreach (var baseInvocation in baseInvocations) |
||||
|
{ |
||||
|
if (baseInvocation.Parent is SyntaxNode parent) |
||||
|
{ |
||||
|
var targetSymbol = context.SemanticModel.GetSymbolInfo(parent, context.CancellationToken); |
||||
|
if (SymbolEqualityComparer.Default.Equals(targetSymbol.Symbol, originalMethod)) |
||||
|
{ |
||||
|
return; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
context.ReportDiagnostic(Diagnostic.Create(Rule, currentMethod.Locations[0], currentMethod.Name)); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue