From 59f7dafe28cae36f33eecbab40635663c042e4d5 Mon Sep 17 00:00:00 2001 From: Allan Targino <13934447+allantargino@users.noreply.github.com> Date: Mon, 6 Feb 2023 15:50:50 -0300 Subject: [PATCH] LSG: removing unnecessary null forgiven operators and refactor other minor nullability issues (#81662) --- .../gen/LoggerMessageGenerator.Parser.cs | 37 ++++++++++++---------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Parser.cs b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Parser.cs index ecbc1d7..02cde02 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Parser.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Parser.cs @@ -36,28 +36,28 @@ namespace Microsoft.Extensions.Logging.Generators /// public IReadOnlyList GetLogClasses(IEnumerable classes) { - INamedTypeSymbol loggerMessageAttribute = _compilation.GetBestTypeByMetadataName(LoggerMessageAttribute); + INamedTypeSymbol? loggerMessageAttribute = _compilation.GetBestTypeByMetadataName(LoggerMessageAttribute); if (loggerMessageAttribute == null) { // nothing to do if this type isn't available return Array.Empty(); } - INamedTypeSymbol loggerSymbol = _compilation.GetBestTypeByMetadataName("Microsoft.Extensions.Logging.ILogger"); + INamedTypeSymbol? loggerSymbol = _compilation.GetBestTypeByMetadataName("Microsoft.Extensions.Logging.ILogger"); if (loggerSymbol == null) { // nothing to do if this type isn't available return Array.Empty(); } - INamedTypeSymbol logLevelSymbol = _compilation.GetBestTypeByMetadataName("Microsoft.Extensions.Logging.LogLevel"); + INamedTypeSymbol? logLevelSymbol = _compilation.GetBestTypeByMetadataName("Microsoft.Extensions.Logging.LogLevel"); if (logLevelSymbol == null) { // nothing to do if this type isn't available return Array.Empty(); } - INamedTypeSymbol exceptionSymbol = _compilation.GetBestTypeByMetadataName("System.Exception"); + INamedTypeSymbol? exceptionSymbol = _compilation.GetBestTypeByMetadataName("System.Exception"); if (exceptionSymbol == null) { Diag(DiagnosticDescriptors.MissingRequiredType, null, "System.Exception"); @@ -72,9 +72,11 @@ namespace Microsoft.Extensions.Logging.Generators var eventNames = new HashSet(); // we enumerate by syntax tree, to minimize the need to instantiate semantic models (since they're expensive) - foreach (var group in classes.GroupBy(x => x.SyntaxTree)) + foreach (IGrouping group in classes.GroupBy(x => x.SyntaxTree)) { - SemanticModel? sm = null; + SyntaxTree syntaxTree = group.Key; + SemanticModel sm = _compilation.GetSemanticModel(syntaxTree); + foreach (ClassDeclarationSyntax classDec in group) { // stop if we're asked to @@ -98,8 +100,7 @@ namespace Microsoft.Extensions.Logging.Generators continue; } - sm ??= _compilation.GetSemanticModel(classDec.SyntaxTree); - IMethodSymbol? logMethodSymbol = sm.GetDeclaredSymbol(method, _cancellationToken); + IMethodSymbol logMethodSymbol = sm.GetDeclaredSymbol(method, _cancellationToken)!; Debug.Assert(logMethodSymbol != null, "log method is present."); (int eventId, int? level, string message, string? eventName, bool skipEnabledCheck) = (-1, null, string.Empty, null, false); @@ -115,9 +116,9 @@ namespace Microsoft.Extensions.Logging.Generators } bool hasMisconfiguredInput = false; - ImmutableArray? boundAttributes = logMethodSymbol?.GetAttributes(); + ImmutableArray boundAttributes = logMethodSymbol.GetAttributes(); - if (boundAttributes == null || boundAttributes!.Value.Length == 0) + if (boundAttributes.Length == 0) { continue; } @@ -138,6 +139,7 @@ namespace Microsoft.Extensions.Logging.Generators if (typedConstant.Kind == TypedConstantKind.Error) { hasMisconfiguredInput = true; + break; // if a compilation error was found, no need to keep evaluating other args } } @@ -146,7 +148,7 @@ namespace Microsoft.Extensions.Logging.Generators eventId = items[0].IsNull ? -1 : (int)GetItem(items[0]); level = items[1].IsNull ? null : (int?)GetItem(items[1]); - message = items[2].IsNull ? "" : (string)GetItem(items[2]); + message = items[2].IsNull ? string.Empty : (string)GetItem(items[2]); } // argument syntax takes parameters. e.g. EventId = 0 @@ -159,6 +161,7 @@ namespace Microsoft.Extensions.Logging.Generators if (typedConstant.Kind == TypedConstantKind.Error) { hasMisconfiguredInput = true; + break; // if a compilation error was found, no need to keep evaluating other args } else { @@ -178,7 +181,7 @@ namespace Microsoft.Extensions.Logging.Generators eventName = (string?)GetItem(value); break; case "Message": - message = value.IsNull ? "" : (string)GetItem(value); + message = value.IsNull ? string.Empty : (string)GetItem(value); break; } } @@ -307,7 +310,7 @@ namespace Microsoft.Extensions.Logging.Generators break; } - ITypeSymbol paramTypeSymbol = paramSymbol!.Type; + ITypeSymbol paramTypeSymbol = paramSymbol.Type; if (paramTypeSymbol is IErrorTypeSymbol) { // semantic problem, just bail quietly @@ -341,10 +344,10 @@ namespace Microsoft.Extensions.Logging.Generators Type = typeName, Qualifier = qualifier, CodeName = needsAtSign ? "@" + paramName : paramName, - IsLogger = !foundLogger && IsBaseOrIdentity(paramTypeSymbol!, loggerSymbol), - IsException = !foundException && IsBaseOrIdentity(paramTypeSymbol!, exceptionSymbol), - IsLogLevel = !foundLogLevel && IsBaseOrIdentity(paramTypeSymbol!, logLevelSymbol), - IsEnumerable = IsBaseOrIdentity(paramTypeSymbol!, enumerableSymbol) && !IsBaseOrIdentity(paramTypeSymbol!, stringSymbol), + IsLogger = !foundLogger && IsBaseOrIdentity(paramTypeSymbol, loggerSymbol), + IsException = !foundException && IsBaseOrIdentity(paramTypeSymbol, exceptionSymbol), + IsLogLevel = !foundLogLevel && IsBaseOrIdentity(paramTypeSymbol, logLevelSymbol), + IsEnumerable = IsBaseOrIdentity(paramTypeSymbol, enumerableSymbol) && !IsBaseOrIdentity(paramTypeSymbol, stringSymbol), }; foundLogger |= lp.IsLogger; -- 2.7.4