Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Cryptography;
using System.Threading;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.LanguageService;
using Microsoft.CodeAnalysis.CSharp.Shared.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
Expand Down Expand Up @@ -381,12 +379,17 @@ bool AnalyzeConstructorBody(
Dictionary<ISymbol, IParameterSymbol> candidateMembersToRemove)
{
var semanticModel = compilation.GetSemanticModel(primaryConstructorDeclaration.SyntaxTree);

var body = primaryConstructorDeclaration.ExpressionBody ?? (SyntaxNode?)primaryConstructorDeclaration.Body;
if (body?.ContainsDirectives is true)
Copy link
Contributor

@mavasani mavasani Jun 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this also turn off the diagnostic in presence of pragma directives? Is that desirable? Not a big deal either way though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably. but we can see if that's a big enough negative if people have an issue with it.

return false;

return primaryConstructorDeclaration switch
{
{ ExpressionBody.Expression: AssignmentExpressionSyntax assignmentExpression }
=> IsAssignmentToInstanceMember(namedType, semanticModel, assignmentExpression, candidateMembersToRemove, out _),
{ Body: { } body }
=> AnalyzeBlockBody(namedType, semanticModel, body, candidateMembersToRemove),
{ Body: { } block }
=> AnalyzeBlockBody(namedType, semanticModel, block, candidateMembersToRemove),
_ => false,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2606,4 +2606,270 @@ class C(int i)
LanguageVersion = LanguageVersion.Preview,
}.RunAsync();
}

[Fact]
public async Task TestNotWithPreprocessorRegion1()
{
await new VerifyCS.Test
{
TestCode = """
using System;

class C
{
public C(int i)
{
#if NET6
Console.WriteLine();
#endif
}
}
""",
LanguageVersion = LanguageVersion.Preview,
}.RunAsync();
}

[Fact]
public async Task TestNotWithPreprocessorRegion2()
{
await new VerifyCS.Test
{
TestCode = """
using System;

class C
{
public C(int i)
{
#if false
Console.WriteLine();
#endif
}
}
""",
LanguageVersion = LanguageVersion.Preview,
}.RunAsync();
}

[Fact]
public async Task TestNotWithPreprocessorRegion3()
{
await new VerifyCS.Test
{
TestCode = """
using System;

class C
{
private int _i;

public C(int i)
#if NET6
=> _i = i;
#else
=> this._i = i;
#endif
}
""",
LanguageVersion = LanguageVersion.Preview,
}.RunAsync();
}

[Fact]
public async Task TestNotWithPreprocessorRegion4()
{
await new VerifyCS.Test
{
TestCode = """
using System;

class C
{
private int _i;

public C(int i)
#if true
=> _i = i;
#else
=> this._i = i;
#endif
}
""",
LanguageVersion = LanguageVersion.Preview,
}.RunAsync();
}

[Fact]
public async Task TestNotWithPreprocessorRegion5()
{
await new VerifyCS.Test
{
TestCode = """
using System;

class C
{
private int _i;

public C(int i)
#if false
=> _i = i;
#else
=> this._i = i;
#endif
}
""",
LanguageVersion = LanguageVersion.Preview,
}.RunAsync();
}

[Fact]
public async Task TestNotWithPreprocessorRegion6()
{
await new VerifyCS.Test
{
TestCode = """
using System;

class C
{
private int _i;

public C(int i)
{
#if true
_i = i;
#endif
}
}
""",
LanguageVersion = LanguageVersion.Preview,
}.RunAsync();
}

[Fact]
public async Task TestNotWithPreprocessorRegion7()
{
await new VerifyCS.Test
{
TestCode = """
using System;

class C
{
private int _i;

public C(int i)
{
#if true
_i = i;
#else
this._i = i;
#endif
}
}
""",
LanguageVersion = LanguageVersion.Preview,
}.RunAsync();
}

[Fact]
public async Task TestNotWithPreprocessorRegion8()
{
await new VerifyCS.Test
{
TestCode = """
using System;

class C
{
private int _i;

public C(int i)
{
#if false
_i = i;
#else
this._i = i;
#endif
}
}
""",
LanguageVersion = LanguageVersion.Preview,
}.RunAsync();
}

[Fact]
public async Task TestWithRegionDirective1()
{
await new VerifyCS.Test
{
TestCode = """
class C
{

#region constructors

public [|C|](int i)
{
}

#endregion

}
""",
FixedCode = """
class C(int i)
{

#region constructors

#endregion

}
""",
LanguageVersion = LanguageVersion.Preview,
}.RunAsync();
}

[Fact]
public async Task TestWithRegionDirective2()
{
await new VerifyCS.Test
{
TestCode = """
class C
{

#region constructors

public [|C|](int i)
{
}

public C(string s) : this(s.Length)
{
}

#endregion

}
""",
FixedCode = """
class C(int i)
{

#region constructors

public C(string s) : this(s.Length)
{
}

#endregion

}
""",
LanguageVersion = LanguageVersion.Preview,
}.RunAsync();
}
}