PowerShell / PowerShell/PSScriptAnalyzer

PSUseDeclaredVarsMoreThanAssignment not correctly handling global vars assigned to within functions

Aberta
#698 0 comentários 1 reação 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

Area - Rules Issue - Bug
Linguagem predominante
C#
Estrelas
2.2k
Forks
414
Merge médio
13h 1min
PRs com merge (30d)
2

Descrição

This rule does not correctly take into account global variables if they are assigned to within a function.

If I set a global variable and then use it later, I still see a warning that the global variable was assigned to, but never used.

Given the following example:

function InitVars()
{
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidGlobalVars", "", Justification="We need a global foo")]
    param()

    $global:globalFoo = $false
    $script:scriptFoo = $false
}

function UseVars()
{
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidGlobalVars", "", Justification="We need a global foo")]
    param()

    if ($global:globalFoo)
    {
        Write-Information "`$global:globalFoo is true"
    }

    if ($scriptFoo)
    {
        Write-Information "`$script:scriptFoo is true"
    }
}

I get the following warning:

  • The variable 'globalFoo' is assigned but never used. (line 6)

There are scenarios where using global variables is still necessary, and so in those few instances I am suppressing PSAvoidGlobalVars. To do that, I have to do the assignment within a method.

As you can see above, it hits with global scoped variables, but not with script scoped variables.

This issue doesn't happen if I leave the global assignment in the root of the file (as opposed to within InitVars, but then I can't use SuppressMessageAttribute.

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 reproduzindo o exemplo fornecido de InitVars e UseVars e, em seguida, acompanhe a regra PSUseDeclaredVarsMoreThanAssignment e seus testes existentes. Considera-se concluído quando a atribuição global dentro de uma função é reconhecida como usada ao ser referenciada posteriormente, enquanto o comportamento atual com escopo de script permanece inalterado.

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
3/5
Tempo estimado
1-2 dias
Status de atividade
Estagnada
Clareza
Razoavelmente clara
Facilidade para iniciantes
42/100

Receba novas issues na sua caixa de entrada

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