diff --git a/src/Moq.Analyzers/AsShouldBeUsedOnlyForInterfaceAnalyzer.cs b/src/Moq.Analyzers/AsShouldBeUsedOnlyForInterfaceAnalyzer.cs index 5c941962..b5c3be7b 100644 --- a/src/Moq.Analyzers/AsShouldBeUsedOnlyForInterfaceAnalyzer.cs +++ b/src/Moq.Analyzers/AsShouldBeUsedOnlyForInterfaceAnalyzer.cs @@ -80,7 +80,7 @@ private static void Analyze(OperationAnalysisContext context, ImmutableArray().Select(mae => mae.Name).DefaultIfNotSingle(); Location location = memberName?.GetLocation() ?? invocationOperation.Syntax.GetLocation(); - context.ReportDiagnostic(Diagnostic.Create(Rule, location)); + context.ReportDiagnostic(location.CreateDiagnostic(Rule)); } } } diff --git a/src/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodAnalyzer.cs b/src/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodAnalyzer.cs index 8760a9d2..cb973e9e 100644 --- a/src/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodAnalyzer.cs +++ b/src/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodAnalyzer.cs @@ -85,7 +85,7 @@ private static void Analyze(SyntaxNodeAnalysisContext context) if (!string.Equals(mockedMethodTypeName, lambdaParameterTypeName, StringComparison.Ordinal)) { - Diagnostic diagnostic = Diagnostic.Create(Rule, callbackLambda.ParameterList.GetLocation()); + Diagnostic diagnostic = callbackLambda.ParameterList.CreateDiagnostic(Rule); context.ReportDiagnostic(diagnostic); } } diff --git a/src/Moq.Analyzers/ConstructorArgumentsShouldMatchAnalyzer.cs b/src/Moq.Analyzers/ConstructorArgumentsShouldMatchAnalyzer.cs index d53d7779..22ad932b 100644 --- a/src/Moq.Analyzers/ConstructorArgumentsShouldMatchAnalyzer.cs +++ b/src/Moq.Analyzers/ConstructorArgumentsShouldMatchAnalyzer.cs @@ -124,7 +124,7 @@ private static void AnalyzeConcrete( if (constructorArguments != null && IsConstructorMismatch(context, objectCreation, genericName, constructorArguments)) { - Diagnostic diagnostic = Diagnostic.Create(Rule, objectCreation.ArgumentList?.GetLocation()); + Diagnostic diagnostic = objectCreation.ArgumentList.CreateDiagnostic(Rule); context.ReportDiagnostic(diagnostic); } } @@ -159,7 +159,7 @@ private static void AnalyzeAbstract( Debug.Assert(objectCreation.ArgumentList != null, "objectCreation.ArgumentList != null"); - Diagnostic diagnostic = Diagnostic.Create(Rule, objectCreation.ArgumentList?.GetLocation()); + Diagnostic diagnostic = objectCreation.ArgumentList.CreateDiagnostic(Rule); context.ReportDiagnostic(diagnostic); } diff --git a/src/Moq.Analyzers/DiagnosticExtensions.cs b/src/Moq.Analyzers/DiagnosticExtensions.cs new file mode 100644 index 00000000..9887ca03 --- /dev/null +++ b/src/Moq.Analyzers/DiagnosticExtensions.cs @@ -0,0 +1,22 @@ +namespace Moq.Analyzers; + +internal static class DiagnosticExtensions +{ + /// + /// Create a with the given at the 's location. + /// + /// The to use for a for a diagnostic. + /// The to use when creating the diagnostic. + /// A with the given at the 's location. + public static Diagnostic CreateDiagnostic(this SyntaxNode? node, DiagnosticDescriptor descriptor) => + Diagnostic.Create(descriptor, node?.GetLocation()); + + /// + /// Create a with the given at the . + /// + /// The to use for a diagnostic. + /// The to use when creating the diagnostic. + /// A with the given at the . + public static Diagnostic CreateDiagnostic(this Location? location, DiagnosticDescriptor descriptor) => + Diagnostic.Create(descriptor, location); +} diff --git a/src/Moq.Analyzers/NoConstructorArgumentsForInterfaceMockAnalyzer.cs b/src/Moq.Analyzers/NoConstructorArgumentsForInterfaceMockAnalyzer.cs index 775cbf1d..98e7c24c 100644 --- a/src/Moq.Analyzers/NoConstructorArgumentsForInterfaceMockAnalyzer.cs +++ b/src/Moq.Analyzers/NoConstructorArgumentsForInterfaceMockAnalyzer.cs @@ -84,7 +84,7 @@ private static void Analyze(SyntaxNodeAnalysisContext context) { Debug.Assert(objectCreation.ArgumentList != null, "objectCreation.ArgumentList != null"); - Diagnostic diagnostic = Diagnostic.Create(Rule, objectCreation.ArgumentList?.GetLocation()); + Diagnostic diagnostic = objectCreation.ArgumentList.CreateDiagnostic(Rule); context.ReportDiagnostic(diagnostic); } } diff --git a/src/Moq.Analyzers/NoMethodsInPropertySetupAnalyzer.cs b/src/Moq.Analyzers/NoMethodsInPropertySetupAnalyzer.cs index 0880481e..9a4b69c9 100644 --- a/src/Moq.Analyzers/NoMethodsInPropertySetupAnalyzer.cs +++ b/src/Moq.Analyzers/NoMethodsInPropertySetupAnalyzer.cs @@ -48,7 +48,7 @@ private static void Analyze(SyntaxNodeAnalysisContext context) ISymbol? mockedMethodSymbol = context.SemanticModel.GetSymbolInfo(mockedMethodCall, context.CancellationToken).Symbol; if (mockedMethodSymbol == null) return; - Diagnostic diagnostic = Diagnostic.Create(Rule, mockedMethodCall.GetLocation()); + Diagnostic diagnostic = mockedMethodCall.CreateDiagnostic(Rule); context.ReportDiagnostic(diagnostic); } }