PowerShell / PowerShell/PSScriptAnalyzer
New Rule Suggestion: unreachable code,
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- C#
- Star
- 2.2k
- Fork
- 414
- Merge trung bình
- 13 giờ 1 phút
- Pull request đã merge (30 ngày)
- 2
Mô tả
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.
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Sử dụng các ví dụ PowerShell trong issue làm các trường hợp hành vi: các câu lệnh không thể tiếp cận sau break, continue hoặc return phải được báo cáo, và các label đứng sau break phải được xác thực dựa trên các label đã khai báo. Xem xét các điểm vào rule và các bài kiểm thử hiện có của analyzer để xác định nơi xử lý control flow và label nên được đặt; hoàn thành khi cả hai mẫu lỗi đều nhận được chẩn đoán dự kiến mà không có false positive.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- powershell
- Lĩnh vực
- devtools
- Loại issue
- Tính năng
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 25/100