PowerShell / PowerShell/PSScriptAnalyzer
New Rule Suggestion: unreachable code,
Ninguém assumiu esta issue ainda.
- Linguagem predominante
- C#
- Estrelas
- 2.2k
- Forks
- 415
- Merge médio
- 13h 1min
- PRs com merge (30d)
- 2
Descrição
Normally, anything after a break or return is unreachable code.
However, it's also possible to have a label after a break, but if you make a mistake typing it, then it's just treated as "anything after a break" and therefore doesn't cause an error.
For example, see this example code full of problems:
:first foreach($i in 0..10) {
$i *= 10
Write-Verbose "Imagine we did stuff with $i"
:mid foreach($j in 0..9) {
Write-Verbose "Imagine we did stuff with $($i+$j)"
:inner foreach($k in 0..9) {
Write-Verbose "Imagine we did stuff with $($i+$j).$k"
if($k -eq $j) {
Write-Warning "Peaked at $($i+$j).$k"
break :mid # common mistake, will cause no error, acts like a break with no label
this code is unreachable
}
if($k -gt $i) {
Write-Warning "Ended at $($i+$j).$k"
break top # uncommon mistake, also no error, breaks out of all loops
}
}
}
}
return
Since this code never runs...
You could write anything that parses like a command and get no errors.
So, my suggestion:
First of all, flag unreachable code.
If there's code after a break or continue or return ... that merits a warning (or information, or something).
Most importantly, flag possible bad labels.
Any time break is followed on the same line by something that is not a comment ... that something should match the name of one of the labels, sans-punctuation.of any sort.
It's possible you could write break top without a corresponding :top label, but it's a bad idea.
Other than that, there's no valid reason why someone would exploit the inconsistency of the language parser here and use punctuation or something like break -this is a comment ...
This should be an error I think, because it's almost guaranteed to be a mistake.
Guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Direção de pesquisa
Use os exemplos de PowerShell na issue como casos comportamentais: instruções inalcançáveis após break, continue ou return devem ser reportadas, e os rótulos após break devem ser validados em relação aos rótulos declarados. Revise os pontos de entrada das regras e os testes existentes do analisador para determinar onde o controle de fluxo e o tratamento de rótulos devem ficar; o trabalho estará concluído quando os dois padrões de erro receberem o diagnóstico pretendido sem falsos positivos.
Escrita pelo modelo de indexação a partir do texto da issue.
Avaliação
- Stack de tecnologia
- powershell
- Domínio
- devtools
- Tipo de issue
- Funcionalidade
- Dificuldade
- 5/5
- Tempo estimado
- Mais de uma semana
- Status de atividade
- Estagnada
- Clareza
- Razoavelmente clara
- Facilidade para iniciantes
- 25/100