Skip to content

Commit

Permalink
Fix RCS1267 (#1412)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt committed Feb 22, 2024
1 parent 9f1cea7 commit 243c71c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
4 changes: 4 additions & 0 deletions ChangeLog.md
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Fix analyzer [RCS1267](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1267) ([PR](https://github.com/dotnet/roslynator/pull/1412))

## [4.11.0] - 2024-02-19

### Added
Expand Down
Expand Up @@ -19,6 +19,7 @@ internal static void Analyze(SyntaxNodeAnalysisContext context, SimpleMemberInvo
&& symbol.ContainingType.IsString())
{
bool? isVerbatim = null;
var containsNonLiteral = false;

foreach (ArgumentSyntax argument in invocationInfo.Arguments)
{
Expand All @@ -31,7 +32,18 @@ internal static void Analyze(SyntaxNodeAnalysisContext context, SimpleMemberInvo
{
var literalExpression = (LiteralExpressionSyntax)expression;

if (literalExpression.Token.Text.StartsWith("@"))
string text = literalExpression.Token.Text;

for (int i = 0; i < text.Length; i++)
{
if (text[i] == '{'
|| text[i] == '}')
{
return;
}
}

if (text.StartsWith("@"))
{
if (isVerbatim is null)
{
Expand All @@ -51,9 +63,14 @@ internal static void Analyze(SyntaxNodeAnalysisContext context, SimpleMemberInvo
return;
}
}
else
{
containsNonLiteral = true;
}
}

if (isVerbatim is not null
&& containsNonLiteral
&& invocationInfo.ArgumentList.IsSingleLine())
{
DiagnosticHelpers.ReportDiagnostic(context, DiagnosticRules.UseStringInterpolationInsteadOfStringConcat, invocationInfo.InvocationExpression);
Expand Down
Expand Up @@ -171,4 +171,37 @@ void M()
"""
);
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseStringInterpolationInsteadOfStringConcat)]
public async Task TestNoDiagnostic_ContainsBraces()
{
await VerifyNoDiagnosticAsync("""
using System;

class C
{
void M()
{
string s1 = string.Concat(DateTime.Now, " { ", " ");
string s2 = string.Concat(DateTime.Now, " ", " } ");
}
}
""");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseStringInterpolationInsteadOfStringConcat)]
public async Task TestNoDiagnostic_LiteralExpressionsOnly()
{
await VerifyNoDiagnosticAsync("""
using System;

class C
{
void M()
{
string s = string.Concat("a", "b", "c");
}
}
""");
}
}

0 comments on commit 243c71c

Please sign in to comment.