Please provide
- Operating system: Windows 10
- Visual Studio version: VS22
- SonarLint plugin version:7.8.0.88494
- Programming language you’re coding in: C#
- Is connected mode used: No
- Connected to SonarCloud or SonarQube (and which version):
And a thorough description of the problem / question:
This example shows that S2629 is giving a false positive:
using Microsoft.Extensions.Logging;
namespace SonarLintProblems.Models
{
public static class PlaceHolderNames
{
public const string DeviceId = "DeviceId";
public const string TemperatureC = "TemperatureC";
public const string Name = "Name";
}
public class NotHappyWithInterpolatingPlaceholderNames
{
private readonly ILogger<NotHappyWithInterpolatingPlaceholderNames>? _logger;
public NotHappyWithInterpolatingPlaceholderNames(ILoggerFactory? loggerFactory)
{
_logger = loggerFactory?.CreateLogger<NotHappyWithInterpolatingPlaceholderNames>();
}
public void S2629FalsePositive()
{
int deviceId = 1;
double temperatureC = 1.1;
string name = "whatever";
// S6676
_logger?.LogInformation("deviceId:{} name:{} temperatureC:{}", deviceId, name, temperatureC);
// S2629 and CA2017
// S2629 is a false positive here because the things inside {} are constants
_logger?.LogInformation($"deviceId:{PlaceHolderNames.DeviceId} name:{PlaceHolderNames.Name} temperatureC:{PlaceHolderNames.TemperatureC}", deviceId, name, temperatureC);
// SonarLint is OK with it, but it violates DRY for the named placeholder names
// because it will be useful in other places to log device id
// and it is not ideal to hardcode "DeviceId" in potentially many different places
_logger?.LogInformation("deviceId:{DeviceId} name:{Name} temperatureC:{TemperatureC}", deviceId, name, temperatureC);
}
}
}