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

Check for the CollectionExpression syntax kind early in UseSearchValuesAnalyzer #7279

Merged
merged 3 commits into from
Apr 9, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ namespace Microsoft.NetCore.CSharp.Analyzers.Performance
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class CSharpUseSearchValuesAnalyzer : UseSearchValuesAnalyzer
{
// The referenced SDK version doesn't yet contain these SyntaxKind values
// https://github.com/dotnet/roslyn/blob/main/src/Compilers/CSharp/Portable/Syntax/SyntaxKind.cs
private const SyntaxKind Utf8StringLiteralToken = (SyntaxKind)8520;
private const SyntaxKind Utf8StringLiteralExpression = (SyntaxKind)8756;
private const SyntaxKind CollectionExpression = (SyntaxKind)9076;
MihaZupan marked this conversation as resolved.
Show resolved Hide resolved

// char[] myField = new char[] { 'a', 'b', 'c' };
// char[] myField = new[] { 'a', 'b', 'c' };
// char[] myField = "abc".ToCharArray();
Expand Down Expand Up @@ -112,7 +118,7 @@ internal static bool IsConstantByteOrCharArrayCreationExpression(SemanticModel s
return true;
}
}
else
else if (expression.IsKind(CollectionExpression))
{
return
semanticModel.GetOperation(expression) is { } operation &&
Expand Down Expand Up @@ -165,9 +171,6 @@ expression is LiteralExpressionSyntax characterLiteral &&

private static bool IsUtf8StringLiteralExpression(ExpressionSyntax expression, out int length)
{
const SyntaxKind Utf8StringLiteralExpression = (SyntaxKind)8756;
const SyntaxKind Utf8StringLiteralToken = (SyntaxKind)8520;

if (expression.IsKind(Utf8StringLiteralExpression) &&
expression is LiteralExpressionSyntax literal &&
literal.Token.IsKind(Utf8StringLiteralToken) &&
Expand Down