diff --git a/.editorconfig b/.editorconfig index d810ecfa..666d479e 100644 --- a/.editorconfig +++ b/.editorconfig @@ -88,9 +88,9 @@ dotnet_remove_unnecessary_suppression_exclusions = none [*.cs] # var preferences -csharp_style_var_elsewhere = false:silent -csharp_style_var_for_built_in_types = false:silent -csharp_style_var_when_type_is_apparent = false:silent +csharp_style_var_elsewhere = false:warning +csharp_style_var_for_built_in_types = false:warning +csharp_style_var_when_type_is_apparent = false:warning # Expression-bodied members csharp_style_expression_bodied_accessors = true:silent diff --git a/Source/Moq.Analyzers/AsShouldBeUsedOnlyForInterfaceAnalyzer.cs b/Source/Moq.Analyzers/AsShouldBeUsedOnlyForInterfaceAnalyzer.cs index 7b8a2289..34fcf7b3 100644 --- a/Source/Moq.Analyzers/AsShouldBeUsedOnlyForInterfaceAnalyzer.cs +++ b/Source/Moq.Analyzers/AsShouldBeUsedOnlyForInterfaceAnalyzer.cs @@ -22,18 +22,18 @@ public override void Initialize(AnalysisContext context) private static void Analyze(SyntaxNodeAnalysisContext context) { - var asInvocation = (InvocationExpressionSyntax)context.Node; + InvocationExpressionSyntax? asInvocation = (InvocationExpressionSyntax)context.Node; if (asInvocation.Expression is MemberAccessExpressionSyntax memberAccessExpression && Helpers.IsMoqAsMethod(context.SemanticModel, memberAccessExpression) && memberAccessExpression.Name is GenericNameSyntax genericName && genericName.TypeArgumentList.Arguments.Count == 1) { - var typeArgument = genericName.TypeArgumentList.Arguments[0]; - var symbolInfo = context.SemanticModel.GetSymbolInfo(typeArgument, context.CancellationToken); + TypeSyntax? typeArgument = genericName.TypeArgumentList.Arguments[0]; + SymbolInfo symbolInfo = context.SemanticModel.GetSymbolInfo(typeArgument, context.CancellationToken); if (symbolInfo.Symbol is ITypeSymbol typeSymbol && typeSymbol.TypeKind != TypeKind.Interface) { - var diagnostic = Diagnostic.Create(Rule, typeArgument.GetLocation()); + Diagnostic? diagnostic = Diagnostic.Create(Rule, typeArgument.GetLocation()); context.ReportDiagnostic(diagnostic); } } diff --git a/Source/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodAnalyzer.cs b/Source/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodAnalyzer.cs index 8ceab124..da27c8af 100644 --- a/Source/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodAnalyzer.cs +++ b/Source/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodAnalyzer.cs @@ -25,46 +25,46 @@ public override void Initialize(AnalysisContext context) private static void Analyze(SyntaxNodeAnalysisContext context) { - var callbackOrReturnsInvocation = (InvocationExpressionSyntax)context.Node; + InvocationExpressionSyntax? callbackOrReturnsInvocation = (InvocationExpressionSyntax)context.Node; - var callbackOrReturnsMethodArguments = callbackOrReturnsInvocation.ArgumentList.Arguments; + SeparatedSyntaxList callbackOrReturnsMethodArguments = callbackOrReturnsInvocation.ArgumentList.Arguments; // Ignoring Callback() and Return() calls without lambda arguments if (callbackOrReturnsMethodArguments.Count == 0) return; if (!Helpers.IsCallbackOrReturnInvocation(context.SemanticModel, callbackOrReturnsInvocation)) return; - var callbackLambda = callbackOrReturnsInvocation.ArgumentList.Arguments[0]?.Expression as ParenthesizedLambdaExpressionSyntax; + ParenthesizedLambdaExpressionSyntax? callbackLambda = callbackOrReturnsInvocation.ArgumentList.Arguments[0]?.Expression as ParenthesizedLambdaExpressionSyntax; // Ignoring callbacks without lambda if (callbackLambda == null) return; // Ignoring calls with no arguments because those are valid in Moq - var lambdaParameters = callbackLambda.ParameterList.Parameters; + SeparatedSyntaxList lambdaParameters = callbackLambda.ParameterList.Parameters; if (lambdaParameters.Count == 0) return; - var setupInvocation = Helpers.FindSetupMethodFromCallbackInvocation(context.SemanticModel, callbackOrReturnsInvocation); - var mockedMethodInvocation = Helpers.FindMockedMethodInvocationFromSetupMethod(setupInvocation); + InvocationExpressionSyntax? setupInvocation = Helpers.FindSetupMethodFromCallbackInvocation(context.SemanticModel, callbackOrReturnsInvocation); + InvocationExpressionSyntax? mockedMethodInvocation = Helpers.FindMockedMethodInvocationFromSetupMethod(setupInvocation); if (mockedMethodInvocation == null) return; - var mockedMethodArguments = mockedMethodInvocation.ArgumentList.Arguments; + SeparatedSyntaxList mockedMethodArguments = mockedMethodInvocation.ArgumentList.Arguments; if (mockedMethodArguments.Count != lambdaParameters.Count) { - var diagnostic = Diagnostic.Create(Rule, callbackLambda.ParameterList.GetLocation()); + Diagnostic? diagnostic = Diagnostic.Create(Rule, callbackLambda.ParameterList.GetLocation()); context.ReportDiagnostic(diagnostic); } else { for (int i = 0; i < mockedMethodArguments.Count; i++) { - var mockedMethodArgumentType = context.SemanticModel.GetTypeInfo(mockedMethodArguments[i].Expression, context.CancellationToken); - var lambdaParameterType = context.SemanticModel.GetTypeInfo(lambdaParameters[i].Type, context.CancellationToken); + TypeInfo mockedMethodArgumentType = context.SemanticModel.GetTypeInfo(mockedMethodArguments[i].Expression, context.CancellationToken); + TypeInfo lambdaParameterType = context.SemanticModel.GetTypeInfo(lambdaParameters[i].Type, context.CancellationToken); string? mockedMethodTypeName = mockedMethodArgumentType.ConvertedType?.ToString(); string? lambdaParameterTypeName = lambdaParameterType.ConvertedType?.ToString(); if (!string.Equals(mockedMethodTypeName, lambdaParameterTypeName, StringComparison.Ordinal)) { - var diagnostic = Diagnostic.Create(Rule, callbackLambda.ParameterList.GetLocation()); + Diagnostic? diagnostic = Diagnostic.Create(Rule, callbackLambda.ParameterList.GetLocation()); context.ReportDiagnostic(diagnostic); } } diff --git a/Source/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodCodeFix.cs b/Source/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodCodeFix.cs index 205806a2..d211dd98 100644 --- a/Source/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodCodeFix.cs +++ b/Source/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodCodeFix.cs @@ -2,6 +2,7 @@ using System.Diagnostics; using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.CodeFixes; +using Microsoft.CodeAnalysis.Text; namespace Moq.Analyzers; @@ -21,15 +22,15 @@ public sealed override FixAllProvider GetFixAllProvider() public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { - var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); + SyntaxNode? root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); if (root == null) { return; } - var diagnostic = context.Diagnostics.First(); - var diagnosticSpan = diagnostic.Location.SourceSpan; + Diagnostic? diagnostic = context.Diagnostics.First(); + TextSpan diagnosticSpan = diagnostic.Location.SourceSpan; // Find the type declaration identified by the diagnostic. ParameterListSyntax? badArgumentListSyntax = root.FindToken(diagnosticSpan.Start) @@ -49,7 +50,7 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) private async Task FixCallbackSignatureAsync(SyntaxNode root, Document document, ParameterListSyntax? oldParameters, CancellationToken cancellationToken) { - var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); + SemanticModel? semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); Debug.Assert(semanticModel != null, nameof(semanticModel) + " != null"); @@ -63,23 +64,23 @@ private async Task FixCallbackSignatureAsync(SyntaxNode root, Document return document; } - var setupMethodInvocation = Helpers.FindSetupMethodFromCallbackInvocation(semanticModel, callbackInvocation); + InvocationExpressionSyntax? setupMethodInvocation = Helpers.FindSetupMethodFromCallbackInvocation(semanticModel, callbackInvocation); Debug.Assert(setupMethodInvocation != null, nameof(setupMethodInvocation) + " != null"); - var matchingMockedMethods = Helpers.GetAllMatchingMockedMethodSymbolsFromSetupMethodInvocation(semanticModel, setupMethodInvocation).ToArray(); + IMethodSymbol[]? matchingMockedMethods = Helpers.GetAllMatchingMockedMethodSymbolsFromSetupMethodInvocation(semanticModel, setupMethodInvocation).ToArray(); if (matchingMockedMethods.Length != 1 || oldParameters == null) { return document; } - var newParameters = SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList(matchingMockedMethods[0].Parameters.Select( + ParameterListSyntax? newParameters = SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList(matchingMockedMethods[0].Parameters.Select( p => { - var type = SyntaxFactory.ParseTypeName(p.Type.ToMinimalDisplayString(semanticModel, oldParameters.SpanStart)); + TypeSyntax? type = SyntaxFactory.ParseTypeName(p.Type.ToMinimalDisplayString(semanticModel, oldParameters.SpanStart)); return SyntaxFactory.Parameter(default, SyntaxFactory.TokenList(), type, SyntaxFactory.Identifier(p.Name), null); }))); - var newRoot = root.ReplaceNode(oldParameters, newParameters); + SyntaxNode? newRoot = root.ReplaceNode(oldParameters, newParameters); return document.WithSyntaxRoot(newRoot); } } diff --git a/Source/Moq.Analyzers/ConstructorArgumentsShouldMatchAnalyzer.cs b/Source/Moq.Analyzers/ConstructorArgumentsShouldMatchAnalyzer.cs index ff6e0a63..2f68714b 100644 --- a/Source/Moq.Analyzers/ConstructorArgumentsShouldMatchAnalyzer.cs +++ b/Source/Moq.Analyzers/ConstructorArgumentsShouldMatchAnalyzer.cs @@ -27,18 +27,18 @@ public override void Initialize(AnalysisContext context) private static void Analyze(SyntaxNodeAnalysisContext context) { - var objectCreation = (ObjectCreationExpressionSyntax)context.Node; + ObjectCreationExpressionSyntax? objectCreation = (ObjectCreationExpressionSyntax)context.Node; - var genericName = GetGenericNameSyntax(objectCreation.Type); + GenericNameSyntax? genericName = GetGenericNameSyntax(objectCreation.Type); if (genericName == null) return; if (!IsMockGenericType(genericName)) return; // Full check that we are calling new Mock() - var constructorSymbol = GetConstructorSymbol(context, objectCreation); + IMethodSymbol? constructorSymbol = GetConstructorSymbol(context, objectCreation); // Vararg parameter is the one that takes all arguments for mocked class constructor - var varArgsConstructorParameter = constructorSymbol?.Parameters.FirstOrDefault(x => x.IsParams); + IParameterSymbol? varArgsConstructorParameter = constructorSymbol?.Parameters.FirstOrDefault(x => x.IsParams); // Vararg parameter are not used, so there are no arguments for mocked class constructor if (varArgsConstructorParameter == null) return; @@ -50,14 +50,14 @@ private static void Analyze(SyntaxNodeAnalysisContext context) return; } - var varArgsConstructorParameterIdx = constructorSymbol.Parameters.IndexOf(varArgsConstructorParameter); + int varArgsConstructorParameterIdx = constructorSymbol.Parameters.IndexOf(varArgsConstructorParameter); // Find mocked type - var mockedTypeSymbol = GetMockedSymbol(context, genericName); + INamedTypeSymbol? mockedTypeSymbol = GetMockedSymbol(context, genericName); if (mockedTypeSymbol == null) return; // Skip first argument if it is not vararg - typically it is MockingBehavior argument - var constructorArguments = objectCreation.ArgumentList?.Arguments.Skip(varArgsConstructorParameterIdx == 0 ? 0 : 1).ToArray(); + ArgumentSyntax[]? constructorArguments = objectCreation.ArgumentList?.Arguments.Skip(varArgsConstructorParameterIdx == 0 ? 0 : 1).ToArray(); if (!mockedTypeSymbol.IsAbstract) { @@ -65,7 +65,7 @@ private static void Analyze(SyntaxNodeAnalysisContext context) && IsConstructorMismatch(context, objectCreation, genericName, constructorArguments) && objectCreation.ArgumentList != null) { - var diagnostic = Diagnostic.Create(Rule, objectCreation.ArgumentList.GetLocation()); + Diagnostic? diagnostic = Diagnostic.Create(Rule, objectCreation.ArgumentList.GetLocation()); context.ReportDiagnostic(diagnostic); } } @@ -95,7 +95,7 @@ private static void Analyze(SyntaxNodeAnalysisContext context) Debug.Assert(objectCreation.ArgumentList != null, "objectCreation.ArgumentList != null"); - var diagnostic = Diagnostic.Create(Rule, objectCreation.ArgumentList?.GetLocation()); + Diagnostic? diagnostic = Diagnostic.Create(Rule, objectCreation.ArgumentList?.GetLocation()); context.ReportDiagnostic(diagnostic); } } @@ -104,9 +104,9 @@ private static void Analyze(SyntaxNodeAnalysisContext context) SyntaxNodeAnalysisContext context, GenericNameSyntax genericName) { - var typeArguments = genericName.TypeArgumentList.Arguments; + SeparatedSyntaxList typeArguments = genericName.TypeArgumentList.Arguments; if (typeArguments.Count != 1) return null; - var mockedTypeSymbolInfo = context.SemanticModel.GetSymbolInfo(typeArguments[0], context.CancellationToken); + SymbolInfo mockedTypeSymbolInfo = context.SemanticModel.GetSymbolInfo(typeArguments[0], context.CancellationToken); if (mockedTypeSymbolInfo.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Class } mockedTypeSymbol) return null; return mockedTypeSymbol; } @@ -154,8 +154,8 @@ private static bool IsMockGenericType(GenericNameSyntax genericName) private static IMethodSymbol? GetConstructorSymbol(SyntaxNodeAnalysisContext context, ObjectCreationExpressionSyntax objectCreation) { - var constructorSymbolInfo = context.SemanticModel.GetSymbolInfo(objectCreation, context.CancellationToken); - var constructorSymbol = constructorSymbolInfo.Symbol as IMethodSymbol; + SymbolInfo constructorSymbolInfo = context.SemanticModel.GetSymbolInfo(objectCreation, context.CancellationToken); + IMethodSymbol? constructorSymbol = constructorSymbolInfo.Symbol as IMethodSymbol; return constructorSymbol?.MethodKind == MethodKind.Constructor && string.Equals( constructorSymbol.ContainingType?.ConstructedFrom.ToDisplayString(), @@ -167,12 +167,12 @@ private static bool IsMockGenericType(GenericNameSyntax genericName) private static bool IsConstructorMismatch(SyntaxNodeAnalysisContext context, ObjectCreationExpressionSyntax objectCreation, GenericNameSyntax genericName, ArgumentSyntax[] constructorArguments) { - var fakeConstructorCall = SyntaxFactory.ObjectCreationExpression( + ObjectCreationExpressionSyntax? fakeConstructorCall = SyntaxFactory.ObjectCreationExpression( genericName.TypeArgumentList.Arguments.First(), SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(constructorArguments)), null); - var mockedClassConstructorSymbolInfo = context.SemanticModel.GetSpeculativeSymbolInfo( + SymbolInfo mockedClassConstructorSymbolInfo = context.SemanticModel.GetSpeculativeSymbolInfo( objectCreation.SpanStart, fakeConstructorCall, SpeculativeBindingOption.BindAsExpression); return mockedClassConstructorSymbolInfo.Symbol == null; diff --git a/Source/Moq.Analyzers/Helpers.cs b/Source/Moq.Analyzers/Helpers.cs index 347eeca0..b942cc1c 100644 --- a/Source/Moq.Analyzers/Helpers.cs +++ b/Source/Moq.Analyzers/Helpers.cs @@ -21,7 +21,7 @@ internal static bool IsMoqAsMethod(SemanticModel semanticModel, MemberAccessExpr internal static bool IsCallbackOrReturnInvocation(SemanticModel semanticModel, InvocationExpressionSyntax callbackOrReturnsInvocation) { - var callbackOrReturnsMethod = callbackOrReturnsInvocation.Expression as MemberAccessExpressionSyntax; + MemberAccessExpressionSyntax? callbackOrReturnsMethod = callbackOrReturnsInvocation.Expression as MemberAccessExpressionSyntax; Debug.Assert(callbackOrReturnsMethod != null, nameof(callbackOrReturnsMethod) + " != null"); @@ -30,7 +30,7 @@ internal static bool IsCallbackOrReturnInvocation(SemanticModel semanticModel, I return false; } - var methodName = callbackOrReturnsMethod.Name.ToString(); + string? methodName = callbackOrReturnsMethod.Name.ToString(); // First fast check before walking semantic model if (!string.Equals(methodName, "Callback", StringComparison.Ordinal) @@ -39,7 +39,7 @@ internal static bool IsCallbackOrReturnInvocation(SemanticModel semanticModel, I return false; } - var symbolInfo = semanticModel.GetSymbolInfo(callbackOrReturnsMethod); + SymbolInfo symbolInfo = semanticModel.GetSymbolInfo(callbackOrReturnsMethod); return symbolInfo.CandidateReason switch { CandidateReason.OverloadResolutionFailure => symbolInfo.CandidateSymbols.Any(IsCallbackOrReturnSymbol), @@ -50,7 +50,7 @@ internal static bool IsCallbackOrReturnInvocation(SemanticModel semanticModel, I internal static InvocationExpressionSyntax? FindSetupMethodFromCallbackInvocation(SemanticModel semanticModel, ExpressionSyntax expression) { - var invocation = expression as InvocationExpressionSyntax; + InvocationExpressionSyntax? invocation = expression as InvocationExpressionSyntax; if (invocation?.Expression is not MemberAccessExpressionSyntax method) return null; if (IsMoqSetupMethod(semanticModel, method)) return invocation; return FindSetupMethodFromCallbackInvocation(semanticModel, method.Expression); @@ -58,20 +58,20 @@ internal static bool IsCallbackOrReturnInvocation(SemanticModel semanticModel, I internal static InvocationExpressionSyntax? FindMockedMethodInvocationFromSetupMethod(InvocationExpressionSyntax? setupInvocation) { - var setupLambdaArgument = setupInvocation?.ArgumentList.Arguments[0].Expression as LambdaExpressionSyntax; + LambdaExpressionSyntax? setupLambdaArgument = setupInvocation?.ArgumentList.Arguments[0].Expression as LambdaExpressionSyntax; return setupLambdaArgument?.Body as InvocationExpressionSyntax; } internal static ExpressionSyntax? FindMockedMemberExpressionFromSetupMethod(InvocationExpressionSyntax? setupInvocation) { - var setupLambdaArgument = setupInvocation?.ArgumentList.Arguments[0].Expression as LambdaExpressionSyntax; + LambdaExpressionSyntax? setupLambdaArgument = setupInvocation?.ArgumentList.Arguments[0].Expression as LambdaExpressionSyntax; return setupLambdaArgument?.Body as ExpressionSyntax; } internal static IEnumerable GetAllMatchingMockedMethodSymbolsFromSetupMethodInvocation(SemanticModel semanticModel, InvocationExpressionSyntax? setupMethodInvocation) { - var setupLambdaArgument = setupMethodInvocation?.ArgumentList.Arguments[0].Expression as LambdaExpressionSyntax; - var mockedMethodInvocation = setupLambdaArgument?.Body as InvocationExpressionSyntax; + LambdaExpressionSyntax? setupLambdaArgument = setupMethodInvocation?.ArgumentList.Arguments[0].Expression as LambdaExpressionSyntax; + InvocationExpressionSyntax? mockedMethodInvocation = setupLambdaArgument?.Body as InvocationExpressionSyntax; return GetAllMatchingSymbols(semanticModel, mockedMethodInvocation); } @@ -79,10 +79,10 @@ internal static IEnumerable GetAllMatchingMockedMethodSymbolsFrom internal static IEnumerable GetAllMatchingSymbols(SemanticModel semanticModel, ExpressionSyntax? expression) where T : class { - var matchingSymbols = new List(); + List? matchingSymbols = new List(); if (expression != null) { - var symbolInfo = semanticModel.GetSymbolInfo(expression); + SymbolInfo symbolInfo = semanticModel.GetSymbolInfo(expression); if (symbolInfo is { CandidateReason: CandidateReason.None, Symbol: T }) { matchingSymbols.Add(symbolInfo.Symbol as T); @@ -100,7 +100,7 @@ private static bool IsCallbackOrReturnSymbol(ISymbol? symbol) { // TODO: Check what is the best way to do such checks if (symbol is not IMethodSymbol methodSymbol) return false; - var methodName = methodSymbol.ToString(); + string? methodName = methodSymbol.ToString(); return methodName.StartsWith("Moq.Language.ICallback", StringComparison.Ordinal) || methodName.StartsWith("Moq.Language.IReturns", StringComparison.Ordinal); } diff --git a/Source/Moq.Analyzers/MoqMethodDescriptor.cs b/Source/Moq.Analyzers/MoqMethodDescriptor.cs index b3a457e4..70289697 100644 --- a/Source/Moq.Analyzers/MoqMethodDescriptor.cs +++ b/Source/Moq.Analyzers/MoqMethodDescriptor.cs @@ -20,7 +20,7 @@ public MoqMethodDescriptor(string shortMethodName, Regex fullMethodNamePattern, public bool IsMoqMethod(SemanticModel semanticModel, MemberAccessExpressionSyntax? method) { - var methodName = method?.Name.ToString(); + string? methodName = method?.Name.ToString(); Debug.Assert(!string.IsNullOrEmpty(methodName), nameof(methodName) + " != null or empty"); @@ -36,7 +36,7 @@ public bool IsMoqMethod(SemanticModel semanticModel, MemberAccessExpressionSynta return false; } - var symbolInfo = semanticModel.GetSymbolInfo(method); + SymbolInfo symbolInfo = semanticModel.GetSymbolInfo(method); return symbolInfo.CandidateReason switch { CandidateReason.OverloadResolutionFailure => symbolInfo.CandidateSymbols.OfType().Any(s => FullMethodNamePattern.IsMatch(s.ToString())), diff --git a/Source/Moq.Analyzers/NoConstructorArgumentsForInterfaceMockAnalyzer.cs b/Source/Moq.Analyzers/NoConstructorArgumentsForInterfaceMockAnalyzer.cs index c5c81d40..1f477f6a 100644 --- a/Source/Moq.Analyzers/NoConstructorArgumentsForInterfaceMockAnalyzer.cs +++ b/Source/Moq.Analyzers/NoConstructorArgumentsForInterfaceMockAnalyzer.cs @@ -27,7 +27,7 @@ public override void Initialize(AnalysisContext context) private static void Analyze(SyntaxNodeAnalysisContext context) { - var objectCreation = (ObjectCreationExpressionSyntax)context.Node; + ObjectCreationExpressionSyntax? objectCreation = (ObjectCreationExpressionSyntax)context.Node; // TODO Think how to make this piece more elegant while fast GenericNameSyntax? genericName = objectCreation.Type as GenericNameSyntax; @@ -42,7 +42,7 @@ private static void Analyze(SyntaxNodeAnalysisContext context) if (!string.Equals(genericName.Identifier.ToFullString(), "Mock", StringComparison.Ordinal)) return; // Full check - var constructorSymbolInfo = context.SemanticModel.GetSymbolInfo(objectCreation, context.CancellationToken); + SymbolInfo constructorSymbolInfo = context.SemanticModel.GetSymbolInfo(objectCreation, context.CancellationToken); if (constructorSymbolInfo.Symbol is not IMethodSymbol constructorSymbol || constructorSymbol.ContainingType == null || constructorSymbol.ContainingType.ConstructedFrom == null) return; if (constructorSymbol.MethodKind != MethodKind.Constructor) return; if (!string.Equals( @@ -57,9 +57,9 @@ private static void Analyze(SyntaxNodeAnalysisContext context) if (!constructorSymbol.Parameters.Any(x => x.IsParams)) return; // Find mocked type - var typeArguments = genericName.TypeArgumentList.Arguments; + SeparatedSyntaxList typeArguments = genericName.TypeArgumentList.Arguments; if (typeArguments.Count != 1) return; - var symbolInfo = context.SemanticModel.GetSymbolInfo(typeArguments[0], context.CancellationToken); + SymbolInfo symbolInfo = context.SemanticModel.GetSymbolInfo(typeArguments[0], context.CancellationToken); if (symbolInfo.Symbol is not INamedTypeSymbol symbol) return; // Checked mocked type @@ -67,7 +67,7 @@ private static void Analyze(SyntaxNodeAnalysisContext context) { Debug.Assert(objectCreation.ArgumentList != null, "objectCreation.ArgumentList != null"); - var diagnostic = Diagnostic.Create(Rule, objectCreation.ArgumentList?.GetLocation()); + Diagnostic? diagnostic = Diagnostic.Create(Rule, objectCreation.ArgumentList?.GetLocation()); context.ReportDiagnostic(diagnostic); } } diff --git a/Source/Moq.Analyzers/NoMethodsInPropertySetupAnalyzer.cs b/Source/Moq.Analyzers/NoMethodsInPropertySetupAnalyzer.cs index 70d8a9fd..c17d929d 100644 --- a/Source/Moq.Analyzers/NoMethodsInPropertySetupAnalyzer.cs +++ b/Source/Moq.Analyzers/NoMethodsInPropertySetupAnalyzer.cs @@ -25,19 +25,19 @@ public override void Initialize(AnalysisContext context) private static void Analyze(SyntaxNodeAnalysisContext context) { - var setupGetOrSetInvocation = (InvocationExpressionSyntax)context.Node; + InvocationExpressionSyntax? setupGetOrSetInvocation = (InvocationExpressionSyntax)context.Node; if (setupGetOrSetInvocation.Expression is not MemberAccessExpressionSyntax setupGetOrSetMethod) return; if (!string.Equals(setupGetOrSetMethod.Name.ToFullString(), "SetupGet", StringComparison.Ordinal) && !string.Equals(setupGetOrSetMethod.Name.ToFullString(), "SetupSet", StringComparison.Ordinal)) return; - var mockedMethodCall = Helpers.FindMockedMethodInvocationFromSetupMethod(setupGetOrSetInvocation); + InvocationExpressionSyntax? mockedMethodCall = Helpers.FindMockedMethodInvocationFromSetupMethod(setupGetOrSetInvocation); if (mockedMethodCall == null) return; - var mockedMethodSymbol = context.SemanticModel.GetSymbolInfo(mockedMethodCall, context.CancellationToken).Symbol; + ISymbol? mockedMethodSymbol = context.SemanticModel.GetSymbolInfo(mockedMethodCall, context.CancellationToken).Symbol; if (mockedMethodSymbol == null) return; - var diagnostic = Diagnostic.Create(Rule, mockedMethodCall.GetLocation()); + Diagnostic? diagnostic = Diagnostic.Create(Rule, mockedMethodCall.GetLocation()); context.ReportDiagnostic(diagnostic); } } diff --git a/Source/Moq.Analyzers/NoSealedClassMocksAnalyzer.cs b/Source/Moq.Analyzers/NoSealedClassMocksAnalyzer.cs index 436f826f..fbd06adf 100644 --- a/Source/Moq.Analyzers/NoSealedClassMocksAnalyzer.cs +++ b/Source/Moq.Analyzers/NoSealedClassMocksAnalyzer.cs @@ -25,7 +25,7 @@ public override void Initialize(AnalysisContext context) private static void Analyze(SyntaxNodeAnalysisContext context) { - var objectCreation = (ObjectCreationExpressionSyntax)context.Node; + ObjectCreationExpressionSyntax? objectCreation = (ObjectCreationExpressionSyntax)context.Node; // TODO Think how to make this piece more elegant while fast GenericNameSyntax? genericName = objectCreation.Type as GenericNameSyntax; @@ -40,7 +40,7 @@ private static void Analyze(SyntaxNodeAnalysisContext context) if (!string.Equals(genericName.Identifier.ToFullString(), "Mock", StringComparison.Ordinal)) return; // Full check - var constructorSymbolInfo = context.SemanticModel.GetSymbolInfo(objectCreation, context.CancellationToken); + SymbolInfo constructorSymbolInfo = context.SemanticModel.GetSymbolInfo(objectCreation, context.CancellationToken); if (constructorSymbolInfo.Symbol is not IMethodSymbol constructorSymbol || constructorSymbol.ContainingType == null || constructorSymbol.ContainingType.ConstructedFrom == null) return; if (constructorSymbol.MethodKind != MethodKind.Constructor) return; if (!string.Equals( @@ -49,15 +49,15 @@ private static void Analyze(SyntaxNodeAnalysisContext context) StringComparison.Ordinal)) return; // Find mocked type - var typeArguments = genericName.TypeArgumentList.Arguments; + SeparatedSyntaxList typeArguments = genericName.TypeArgumentList.Arguments; if (typeArguments.Count != 1) return; - var symbolInfo = context.SemanticModel.GetSymbolInfo(typeArguments[0], context.CancellationToken); + SymbolInfo symbolInfo = context.SemanticModel.GetSymbolInfo(typeArguments[0], context.CancellationToken); if (symbolInfo.Symbol is not INamedTypeSymbol symbol) return; // Checked mocked type if (symbol.IsSealed && symbol.TypeKind != TypeKind.Delegate) { - var diagnostic = Diagnostic.Create(Rule, typeArguments[0].GetLocation()); + Diagnostic? diagnostic = Diagnostic.Create(Rule, typeArguments[0].GetLocation()); context.ReportDiagnostic(diagnostic); } } diff --git a/Source/Moq.Analyzers/SetupShouldBeUsedOnlyForOverridableMembersAnalyzer.cs b/Source/Moq.Analyzers/SetupShouldBeUsedOnlyForOverridableMembersAnalyzer.cs index 75cc2f79..d829e24c 100644 --- a/Source/Moq.Analyzers/SetupShouldBeUsedOnlyForOverridableMembersAnalyzer.cs +++ b/Source/Moq.Analyzers/SetupShouldBeUsedOnlyForOverridableMembersAnalyzer.cs @@ -22,21 +22,21 @@ public override void Initialize(AnalysisContext context) private static void Analyze(SyntaxNodeAnalysisContext context) { - var setupInvocation = (InvocationExpressionSyntax)context.Node; + InvocationExpressionSyntax? setupInvocation = (InvocationExpressionSyntax)context.Node; if (setupInvocation.Expression is MemberAccessExpressionSyntax memberAccessExpression && Helpers.IsMoqSetupMethod(context.SemanticModel, memberAccessExpression)) { - var mockedMemberExpression = Helpers.FindMockedMemberExpressionFromSetupMethod(setupInvocation); + ExpressionSyntax? mockedMemberExpression = Helpers.FindMockedMemberExpressionFromSetupMethod(setupInvocation); if (mockedMemberExpression == null) { return; } - var symbolInfo = context.SemanticModel.GetSymbolInfo(mockedMemberExpression, context.CancellationToken); + SymbolInfo symbolInfo = context.SemanticModel.GetSymbolInfo(mockedMemberExpression, context.CancellationToken); if (symbolInfo.Symbol is IPropertySymbol or IMethodSymbol && !IsMethodOverridable(symbolInfo.Symbol)) { - var diagnostic = Diagnostic.Create(Rule, mockedMemberExpression.GetLocation()); + Diagnostic? diagnostic = Diagnostic.Create(Rule, mockedMemberExpression.GetLocation()); context.ReportDiagnostic(diagnostic); } } diff --git a/Source/Moq.Analyzers/SetupShouldNotIncludeAsyncResultAnalyzer.cs b/Source/Moq.Analyzers/SetupShouldNotIncludeAsyncResultAnalyzer.cs index 9bcfe052..25b81115 100644 --- a/Source/Moq.Analyzers/SetupShouldNotIncludeAsyncResultAnalyzer.cs +++ b/Source/Moq.Analyzers/SetupShouldNotIncludeAsyncResultAnalyzer.cs @@ -22,22 +22,22 @@ public override void Initialize(AnalysisContext context) private static void Analyze(SyntaxNodeAnalysisContext context) { - var setupInvocation = (InvocationExpressionSyntax)context.Node; + InvocationExpressionSyntax? setupInvocation = (InvocationExpressionSyntax)context.Node; if (setupInvocation.Expression is MemberAccessExpressionSyntax memberAccessExpression && Helpers.IsMoqSetupMethod(context.SemanticModel, memberAccessExpression)) { - var mockedMemberExpression = Helpers.FindMockedMemberExpressionFromSetupMethod(setupInvocation); + ExpressionSyntax? mockedMemberExpression = Helpers.FindMockedMemberExpressionFromSetupMethod(setupInvocation); if (mockedMemberExpression == null) { return; } - var symbolInfo = context.SemanticModel.GetSymbolInfo(mockedMemberExpression, context.CancellationToken); + SymbolInfo symbolInfo = context.SemanticModel.GetSymbolInfo(mockedMemberExpression, context.CancellationToken); if ((symbolInfo.Symbol is IPropertySymbol || symbolInfo.Symbol is IMethodSymbol) && !IsMethodOverridable(symbolInfo.Symbol) && IsMethodReturnTypeTask(symbolInfo.Symbol)) { - var diagnostic = Diagnostic.Create(Rule, mockedMemberExpression.GetLocation()); + Diagnostic? diagnostic = Diagnostic.Create(Rule, mockedMemberExpression.GetLocation()); context.ReportDiagnostic(diagnostic); } } @@ -51,7 +51,7 @@ private static bool IsMethodOverridable(ISymbol methodSymbol) private static bool IsMethodReturnTypeTask(ISymbol methodSymbol) { - var type = methodSymbol.ToDisplayString(); + string? type = methodSymbol.ToDisplayString(); return string.Equals(type, "System.Threading.Tasks.Task", StringComparison.Ordinal) || string.Equals(type, "System.Threading.ValueTask", StringComparison.Ordinal) || type.StartsWith("System.Threading.Tasks.Task<", StringComparison.Ordinal)