Skip to content

Commit

Permalink
LSG: removing unnecessary null forgiven operators and refactor other …
Browse files Browse the repository at this point in the history
…minor nullability issues (#81662)
  • Loading branch information
allantargino authored Feb 6, 2023
1 parent c606f5a commit 59f7daf
Showing 1 changed file with 20 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,28 @@ public Parser(Compilation compilation, Action<Diagnostic> reportDiagnostic, Canc
/// </summary>
public IReadOnlyList<LoggerClass> GetLogClasses(IEnumerable<ClassDeclarationSyntax> 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<LoggerClass>();
}

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<LoggerClass>();
}

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<LoggerClass>();
}

INamedTypeSymbol exceptionSymbol = _compilation.GetBestTypeByMetadataName("System.Exception");
INamedTypeSymbol? exceptionSymbol = _compilation.GetBestTypeByMetadataName("System.Exception");
if (exceptionSymbol == null)
{
Diag(DiagnosticDescriptors.MissingRequiredType, null, "System.Exception");
Expand All @@ -72,9 +72,11 @@ public IReadOnlyList<LoggerClass> GetLogClasses(IEnumerable<ClassDeclarationSynt
var eventNames = new HashSet<string>();

// 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<SyntaxTree, ClassDeclarationSyntax> 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
Expand All @@ -98,8 +100,7 @@ public IReadOnlyList<LoggerClass> GetLogClasses(IEnumerable<ClassDeclarationSynt
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);

Expand All @@ -115,9 +116,9 @@ public IReadOnlyList<LoggerClass> GetLogClasses(IEnumerable<ClassDeclarationSynt
}

bool hasMisconfiguredInput = false;
ImmutableArray<AttributeData>? boundAttributes = logMethodSymbol?.GetAttributes();
ImmutableArray<AttributeData> boundAttributes = logMethodSymbol.GetAttributes();

if (boundAttributes == null || boundAttributes!.Value.Length == 0)
if (boundAttributes.Length == 0)
{
continue;
}
Expand All @@ -138,6 +139,7 @@ public IReadOnlyList<LoggerClass> GetLogClasses(IEnumerable<ClassDeclarationSynt
if (typedConstant.Kind == TypedConstantKind.Error)
{
hasMisconfiguredInput = true;
break; // if a compilation error was found, no need to keep evaluating other args
}
}

Expand All @@ -146,7 +148,7 @@ public IReadOnlyList<LoggerClass> GetLogClasses(IEnumerable<ClassDeclarationSynt

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
Expand All @@ -159,6 +161,7 @@ public IReadOnlyList<LoggerClass> GetLogClasses(IEnumerable<ClassDeclarationSynt
if (typedConstant.Kind == TypedConstantKind.Error)
{
hasMisconfiguredInput = true;
break; // if a compilation error was found, no need to keep evaluating other args
}
else
{
Expand All @@ -178,7 +181,7 @@ public IReadOnlyList<LoggerClass> GetLogClasses(IEnumerable<ClassDeclarationSynt
eventName = (string?)GetItem(value);
break;
case "Message":
message = value.IsNull ? "" : (string)GetItem(value);
message = value.IsNull ? string.Empty : (string)GetItem(value);
break;
}
}
Expand Down Expand Up @@ -307,7 +310,7 @@ public IReadOnlyList<LoggerClass> GetLogClasses(IEnumerable<ClassDeclarationSynt
break;
}

ITypeSymbol paramTypeSymbol = paramSymbol!.Type;
ITypeSymbol paramTypeSymbol = paramSymbol.Type;
if (paramTypeSymbol is IErrorTypeSymbol)
{
// semantic problem, just bail quietly
Expand Down Expand Up @@ -341,10 +344,10 @@ public IReadOnlyList<LoggerClass> GetLogClasses(IEnumerable<ClassDeclarationSynt
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;
Expand Down

0 comments on commit 59f7daf

Please sign in to comment.