Skip to content

Implement SeparatedSyntaxList.Count(Func<T, bool>) #78348

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

Merged
merged 2 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -6,16 +6,14 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Reflection;
using Microsoft.CodeAnalysis.CodeGen;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.PooledObjects;
using Roslyn.Utilities;
using Microsoft.CodeAnalysis;
using System.Diagnostics.CodeAnalysis;

namespace Microsoft.CodeAnalysis.CSharp.Symbols
{
Expand Down Expand Up @@ -90,7 +88,7 @@ internal static bool IsTargetEarlyAttribute(NamedTypeSymbol attributeType, Attri
Debug.Assert(!attributeType.IsErrorType());

int argumentCount = (attributeSyntax.ArgumentList != null) ?
attributeSyntax.ArgumentList.Arguments.Count<AttributeArgumentSyntax>((arg) => arg.NameEquals == null) :
attributeSyntax.ArgumentList.Arguments.Count(static (arg) => arg.NameEquals == null) :
0;
return AttributeData.IsTargetEarlyAttribute(attributeType, argumentCount, description);
}
Expand Down Expand Up @@ -138,7 +136,7 @@ internal bool IsSecurityAttribute(CSharpCompilation compilation)
{
string className = this.AttributeClass.ToDisplayString(SymbolDisplayFormat.TestFormat);

if (!this.CommonConstructorArguments.Any() & !this.CommonNamedArguments.Any())
if (this.CommonConstructorArguments.IsEmpty && this.CommonNamedArguments.IsEmpty)
{
return className;
}
Expand Down Expand Up @@ -343,7 +341,7 @@ private DeclarativeSecurityAction DecodeSecurityAttributeAction(Symbol targetSym
Debug.Assert(this.IsSecurityAttribute(compilation));

var ctorArgs = this.CommonConstructorArguments;
if (!ctorArgs.Any())
if (ctorArgs.IsEmpty)
{
// NOTE: Security custom attributes must have a valid SecurityAction as its first argument, we have none here.
// NOTE: Ideally, we should always generate 'CS7048: First argument to a security attribute must be a valid SecurityAction' for this case.
Expand All @@ -365,7 +363,7 @@ private DeclarativeSecurityAction DecodeSecurityAttributeAction(Symbol targetSym
}
else
{
TypedConstant firstArg = ctorArgs.First();
TypedConstant firstArg = ctorArgs[0];
var firstArgType = (TypeSymbol?)firstArg.TypeInternal;
if (firstArgType is object && firstArgType.Equals(compilation.GetWellKnownType(WellKnownType.System_Security_Permissions_SecurityAction)))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;

namespace Microsoft.CodeAnalysis
{
internal static class SeparatedSyntaxListExtensions
{
internal static int Count<TNode>(this SeparatedSyntaxList<TNode> list, Func<TNode, bool> predicate)
where TNode : SyntaxNode
{
int n = list.Count;
int count = 0;
for (int i = 0; i < n; i++)
{
if (predicate(list[i]))
{
count++;
}
}

return count;
}
}
}