PowerShell / PowerShell/PSScriptAnalyzer

New Rule Suggestion: unreachable code,

Abierto
#395 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Issue - New Rule Up-for-Grabs
Lenguaje dominante
C#
Estrellas
2.2k
Forks
414
Merge medio
13 h 1 min
PR fusionados (30 d)
2

Descripción

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.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Usa los ejemplos de PowerShell del issue como casos de comportamiento: se deben notificar las instrucciones inalcanzables después de break, continue o return, y las etiquetas que siguen a break deben validarse con respecto a las etiquetas declaradas. Revisa los puntos de entrada de las reglas y las pruebas existentes del analizador para determinar dónde deben ubicarse el control de flujo y el manejo de etiquetas; el trabajo estará terminado cuando ambos patrones de error reciban el diagnóstico previsto sin falsos positivos.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
powershell
Área
devtools
Tipo de issue
Nueva funcionalidad
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.