Skip to content

Commit 10c6d36

Browse files
Merge pull request #68612 from CyrusNajmabadi/usePrimaryDirectives
Fix 'use primary constructor' in the presence of directives
2 parents bb804d5 + 26d99db commit 10c6d36

File tree

2 files changed

+273
-4
lines changed

2 files changed

+273
-4
lines changed

src/Analyzers/CSharp/Analyzers/UsePrimaryConstructor/CSharpUsePrimaryConstructorDiagnosticAnalyzer.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@
1111
using System.Diagnostics;
1212
using System.Diagnostics.CodeAnalysis;
1313
using System.Linq;
14-
using System.Security.Cryptography;
1514
using System.Threading;
1615
using Microsoft.CodeAnalysis.CodeStyle;
1716
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
1817
using Microsoft.CodeAnalysis.CSharp.Extensions;
19-
using Microsoft.CodeAnalysis.CSharp.LanguageService;
2018
using Microsoft.CodeAnalysis.CSharp.Shared.Extensions;
2119
using Microsoft.CodeAnalysis.CSharp.Syntax;
2220
using Microsoft.CodeAnalysis.Diagnostics;
@@ -381,12 +379,17 @@ bool AnalyzeConstructorBody(
381379
Dictionary<ISymbol, IParameterSymbol> candidateMembersToRemove)
382380
{
383381
var semanticModel = compilation.GetSemanticModel(primaryConstructorDeclaration.SyntaxTree);
382+
383+
var body = primaryConstructorDeclaration.ExpressionBody ?? (SyntaxNode?)primaryConstructorDeclaration.Body;
384+
if (body?.ContainsDirectives is true)
385+
return false;
386+
384387
return primaryConstructorDeclaration switch
385388
{
386389
{ ExpressionBody.Expression: AssignmentExpressionSyntax assignmentExpression }
387390
=> IsAssignmentToInstanceMember(namedType, semanticModel, assignmentExpression, candidateMembersToRemove, out _),
388-
{ Body: { } body }
389-
=> AnalyzeBlockBody(namedType, semanticModel, body, candidateMembersToRemove),
391+
{ Body: { } block }
392+
=> AnalyzeBlockBody(namedType, semanticModel, block, candidateMembersToRemove),
390393
_ => false,
391394
};
392395
}

src/Analyzers/CSharp/Tests/UsePrimaryConstructor/UsePrimaryConstructorTests.cs

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2606,4 +2606,270 @@ class C(int i)
26062606
LanguageVersion = LanguageVersion.Preview,
26072607
}.RunAsync();
26082608
}
2609+
2610+
[Fact]
2611+
public async Task TestNotWithPreprocessorRegion1()
2612+
{
2613+
await new VerifyCS.Test
2614+
{
2615+
TestCode = """
2616+
using System;
2617+
2618+
class C
2619+
{
2620+
public C(int i)
2621+
{
2622+
#if NET6
2623+
Console.WriteLine();
2624+
#endif
2625+
}
2626+
}
2627+
""",
2628+
LanguageVersion = LanguageVersion.Preview,
2629+
}.RunAsync();
2630+
}
2631+
2632+
[Fact]
2633+
public async Task TestNotWithPreprocessorRegion2()
2634+
{
2635+
await new VerifyCS.Test
2636+
{
2637+
TestCode = """
2638+
using System;
2639+
2640+
class C
2641+
{
2642+
public C(int i)
2643+
{
2644+
#if false
2645+
Console.WriteLine();
2646+
#endif
2647+
}
2648+
}
2649+
""",
2650+
LanguageVersion = LanguageVersion.Preview,
2651+
}.RunAsync();
2652+
}
2653+
2654+
[Fact]
2655+
public async Task TestNotWithPreprocessorRegion3()
2656+
{
2657+
await new VerifyCS.Test
2658+
{
2659+
TestCode = """
2660+
using System;
2661+
2662+
class C
2663+
{
2664+
private int _i;
2665+
2666+
public C(int i)
2667+
#if NET6
2668+
=> _i = i;
2669+
#else
2670+
=> this._i = i;
2671+
#endif
2672+
}
2673+
""",
2674+
LanguageVersion = LanguageVersion.Preview,
2675+
}.RunAsync();
2676+
}
2677+
2678+
[Fact]
2679+
public async Task TestNotWithPreprocessorRegion4()
2680+
{
2681+
await new VerifyCS.Test
2682+
{
2683+
TestCode = """
2684+
using System;
2685+
2686+
class C
2687+
{
2688+
private int _i;
2689+
2690+
public C(int i)
2691+
#if true
2692+
=> _i = i;
2693+
#else
2694+
=> this._i = i;
2695+
#endif
2696+
}
2697+
""",
2698+
LanguageVersion = LanguageVersion.Preview,
2699+
}.RunAsync();
2700+
}
2701+
2702+
[Fact]
2703+
public async Task TestNotWithPreprocessorRegion5()
2704+
{
2705+
await new VerifyCS.Test
2706+
{
2707+
TestCode = """
2708+
using System;
2709+
2710+
class C
2711+
{
2712+
private int _i;
2713+
2714+
public C(int i)
2715+
#if false
2716+
=> _i = i;
2717+
#else
2718+
=> this._i = i;
2719+
#endif
2720+
}
2721+
""",
2722+
LanguageVersion = LanguageVersion.Preview,
2723+
}.RunAsync();
2724+
}
2725+
2726+
[Fact]
2727+
public async Task TestNotWithPreprocessorRegion6()
2728+
{
2729+
await new VerifyCS.Test
2730+
{
2731+
TestCode = """
2732+
using System;
2733+
2734+
class C
2735+
{
2736+
private int _i;
2737+
2738+
public C(int i)
2739+
{
2740+
#if true
2741+
_i = i;
2742+
#endif
2743+
}
2744+
}
2745+
""",
2746+
LanguageVersion = LanguageVersion.Preview,
2747+
}.RunAsync();
2748+
}
2749+
2750+
[Fact]
2751+
public async Task TestNotWithPreprocessorRegion7()
2752+
{
2753+
await new VerifyCS.Test
2754+
{
2755+
TestCode = """
2756+
using System;
2757+
2758+
class C
2759+
{
2760+
private int _i;
2761+
2762+
public C(int i)
2763+
{
2764+
#if true
2765+
_i = i;
2766+
#else
2767+
this._i = i;
2768+
#endif
2769+
}
2770+
}
2771+
""",
2772+
LanguageVersion = LanguageVersion.Preview,
2773+
}.RunAsync();
2774+
}
2775+
2776+
[Fact]
2777+
public async Task TestNotWithPreprocessorRegion8()
2778+
{
2779+
await new VerifyCS.Test
2780+
{
2781+
TestCode = """
2782+
using System;
2783+
2784+
class C
2785+
{
2786+
private int _i;
2787+
2788+
public C(int i)
2789+
{
2790+
#if false
2791+
_i = i;
2792+
#else
2793+
this._i = i;
2794+
#endif
2795+
}
2796+
}
2797+
""",
2798+
LanguageVersion = LanguageVersion.Preview,
2799+
}.RunAsync();
2800+
}
2801+
2802+
[Fact]
2803+
public async Task TestWithRegionDirective1()
2804+
{
2805+
await new VerifyCS.Test
2806+
{
2807+
TestCode = """
2808+
class C
2809+
{
2810+
2811+
#region constructors
2812+
2813+
public [|C|](int i)
2814+
{
2815+
}
2816+
2817+
#endregion
2818+
2819+
}
2820+
""",
2821+
FixedCode = """
2822+
class C(int i)
2823+
{
2824+
2825+
#region constructors
2826+
2827+
#endregion
2828+
2829+
}
2830+
""",
2831+
LanguageVersion = LanguageVersion.Preview,
2832+
}.RunAsync();
2833+
}
2834+
2835+
[Fact]
2836+
public async Task TestWithRegionDirective2()
2837+
{
2838+
await new VerifyCS.Test
2839+
{
2840+
TestCode = """
2841+
class C
2842+
{
2843+
2844+
#region constructors
2845+
2846+
public [|C|](int i)
2847+
{
2848+
}
2849+
2850+
public C(string s) : this(s.Length)
2851+
{
2852+
}
2853+
2854+
#endregion
2855+
2856+
}
2857+
""",
2858+
FixedCode = """
2859+
class C(int i)
2860+
{
2861+
2862+
#region constructors
2863+
2864+
public C(string s) : this(s.Length)
2865+
{
2866+
}
2867+
2868+
#endregion
2869+
2870+
}
2871+
""",
2872+
LanguageVersion = LanguageVersion.Preview,
2873+
}.RunAsync();
2874+
}
26092875
}

0 commit comments

Comments
 (0)