Skip to content

Commit

Permalink
Check for the CollectionExpression syntax kind early in UseSearchValu…
Browse files Browse the repository at this point in the history
…esAnalyzer (#7279)

* Check for the CollectionExpression syntax kind early in UseSearchValuesAnalyzer

* Document where the magic constants are coming from

* Create SyntaxKindEx helper
  • Loading branch information
MihaZupan committed Apr 9, 2024
1 parent c91db1a commit 7455f63
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using System.Collections.Generic;
using Analyzer.Utilities.Lightup;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand Down Expand Up @@ -112,7 +113,7 @@ internal static bool IsConstantByteOrCharArrayCreationExpression(SemanticModel s
return true;
}
}
else
else if (expression.IsKind(SyntaxKindEx.CollectionExpression))
{
return
semanticModel.GetOperation(expression) is { } operation &&
Expand Down Expand Up @@ -165,12 +166,9 @@ 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) &&
if (expression.IsKind(SyntaxKindEx.Utf8StringLiteralExpression) &&
expression is LiteralExpressionSyntax literal &&
literal.Token.IsKind(Utf8StringLiteralToken) &&
literal.Token.IsKind(SyntaxKindEx.Utf8StringLiteralToken) &&
literal.Token.Value is string value)
{
length = value.Length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Extensions\SyntaxNodeExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Lightup\SyntaxKindEx.cs" />
</ItemGroup>
</Project>
14 changes: 14 additions & 0 deletions src/Utilities/Compiler.CSharp/Lightup/SyntaxKindEx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using Microsoft.CodeAnalysis.CSharp;

namespace Analyzer.Utilities.Lightup
{
internal static class SyntaxKindEx
{
// https://github.com/dotnet/roslyn/blob/main/src/Compilers/CSharp/Portable/Syntax/SyntaxKind.cs
public const SyntaxKind Utf8StringLiteralToken = (SyntaxKind)8520;
public const SyntaxKind Utf8StringLiteralExpression = (SyntaxKind)8756;
public const SyntaxKind CollectionExpression = (SyntaxKind)9076;
}
}

0 comments on commit 7455f63

Please sign in to comment.