GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Optimise LINQ queries by bergmeister · Pull Request #2160 · PowerShell/PSScriptAnalyzer

Conversation

@bergmeister

@bergmeister Christoph Bergmeister (bergmeister) commented Mar 6, 2026

Copy link
Copy Markdown
Collaborator

PR Summary

NOTE: This was created using GH Copilot agent when being asked for optimizations. They seem sensible. Not sure if we will see the benefit of it as many LINQ methods have internal optimisation to use Count property if available but having code the right way seems better practice. Below is Copilot's summary:

Replaced inefficient LINQ patterns with performant alternatives across core analysis paths.

Changes

List count checks (ScriptAnalyzer.cs:274-276)

  • List<T>.Count() → List<T>.Count property access

Enumerable emptiness checks (ScriptAnalyzer.cs:616, RuleSuppression.cs:345, Settings.cs:456)

  • IEnumerable<T>.Count() == 0 → !IEnumerable<T>.Any()

Filtered counting (AvoidMultipleTypeAttributes.cs:40)

  • .Where(predicate).Count() → .Count(predicate)
// Before
if (paramAst.Attributes.Where(typeAst => typeAst is TypeConstraintAst).Count() > 1)

// After  
if (paramAst.Attributes.Count(typeAst => typeAst is TypeConstraintAst) > 1)

Impact: Eliminates unnecessary enumerations in settings parsing and per-rule execution paths. Any() short-circuits on first match; Count(predicate) eliminates intermediate allocations.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • 0t3vsblobprodcus362.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet build --framework net8 --configuration PSV7Debug (dns block)
    • Triggering command: /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet restore --no-dependencies /home/REDACTED/work/PSScriptAnalyzer/PSScriptAnalyzer/PSScriptAnalyzer.sln --packages /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
    • Triggering command: /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet restore --no-dependencies /home/REDACTED/work/PSScriptAnalyzer/PSScriptAnalyzer/PSCompatibilityCollector/Microsoft.PowerShell.CrossCompatibility/Microsoft.PowerShell.CrossCompatibility.csproj --packages /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
  • 1k9vsblobprodcus379.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet build --framework net8 --configuration PSV7Debug (dns block)
  • 4myvsblobprodcus32.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet build --framework net8 --configuration PSV7Debug (dns block)
    • Triggering command: /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet restore --no-dependencies /home/REDACTED/work/PSScriptAnalyzer/PSScriptAnalyzer/PSScriptAnalyzer.sln --packages /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
    • Triggering command: /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet restore --no-dependencies /home/REDACTED/work/PSScriptAnalyzer/PSScriptAnalyzer/PSCompatibilityCollector/Microsoft.PowerShell.CrossCompatibility/Microsoft.PowerShell.CrossCompatibility.csproj --packages /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
  • 5dkvsblobprodcus355.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet build --framework net8 --configuration PSV7Debug (dns block)
    • Triggering command: /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet restore --no-dependencies /home/REDACTED/work/PSScriptAnalyzer/PSScriptAnalyzer/PSScriptAnalyzer.sln --packages /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
    • Triggering command: /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet restore --no-dependencies /home/REDACTED/work/PSScriptAnalyzer/PSScriptAnalyzer/Engine/Engine.csproj --packages /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
  • 7tjvsblobprodcus341.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet build --framework net8 --configuration PSV7Debug (dns block)
  • c78vsblobprodcus322.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet build --framework net8 --configuration PSV7Debug (dns block)
  • jd4vsblobprodcus366.vsblob.vsassets.io
    • Triggering command: /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet restore --no-dependencies /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/F1242452CC06D4F8E99F297E498B3485/missingpackages_workingdir --packages /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/missingpackages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal --configfile /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/nugetconfig/nuget.config --force (dns block)
  • josvsblobprodcus372.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet build --framework net8 --configuration PSV7Debug (dns block)
    • Triggering command: /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet restore --no-dependencies /home/REDACTED/work/PSScriptAnalyzer/PSScriptAnalyzer/PSScriptAnalyzer.sln --packages /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
    • Triggering command: /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet restore --no-dependencies /home/REDACTED/work/PSScriptAnalyzer/PSScriptAnalyzer/PSCompatibilityCollector/Microsoft.PowerShell.CrossCompatibility/Microsoft.PowerShell.CrossCompatibility.csproj --packages /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
  • jrqvsblobprodcus343.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet build --framework net8 --configuration PSV7Debug (dns block)
    • Triggering command: /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet restore --no-dependencies /home/REDACTED/work/PSScriptAnalyzer/PSScriptAnalyzer/PSScriptAnalyzer.sln --packages /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
    • Triggering command: /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet restore --no-dependencies /home/REDACTED/work/PSScriptAnalyzer/PSScriptAnalyzer/Rules/Rules.csproj --packages /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
  • kh4vsblobprodcus325.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet build --framework net8 --configuration PSV7Debug (dns block)
  • l49vsblobprodcus358.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet build --framework net8 --configuration PSV7Debug (dns block)
  • m6xvsblobprodcus342.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet build --framework net8 --configuration PSV7Debug (dns block)
  • p2ovsblobprodcus312.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet build --framework net8 --configuration PSV7Debug (dns block)
    • Triggering command: /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet restore --no-dependencies /home/REDACTED/work/PSScriptAnalyzer/PSScriptAnalyzer/PSScriptAnalyzer.sln --packages /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
    • Triggering command: /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet restore --no-dependencies /home/REDACTED/work/PSScriptAnalyzer/PSScriptAnalyzer/PSCompatibilityCollector/Microsoft.PowerShell.CrossCompatibility/Microsoft.PowerShell.CrossCompatibility.csproj --packages /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
  • rcxvsblobprodcus328.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet build --framework net8 --configuration PSV7Debug (dns block)
    • Triggering command: /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet restore --no-dependencies /home/REDACTED/work/PSScriptAnalyzer/PSScriptAnalyzer/PSScriptAnalyzer.sln --packages /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
    • Triggering command: /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet restore --no-dependencies /home/REDACTED/work/PSScriptAnalyzer/PSScriptAnalyzer/PSCompatibilityCollector/Microsoft.PowerShell.CrossCompatibility/Microsoft.PowerShell.CrossCompatibility.csproj --packages /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
  • sqdvsblobprodcus333.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet build --framework net8 --configuration PSV7Debug (dns block)
    • Triggering command: /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet restore --no-dependencies /home/REDACTED/work/PSScriptAnalyzer/PSScriptAnalyzer/PSScriptAnalyzer.sln --packages /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
    • Triggering command: /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet restore --no-dependencies /home/REDACTED/work/PSScriptAnalyzer/PSScriptAnalyzer/PSCompatibilityCollector/Microsoft.PowerShell.CrossCompatibility/Microsoft.PowerShell.CrossCompatibility.csproj --packages /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
  • tphvsblobprodcus375.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet build --framework net8 --configuration PSV7Debug (dns block)
    • Triggering command: /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet restore --no-dependencies /home/REDACTED/work/PSScriptAnalyzer/PSScriptAnalyzer/PSScriptAnalyzer.sln --packages /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
    • Triggering command: /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet restore --no-dependencies /home/REDACTED/work/PSScriptAnalyzer/PSScriptAnalyzer/PSCompatibilityCollector/Microsoft.PowerShell.CrossCompatibility/Microsoft.PowerShell.CrossCompatibility.csproj --packages /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
  • u6ovsblobprodcus377.vsblob.vsassets.io
    • Triggering command: /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet restore --no-dependencies /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/F1242452CC06D4F8E99F297E498B3485/missingpackages_workingdir --packages /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/missingpackages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal --configfile /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/nugetconfig/nuget.config --force (dns block)
  • uy6vsblobprodcus34.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet build --framework net8 --configuration PSV7Debug (dns block)
  • v53vsblobprodcus320.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet build --framework net8 --configuration PSV7Debug (dns block)
    • Triggering command: /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet restore --no-dependencies /home/REDACTED/work/PSScriptAnalyzer/PSScriptAnalyzer/PSScriptAnalyzer.sln --packages /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
    • Triggering command: /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/.dotnet/dotnet restore --no-dependencies /home/REDACTED/work/PSScriptAnalyzer/PSScriptAnalyzer/PSCompatibilityCollector/Microsoft.PowerShell.CrossCompatibility/Microsoft.PowerShell.CrossCompatibility.csproj --packages /home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/home/REDACTED/work/PSScriptAnalyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
  • www.powershellgallery.com
    • Triggering command: /usr/bin/pwsh pwsh -Command ./build.ps1 /rg (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

  • Configure Actions setup steps to set up my environment, which run before the firewall is enabled
  • Add the appropriate URLs or hosts to the custom allowlist in this repository's Copilot coding agent settings (admins only)

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

PR Checklist

  • PR has a meaningful title
    • Use the present tense and imperative mood when describing your changes
  • Summarized changes
  • Change is not breaking
  • Make sure all .cs, .ps1 and .psm1 files have the correct copyright header
  • Make sure you've added a new test if existing tests do not effectively test the code changed and/or updated documentation
  • This PR is ready to merge and is not Work in Progress.
    • If the PR is work in progress, please add the prefix WIP: to the beginning of the title and remove the prefix when the PR is ready.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Copilot AI and others added 2 commits February 17, 2026 10:06
Initial plan
d6f0b3e
@bergmeister
Optimize LINQ operations for better performance
a02b5bb
Co-authored-by: bergmeister <[email protected]>
@bergmeister Christoph Bergmeister (bergmeister) added the Area - Build label Mar 6, 2026
@bergmeister
Merge branch 'main' into copilot/find-optimisations
5275279
@bergmeister
Christoph Bergmeister (bergmeister) marked this pull request as ready for review March 6, 2026 17:47
@bergmeister
Christoph Bergmeister (bergmeister) requested a review from a team as a code owner March 6, 2026 17:47
Copilot AI review requested due to automatic review settings March 6, 2026 17:47
Copilot started reviewing on behalf of Christoph Bergmeister (bergmeister) March 6, 2026 17:48
View session
Copilot AI reviewed Mar 6, 2026
View reviewed changes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refines several LINQ usage patterns in the ScriptAnalyzer engine and built-in rules to avoid unnecessary enumerations and iterator allocations in common execution paths.

Changes:

  • Replaced Count() == 0 emptiness checks with !Any() for IEnumerable<T> sequences.
  • Replaced Where(predicate).Count() with Count(predicate) to avoid intermediate iterators.
  • Replaced List<T>.Count() calls with List<T>.Count property access.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
Rules/AvoidMultipleTypeAttributes.cs Uses Count(predicate) instead of Where(...).Count() when checking type-constraint attribute count.
Engine/Settings.cs Uses Any() for hashtable-AST presence checks while parsing settings files.
Engine/ScriptAnalyzer.cs Uses List<T>.Count properties and switches hashtable-AST emptiness checks to Any().
Engine/Generic/RuleSuppression.cs Uses Any() for suppression target resolution emptiness checks.
Comments suppressed due to low confidence (3)

Engine/Settings.cs:461

  • This still enumerates hashTableAsts twice (Any() then First()), which can be expensive since FindAll(...) may involve a traversal. Consider using FirstOrDefault() once and null-checking the result to avoid multiple enumerations (see similar pattern in Rules/AvoidUsingDeprecatedManifestFields.cs).
            IEnumerable<Ast> hashTableAsts = profileAst.FindAll(item => item is HashtableAst, false);

            // no hashtable, raise warning
            if (!hashTableAsts.Any())
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.InvalidProfile, settingsFilePath));
            }

            HashtableAst hashTableAst = hashTableAsts.First() as HashtableAst;

Engine/ScriptAnalyzer.cs:621

  • This emptiness check uses Any() but the code then enumerates hashTableAsts again via First(). To keep the optimization end-to-end, consider replacing the Any()/First() pair with a single FirstOrDefault() and checking for null before proceeding.
                IEnumerable<Ast> hashTableAsts = profileAst.FindAll(item => item is HashtableAst, false);

                // no hashtable, raise warning
                if (!hashTableAsts.Any())
                {
                    writer.WriteError(new ErrorRecord(new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.InvalidProfile, profile)),
                        Strings.ConfigurationFileHasNoHashTable, ErrorCategory.ResourceUnavailable, profile));
                    hasError = true;
                }
                else
                {
                    HashtableAst hashTableAst = hashTableAsts.First() as HashtableAst;

Engine/Generic/RuleSuppression.cs:361

  • targetAsts is enumerated twice here (Any() and then the foreach). If FindAll(...) returns a non-materialized enumerable, this causes repeated traversal. Consider materializing once (e.g., to a list) or restructuring to avoid a separate emptiness check before iterating.
                        if (!targetAsts.Any())
                        {
                            if (String.IsNullOrWhiteSpace(scopeAst.Extent.File))
                            {
                                ruleSupp.Error = String.Format(CultureInfo.CurrentCulture, Strings.RuleSuppressionErrorFormatScriptDefinition, ruleSupp.StartAttributeLine,
                                    String.Format(Strings.TargetCannotBeFoundError, ruleSupp.Target, ruleSupp.Scope));
                            }
                            else
                            {
                                ruleSupp.Error = String.Format(CultureInfo.CurrentCulture, Strings.RuleSuppressionErrorFormat, ruleSupp.StartAttributeLine,
                                    System.IO.Path.GetFileName(scopeAst.Extent.File), String.Format(Strings.TargetCannotBeFoundError, ruleSupp.Target, ruleSupp.Scope));
                            }

                            result.Add(ruleSupp);
                            continue;
                        }

                        foreach (Ast targetAst in targetAsts)
                        {

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Rules/AvoidMultipleTypeAttributes.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Christoph Bergmeister (bergmeister) and others added 2 commits March 6, 2026 18:01
@bergmeister
Update Rules/AvoidMultipleTypeAttributes.cs
2ccec00
Co-authored-by: Copilot <[email protected]>
@bergmeister
Update Rules/AvoidMultipleTypeAttributes.cs
3214bc3
Co-authored-by: Copilot <[email protected]>
@bergmeister
Christoph Bergmeister (bergmeister) requested a review from Andy Jordan (andyleejordan) March 6, 2026 18:06
andyleejordan
Andy Jordan (andyleejordan) approved these changes Mar 18, 2026
View reviewed changes

@andyleejordan Andy Jordan (andyleejordan) left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol it's doing it how I'd do it.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@andyleejordan
Andy Jordan (andyleejordan) merged commit 474e28b into PowerShell:main Mar 18, 2026
4 checks passed

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

Copilot code review Copilot
Copilot review effort
Applies to this pull request for everyone.Learn more about Copilot code review.
Copilot left review comments
+1 more reviewer
@andyleejordan Andy Jordan (andyleejordan) andyleejordan approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

Area - Build

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@bergmeister @andyleejordan
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.