Skip to content

Commit 24c37f6

Browse files
committed
Fix that SA1003 does not allow postfix unary expressions as last elements in an anonymous object creation or initializers
1 parent 38a33f8 commit 24c37f6

2 files changed

Lines changed: 70 additions & 2 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1003UnitTests.cs

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,24 @@ public void Bar()
5252
var v9_2 = v4[v1++];
5353
var v9_3 = $""{v1++}"";
5454
var v9_4 = v1++;
55+
var v9_5 = new
56+
{
57+
Property = v1++
58+
};
59+
var v9_6 = new
60+
{
61+
Property = v1--
62+
};
63+
var v9_7 = new int[]
64+
{
65+
v1++,
66+
v1++
67+
};
68+
var v9_8 = new int[]
69+
{
70+
v1--,
71+
v1--
72+
};
5573
}
5674
}
5775
";
@@ -84,6 +102,29 @@ public void Bar()
84102
var v6 = new[] { 1, 2, 3 };
85103
var v7 = ( (byte)v1 == 0);
86104
var v8 = v6[ ~v1];
105+
106+
var v9_1 = new
107+
{
108+
Property = v1++
109+
,
110+
};
111+
var v9_2 = new
112+
{
113+
Property = v1--
114+
,
115+
};
116+
var v9_3 = new int[]
117+
{
118+
v1++,
119+
v1++
120+
,
121+
};
122+
var v9_4 = new int[]
123+
{
124+
v1--,
125+
v1--
126+
,
127+
};
87128
}
88129
}
89130
";
@@ -106,6 +147,25 @@ public void Bar()
106147
var v6 = new[] { 1, 2, 3 };
107148
var v7 = ((byte)v1 == 0);
108149
var v8 = v6[~v1];
150+
151+
var v9_1 = new
152+
{
153+
Property = v1++,
154+
};
155+
var v9_2 = new
156+
{
157+
Property = v1--,
158+
};
159+
var v9_3 = new int[]
160+
{
161+
v1++,
162+
v1++,
163+
};
164+
var v9_4 = new int[]
165+
{
166+
v1--,
167+
v1--,
168+
};
109169
}
110170
}
111171
";
@@ -116,7 +176,11 @@ public void Bar()
116176
this.CSharpDiagnostic(DescriptorNotFollowedByComment).WithLocation(13, 18).WithArguments("~"),
117177
this.CSharpDiagnostic(DescriptorNotFollowedByComment).WithLocation(15, 18).WithArguments("~"),
118178
this.CSharpDiagnostic(DescriptorNotPrecededByWhitespace).WithLocation(17, 20).WithArguments("(byte)"),
119-
this.CSharpDiagnostic(DescriptorNotPrecededByWhitespace).WithLocation(18, 22).WithArguments("~")
179+
this.CSharpDiagnostic(DescriptorNotPrecededByWhitespace).WithLocation(18, 22).WithArguments("~"),
180+
this.CSharpDiagnostic(DescriptorNotAtEndOfLine).WithLocation(22, 26).WithArguments("++"),
181+
this.CSharpDiagnostic(DescriptorNotAtEndOfLine).WithLocation(27, 26).WithArguments("--"),
182+
this.CSharpDiagnostic(DescriptorNotAtEndOfLine).WithLocation(33, 15).WithArguments("++"),
183+
this.CSharpDiagnostic(DescriptorNotAtEndOfLine).WithLocation(39, 15).WithArguments("--")
120184
};
121185

122186
DiagnosticResult[] fixedExpected =

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1003SymbolsMustBeSpacedCorrectly.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,11 @@ private static void HandlePostfixUnaryExpression(SyntaxNodeAnalysisContext conte
238238
&& !followingToken.IsKind(SyntaxKind.CommaToken)
239239
&& !(followingToken.IsKind(SyntaxKind.CloseBraceToken) && (followingToken.Parent is InterpolationSyntax));
240240

241-
CheckToken(context, unaryExpression.OperatorToken, false, false, mustHaveTrailingWhitespace);
241+
// If the next token is a close brace token we are in an anonymous object creation or an initialization.
242+
// Then we allow a new line
243+
bool allowEndOfLine = followingToken.IsKind(SyntaxKind.CloseBraceToken);
244+
245+
CheckToken(context, unaryExpression.OperatorToken, false, allowEndOfLine, mustHaveTrailingWhitespace);
242246
}
243247

244248
private static void HandleAssignmentExpression(SyntaxNodeAnalysisContext context)

0 commit comments

Comments
 (0)