Skip to content

Commit faf4886

Browse files
committed
Fix FixPSUseDeclaredVarsMoreThanAssignments to also detect variables that are strongly typed.
1 parent 2056fe0 commit faf4886

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

Rules/UseDeclaredVarsMoreThanAssignments.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,17 @@ private IEnumerable<DiagnosticRecord> AnalyzeScriptBlockAst(ScriptBlockAst scrip
135135
// Only checks for the case where lhs is a variable. Ignore things like $foo.property
136136
VariableExpressionAst assignmentVarAst = assignmentAst.Left as VariableExpressionAst;
137137

138+
if (assignmentVarAst == null)
139+
{
140+
// If the variable is declared in a strongly typed way, e.g. [string]$s = 'foo' then the type is ConvertExpressionAst.
141+
// Therefore we need to the VariableExpressionAst from its Child property.
142+
var assignmentVarAstAsConvertExpressionAst = assignmentAst.Left as ConvertExpressionAst;
143+
if (assignmentVarAstAsConvertExpressionAst != null && assignmentVarAstAsConvertExpressionAst.Child != null)
144+
{
145+
assignmentVarAst = assignmentVarAstAsConvertExpressionAst.Child as VariableExpressionAst;
146+
}
147+
}
148+
138149
if (assignmentVarAst != null)
139150
{
140151
// Ignore if variable is global or environment variable or scope is function

0 commit comments

Comments
 (0)