File tree Expand file tree Collapse file tree
StyleCop.Analyzers.Test.CSharp9/ReadabilityRules Expand file tree Collapse file tree Original file line number Diff line number Diff line change 33
44namespace StyleCop . Analyzers . Test . CSharp9 . ReadabilityRules
55{
6+ using System . Threading ;
7+ using System . Threading . Tasks ;
8+ using Microsoft . CodeAnalysis . CSharp ;
69 using StyleCop . Analyzers . Test . CSharp8 . ReadabilityRules ;
10+ using StyleCop . Analyzers . Test . Verifiers ;
11+ using Xunit ;
12+ using static StyleCop . Analyzers . Test . Verifiers . StyleCopCodeFixVerifier <
13+ StyleCop . Analyzers . ReadabilityRules . SA1101PrefixLocalCallsWithThis ,
14+ StyleCop . Analyzers . ReadabilityRules . SA1101CodeFixProvider > ;
715
816 public class SA1101CSharp9UnitTests : SA1101CSharp8UnitTests
917 {
18+ [ Fact ]
19+ [ WorkItem ( 3201 , "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3201" ) ]
20+ public async Task TestRecordWithExpressionAsync ( )
21+ {
22+ var testCode = @"public class Test
23+ {
24+ public record A
25+ {
26+ public string Prop { get; init; }
27+ }
28+
29+ public A UpdateA(A value)
30+ {
31+ return value with { Prop = ""newValue"" };
32+ }
33+ }" ;
34+
35+ await new CSharpTest ( LanguageVersion . CSharp9 )
36+ {
37+ ReferenceAssemblies = GenericAnalyzerTest . ReferenceAssembliesNet50 ,
38+ TestCode = testCode ,
39+ } . RunAsync ( CancellationToken . None ) . ConfigureAwait ( false ) ;
40+ }
1041 }
1142}
Original file line number Diff line number Diff line change @@ -49,5 +49,6 @@ internal static class SyntaxKindEx
4949 public const SyntaxKind ImplicitStackAllocArrayCreationExpression = ( SyntaxKind ) 9053 ;
5050 public const SyntaxKind SuppressNullableWarningExpression = ( SyntaxKind ) 9054 ;
5151 public const SyntaxKind NullableDirectiveTrivia = ( SyntaxKind ) 9055 ;
52+ public const SyntaxKind WithInitializerExpression = ( SyntaxKind ) 9062 ;
5253 }
5354}
Original file line number Diff line number Diff line change @@ -96,13 +96,23 @@ private static void HandleSimpleName(SyntaxNodeAnalysisContext context)
9696 return ;
9797
9898 case SyntaxKind . SimpleAssignmentExpression :
99- if ( ( ( AssignmentExpressionSyntax ) context . Node . Parent ) . Left == context . Node
100- && ( context . Node . Parent . Parent ? . IsKind ( SyntaxKind . ObjectInitializerExpression ) ?? true ) )
99+ if ( ( ( AssignmentExpressionSyntax ) context . Node . Parent ) . Left == context . Node )
101100 {
102- /* Handle 'X' in:
103- * new TypeName() { X = 3 }
104- */
105- return ;
101+ if ( context . Node . Parent . Parent . IsKind ( SyntaxKind . ObjectInitializerExpression ) )
102+ {
103+ /* Handle 'X' in:
104+ * new TypeName() { X = 3 }
105+ */
106+ return ;
107+ }
108+
109+ if ( context . Node . Parent . Parent . IsKind ( SyntaxKindEx . WithInitializerExpression ) )
110+ {
111+ /* Handle 'X' in:
112+ * value with { X = 3 }
113+ */
114+ return ;
115+ }
106116 }
107117
108118 break ;
You can’t perform that action at this time.
0 commit comments