Skip to content

Commit

Permalink
Rule S3416: Fix FPs (#8932)
Browse files Browse the repository at this point in the history
  • Loading branch information
costin-zaharia-sonarsource authored Mar 15, 2024
1 parent eea7d8d commit c24cc1f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
1 change: 0 additions & 1 deletion analyzers/rspec/cs/Sonar_way_profile.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@
"S3398",
"S3400",
"S3415",
"S3416",
"S3427",
"S3433",
"S3440",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ private static void Process(SonarSyntaxNodeReportingContext context)

if (invocation.GetName() is "GetLogger" or "CreateLogger"
&& invocation.Ancestors().OfType<TypeDeclarationSyntax>().FirstOrDefault() is { } enclosingType // filter out top-level statements
&& !IsArgumentInObjectCreation(invocation)
&& ExtractArgument(invocation) is { } argument
&& context.SemanticModel.GetSymbolInfo(invocation).Symbol is IMethodSymbol method
&& IsValidMethod(method)
Expand All @@ -61,6 +62,9 @@ private static void Process(SonarSyntaxNodeReportingContext context)
}
}

private static bool IsArgumentInObjectCreation(InvocationExpressionSyntax invocation) =>
invocation.Ancestors().OfType<ObjectCreationExpressionSyntax>().Any();

// Extracts T for generic argument, nameof or typeof expressions
private static SyntaxNode ExtractArgument(InvocationExpressionSyntax invocation)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class LoggersShouldBeNamedForEnclosingTypeTest
public void LoggersShouldBeNamedForEnclosingType_CS() =>
Builder
.AddPaths("LoggersShouldBeNamedForEnclosingType.cs")
.AddReferences(NuGetMetadataReference.MicrosoftExtensionsLoggingAbstractions())
.AddReferences(NuGetMetadataReference.MicrosoftExtensionsLoggingPackages(Constants.NuGetLatestVersion))
.Verify();

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,53 @@ class LoggerHelper
ILogger<T> CreateLogger<T>() => factory.CreateLogger<T>(); // Compliant, T is GenericType
}

public class Service
{
public Service(ILogger<Service> logger)
{
}

public Service(ILogger<Service> logger, string otherParameter)
{
}
}

public class Factory
{
private readonly ILogger<Service> logger = LoggerFactory.Create(builder => { }).CreateLogger<Service>(); // Noncompliant

public Service CreateType(ILoggerFactory loggerFactory)
{
return new Service(loggerFactory.CreateLogger<Service>());
}

public Service CreateType(ILoggerFactory loggerFactory, string otherParameter)
{
return new Service(loggerFactory.CreateLogger<Service>(), otherParameter);
}

public Service CreateType_LocalVariable(ILoggerFactory loggerFactory)
{
var logger = loggerFactory.CreateLogger<Service>(); // Noncompliant FP
return new Service(logger);
}

public Service CreateType_LocalVariableField()
{
return new Service(logger);
}

public Service CreateType_Decorator(ILoggerFactory loggerFactory)
{
return new Service(new Decorator<Service>(loggerFactory.CreateLogger<Service>()));
}

private class Decorator<T> : ILogger<T>
{
public Decorator(ILogger<T> logger) { }
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) { }
public bool IsEnabled(LogLevel logLevel) => true;
public IDisposable BeginScope<TState>(TState state) => null;
}
}

0 comments on commit c24cc1f

Please sign in to comment.