PowerShell / PowerShell/PSScriptAnalyzer
New Rule Suggestion: unreachable code,
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- C#
- Sterne
- 2.2k
- Forks
- 414
- Ø Merge
- 13 Std. 1 Min.
- Gemergte PRs (30 T.)
- 2
Beschreibung
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Verwende die PowerShell-Beispiele im Issue als Verhaltensfälle: Unerreichbare Anweisungen nach break, continue oder return sollten gemeldet werden, und Labels nach break sollten anhand der deklarierten Labels validiert werden. Überprüfe die vorhandenen Rule-Einstiegspunkte und Tests des Analyzers, um zu bestimmen, wo Control Flow und die Behandlung von Labels hingehören; fertig ist die Arbeit, wenn beide Fehlermuster die vorgesehenen Diagnosen ohne False Positives auslösen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- powershell
- Bereich
- devtools
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 25/100