PowerShell / PowerShell/PSScriptAnalyzer

New Rule Suggestion: unreachable code,

未關閉
#395 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

Issue - New Rule Up-for-Grabs
主要語言
C#
星號
2.2k
分支
415
平均合併
13 小時 1 分鐘
30 天內合併 PR
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. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

使用 issue 中的 PowerShell 範例作為行為案例:應回報 break、continue 或 return 之後的不可達陳述式,並根據已宣告的標籤驗證 break 之後的標籤。檢查 analyzer 現有的規則進入點和測試,以判斷控制流程和標籤處理應歸屬於何處;當兩種錯誤模式都能在沒有誤報的情況下產生預期診斷時,即表示完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
powershell
領域
devtools
Issue 類型
功能
難度
5/5
預估耗時
一週以上
活躍度
停滯
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。