PowerShell / PowerShell/PSScriptAnalyzer

Cmdlets that run in current scope (like ForEach-Object) not accounted for by PSUseDeclaredVarsMoreThanAssignments

Aberta
#1,163 9 comentários 13 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

Area - Engine Area - PSUseDeclaredVarsMoreThanAssignments Issue - Enhancement
Linguagem predominante
C#
Estrelas
2.2k
Forks
415
Merge médio
13h 1min
PRs com merge (30d)
2

Descrição

Although issue #1031 and #1129 probably have the same cause, I have added a new bug report as it is (afaik) not directly related to the Begin, Process and End function blocks as suggested in there which brings the issue in a different perspective.

Apparently for some cmdlets, PowerShell is invoked in the current scope but that is apparently not respected by the PSScriptAnalyzer.

Steps to reproduce

$Test = $False
1..3 | ForEach-Object {$Test = $True}
$Test

note that the result of the above is $True, meaning that the value of $Test is actually assigned and changed to $True within the ForEach-Object cmdlet.

Expected behavior

No warning.

Actual behavior

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSUseDeclaredVarsMoreThanAssignment Warning      Test.ps1   2     The variable 'Test' is assigned but never used.

Environment data

Name                           Value
----                           -----
PSVersion                      5.1.17134.590
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17134.590
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

PSScriptAnalyzer.Version:    1.17.1

Will this eventually be resolved?
How can I nicely avoid the incorrect warning in an example like above?

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Direção de pesquisa

Comece executando a reprodução no PowerShell com PSUseDeclaredVarsMoreThanAssignments e confirme o aviso para a atribuição de ForEach-Object. Rastreie o tratamento dos cmdlets do escopo atual pela regra e considere a issue concluída quando o exemplo não produzir nenhum aviso sem causar regressões nos diagnósticos comuns de atribuições não utilizadas.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
csharp, powershell
Domínio
tooling
Tipo de issue
Bug
Dificuldade
4/5
Tempo estimado
3-5 dias
Status de atividade
Estagnada
Clareza
Razoavelmente clara
Facilidade para iniciantes
45/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.