-
-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add analyzer "Use string interpolation instead of 'string.Concat'" (#…
- Loading branch information
1 parent
e96a13a
commit 774b83d
Showing
10 changed files
with
372 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 0 additions & 22 deletions
22
src/Analyzers.CodeFixes/CSharp/Refactorings/CallThenByInsteadOfOrderByRefactoring.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/Analyzers/CSharp/Analysis/UseStringInterpolationInsteadOfStringConcatAnalysis.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (c) .NET Foundation and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Roslynator.CSharp.Syntax; | ||
|
||
namespace Roslynator.CSharp.Analysis; | ||
|
||
internal static class UseStringInterpolationInsteadOfStringConcatAnalysis | ||
{ | ||
internal static void Analyze(SyntaxNodeAnalysisContext context, SimpleMemberInvocationExpressionInfo invocationInfo) | ||
{ | ||
ISymbol symbol = context.SemanticModel.GetSymbol(invocationInfo.InvocationExpression, context.CancellationToken); | ||
|
||
if (symbol?.Name == "Concat" | ||
&& symbol.IsStatic | ||
&& symbol.ContainingType.IsString()) | ||
{ | ||
bool? isVerbatim = null; | ||
|
||
foreach (ArgumentSyntax argument in invocationInfo.Arguments) | ||
{ | ||
ExpressionSyntax expression = argument.Expression; | ||
|
||
if (expression.IsKind(SyntaxKind.InterpolatedStringExpression)) | ||
return; | ||
|
||
if (expression.IsKind(SyntaxKind.StringLiteralExpression)) | ||
{ | ||
var literalExpression = (LiteralExpressionSyntax)expression; | ||
|
||
if (literalExpression.Token.Text.StartsWith("@")) | ||
{ | ||
if (isVerbatim is null) | ||
{ | ||
isVerbatim = true; | ||
} | ||
else if (isVerbatim == false) | ||
{ | ||
return; | ||
} | ||
} | ||
else if (isVerbatim is null) | ||
{ | ||
isVerbatim = false; | ||
} | ||
else if (isVerbatim == true) | ||
{ | ||
return; | ||
} | ||
} | ||
} | ||
|
||
if (isVerbatim is not null | ||
&& invocationInfo.ArgumentList.IsSingleLine()) | ||
{ | ||
DiagnosticHelpers.ReportDiagnostic(context, DiagnosticRules.UseStringInterpolationInsteadOfStringConcat, invocationInfo.InvocationExpression); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.