PowerShell / PowerShell/PSScriptAnalyzer
PSUseOutputTypeCorrectly/[OutputType()] fires incorrectly, reporting that a fn should return string when the function actually return array of strings
还没有人认领这个 Issue。
- 主要语言
- C#
- 星标
- 2.2k
- 派生
- 414
- 平均合并
- 13 小时 1 分钟
- 30 天内合并 PR
- 2
描述
Before submitting a bug report:
- Make sure you are able to repro it on the latest released version
- Perform a quick search for existing issues to check if this bug has already been reported
Steps to reproduce
function Split-KeyValuePairFormatter {
[OutputType('System.String[]')]
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]
$Format,
[string]
$KeyConstituent,
[string]
$ValueConstituent,
[string]
$KeyPlaceHolder = "<%KEY%>",
[string]
$ValuePlaceHolder = "<%VALUE%>"
)
[string[]]$constituents = @();
# IMPLEMENTATION CODE OMITTED, but you can see that the return statement
# just returns the array declared as string[]
return $constituents;
}
Expected behavior
No warning expected
Actual behavior
Error:
RuleName Severity ScriptName Line Message
-------- -------- ---------- ---- -------
PSUseOutputTypeCorrectly Information split-key- 152 The cmdlet 'Split-KeyValuePairFormatter' returns an
value-pair object of type 'System.String' but this type is not
-formatter declared in the OutputType attribute.
.ps1
If I change OutputType to: [OutputType('System.String')], which is clearly wrong as it doesn't reflect the behaviour of the actual code, then there is no error. This does not seem right. As a work around, I'm having to declare an incorrect return type, ie System.string in [OutputType()], and put in a comment to indicate as such. Luckily, there is no effect on the functinality, but I can't omit the OutputType without PSScriptAnalyzer reporting a warning.
Environment data
> $PSVersionTable
Name Value
---- -----
PSVersion 7.0.0
PSEdition Core
GitCommitId 7.0.0
OS Microsoft Windows 10.0.18362
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.19.0
1.18.3
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先,使用 Split-KeyValuePairFormatter 示例复现 PSUseOutputTypeCorrectly 警告,并检查该规则对 OutputType('System.String[]') 的处理方式。将其推断出的返回类型与声明的数组类型进行比较。当该规则接受字符串数组返回值,而不要求错误地声明为 System.String 时,即表示完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- powershell
- 领域
- tooling
- Issue 类型
- 缺陷
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 42/100