Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix S2857 FP: Multiple FP on String Interpolation with Verbatim Literal #9177

Open
RicardoLans opened this issue Apr 22, 2024 · 1 comment
Open
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.

Comments

@RicardoLans
Copy link

Description

When using string interpolation together with verbatim literal we get multiple false positives reported.

Repro steps

string sqlQuery = $@"
	SELECT
		[Artifact].[Id] AS [{nameof(ArtifactDto.Id)}],
		[Artifact].[TagIdentifier] AS [{nameof(ArtifactDto.TagIdentifier)}],
		[Artifact].[DomainSpecificId] AS [{nameof(ArtifactDto.DomainSpecificId)}],
		[Artifact].[TagReplacements] AS [{nameof(ArtifactDto.TagReplacements)}],
		[Artifact].[LocationId] AS [{nameof(ArtifactDto.LocationId)}],
		[Location].[Name] AS [{nameof(ArtifactDto.LocationName)}]
	FROM
		[Artifacts] AS [Artifact]
	JOIN
		[Locations] AS [Location]
	ON
		[Artifact].[LocationId] = [Location].[Id]";

Expected behavior

Expected to have no issues raised by analyzer on this piece of code as when using debugger it shows exactly the parsed string contains enough whitespace to be valid SQL.

Actual behavior

12 issues are raised by the analyzer:
Add a space before 'Id' on {nameof(ArtifactDto.Id)}
Add a space before '],' on ArtifactDto.Id)}],
Add a space before 'TagIdentifier' on {nameof(ArtifactDto.TagIdentifier)}
Add a space before '],' on ArtifactDto.TagIdentifier)}],
Add a space before 'DomainSpecificId' on {nameof(ArtifactDto.DomainSpecificId)}
Add a space before '],' on ArtifactDto.DomainSpecificId)}],
Add a space before 'TagReplacements' on {nameof(ArtifactDto.TagReplacements)}
Add a space before '],' on ArtifactDto.TagReplacements)}],
Add a space before 'LocationId' on {nameof(ArtifactDto.LocationId)}
Add a space before '],' on ArtifactDto.LocationId)}],
Add a space before 'LocationName' on {nameof(ArtifactDto.LocationName)}
Add a space before '],' on ArtifactDto.LocationName)}],

Known workarounds

N/A

Related information

  • C#/VB.NET Plugins version: C#12
  • Visual Studio version: VS2022 (17.9.6)
  • MSBuild / dotnet version: MSBuild version 17.9.6+a4ecab324 for .NET / dotnet 8.0.204
  • SonarScanner for .NET version (if used): sonar-dotnet analyzer 9.23.1.88495
  • Operating System: Windows 10 Pro 22H2
@martin-strecker-sonarsource
Copy link
Contributor

Hello @RicardoLans,

We can confirm this as a False positive. I added your example in #9181 to our code base and put your issue in our backlog.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Projects
None yet
Development

No branches or pull requests

2 participants