FP CSharp S2857

The S2857 Rule says to add spaces around a table name which is included by string interpolation in csharp

i.e

  const string tableName = "MyTableName";
  const string strQuery = $"SELECT * FROM [{tableName}] ";

states to change to

  const string tableName = "MyTableName";
  const string strQuery = $"SELECT * FROM [ {tableName} ] ";

This is a failure in sqlite as the spaces then means the select statement does not work

In this case the [ and ] could be omitted of course , but are included as the table may may be defined elsewhere.

The same error occurs however with

 const string tableName = "My Table Name";
 const string strQuery = $"SELECT * FROM [{tableName}] ";

(I make no comment on the practice of having spaces in table names !)

This looks similar to a April 2024 complaint about string literals ? Repro for Fix S2857 FP: Multiple FP on String Interpolation with Verbatim Literal by martin-strecker-sonarsource · Pull Request #9181 · SonarSource/sonar-dotnet · GitHub

I agree that this is a FP. I would argue though that is should not try to report an table names containing spaces. In most cases it would be hard/impossible to resolve the actual name.

Do I understand correctly that you use consts for table names to have have the name defined only once, or is this just a way to make the reproduction of the issue as easy as possible?

1 Like

Hello @TonyJ,

I confirm this is a false positive. I added a ticket to our backlog (internal access only) to fix this issue in a future sprint.

Thanks!