PowerShell / PowerShell/PSScriptAnalyzer

New Rule Suggestion: unreachable code,

オープン
#395 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Issue - New Rule Up-for-Grabs
主要言語
C#
スター
2.2k
フォーク
414
平均マージ
13時間 1分
マージ済み PR(30日)
2

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

issue 内の PowerShell の例を動作ケースとして使用します。break、continue、または return の後にある到達不能なステートメントは報告されるべきであり、break の後に続くラベルは宣言済みのラベルに対して検証されるべきです。制御フローとラベル処理をどこに配置すべきか判断するため、analyzer の既存のルールエントリポイントとテストを確認します。両方の誤りパターンが、誤検知なしに意図された診断を受ければ完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
powershell
領域
devtools
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。