PowerShell / PowerShell/PSScriptAnalyzer
New Rule Suggestion: unreachable code,
Nessuno ha ancora preso questa issue.
- Lingua principale
- C#
- Stelle
- 2.2k
- Fork
- 414
- Merge medio
- 13h 1m
- PR unite (30g)
- 2
Descrizione
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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Usa gli esempi PowerShell nell'issue come casi comportamentali: le istruzioni irraggiungibili dopo break, continue o return devono essere segnalate, e le label che seguono break devono essere convalidate rispetto alle label dichiarate. Esamina i punti di ingresso delle regole e i test esistenti dell'analizzatore per determinare dove collocare il controllo del flusso e la gestione delle label; il lavoro è completato quando entrambi i pattern di errore ricevono la diagnostica prevista senza falsi positivi.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- powershell
- Ambito
- devtools
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100