From 5a5ef7c3f9b2eb8ed73001433acd11eddd3e53bb Mon Sep 17 00:00:00 2001 From: mary-georgiou-sonarsource Date: Tue, 4 Jun 2024 10:37:12 +0200 Subject: [PATCH 1/2] fix FP --- .../SonarAnalyzer.CSharp/Rules/UseConstantLoggingTemplate.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/analyzers/src/SonarAnalyzer.CSharp/Rules/UseConstantLoggingTemplate.cs b/analyzers/src/SonarAnalyzer.CSharp/Rules/UseConstantLoggingTemplate.cs index a2413a37ead..7aa4de06222 100644 --- a/analyzers/src/SonarAnalyzer.CSharp/Rules/UseConstantLoggingTemplate.cs +++ b/analyzers/src/SonarAnalyzer.CSharp/Rules/UseConstantLoggingTemplate.cs @@ -114,7 +114,7 @@ private static CSharpSyntaxNode ArgumentValue(InvocationExpressionSyntax invocat private static SyntaxNode InvalidSyntaxNode(SyntaxNode messageArgument, SemanticModel model) => messageArgument.DescendantNodesAndSelf().FirstOrDefault(x => - x.Kind() == SyntaxKind.InterpolatedStringExpression + (x as InterpolatedStringExpressionSyntax is { } interpolatedString && !interpolatedString.HasConstantValue(model)) || (x is BinaryExpressionSyntax { RawKind: (int)SyntaxKind.AddExpression } concatenation && !AllMembersAreConstantStrings(concatenation, model)) || IsStringFormatInvocation(x, model)); From 06ca6d578889477551e2dd0a333e9cad0bd6ec12 Mon Sep 17 00:00:00 2001 From: mary-georgiou-sonarsource Date: Tue, 4 Jun 2024 10:37:42 +0200 Subject: [PATCH 2/2] Cayc --- .../SonarAnalyzer.CSharp/Rules/UseConstantLoggingTemplate.cs | 2 +- .../SonarAnalyzer.Test/TestCases/UseConstantLoggingTemplate.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/analyzers/src/SonarAnalyzer.CSharp/Rules/UseConstantLoggingTemplate.cs b/analyzers/src/SonarAnalyzer.CSharp/Rules/UseConstantLoggingTemplate.cs index 7aa4de06222..591bc10b7f3 100644 --- a/analyzers/src/SonarAnalyzer.CSharp/Rules/UseConstantLoggingTemplate.cs +++ b/analyzers/src/SonarAnalyzer.CSharp/Rules/UseConstantLoggingTemplate.cs @@ -94,7 +94,7 @@ protected override void Initialize(SonarAnalysisContext context) => && ArgumentValue(invocation, method, messageParameter) is { } argumentValue && InvalidSyntaxNode(argumentValue, c.SemanticModel) is { } invalidNode) { - c.ReportIssue(Diagnostic.Create(Rule, invalidNode.GetLocation(), Messages[invalidNode.Kind()])); + c.ReportIssue(Rule, invalidNode.GetLocation(), Messages[invalidNode.Kind()]); } }, SyntaxKind.InvocationExpression); diff --git a/analyzers/tests/SonarAnalyzer.Test/TestCases/UseConstantLoggingTemplate.cs b/analyzers/tests/SonarAnalyzer.Test/TestCases/UseConstantLoggingTemplate.cs index 309f91a4268..ad019cfddd3 100644 --- a/analyzers/tests/SonarAnalyzer.Test/TestCases/UseConstantLoggingTemplate.cs +++ b/analyzers/tests/SonarAnalyzer.Test/TestCases/UseConstantLoggingTemplate.cs @@ -42,7 +42,7 @@ public void BasicScenarios(ILogger logger, int arg) logger.Log(LogLevel.Warning, "First " + "Second " + "Third"); // Compliant - all strings in the concatenation are constants, the compiler can optimize it logger.Log(LogLevel.Warning, FieldConstant + localConstant); // Compliant logger.Log(LogLevel.Warning, FieldConstant + "Second"); // Compliant - logger.Log(LogLevel.Warning, $"Constant: {FieldConstant}"); // Noncompliant FP Repro https://github.com/SonarSource/sonar-dotnet/issues/9247 + logger.Log(LogLevel.Warning, $"Constant: {FieldConstant}"); // Compliant, see https://github.com/SonarSource/sonar-dotnet/issues/9247 logger.Log(LogLevel.Warning, "First " + arg + "Third"); // Noncompliant logger.Log(LogLevel.Warning, ("First " + "Second").ToLower()); // FN