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 @@ -4,6 +4,7 @@

using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
Expand Down Expand Up @@ -102,4 +103,16 @@ protected override bool TryGetPartsOfIfStatement(

return true;
}

public override IfStatementAnalysisResult? AnalyzeIfStatement(
SemanticModel semanticModel,
IMethodSymbol? referenceEqualsMethod,
IfStatementSyntax ifStatement,
CancellationToken cancellationToken)
{
if (ifStatement.Statement.ContainsDirectives)
return null;

return base.AnalyzeIfStatement(semanticModel, referenceEqualsMethod, ifStatement, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2779,4 +2779,138 @@ public bool Equals(Element<T> x)
}
""",
languageVersion: LanguageVersion.CSharp14);

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/65880")]
public Task TestIfStatement_WithPreprocessorDirectiveInBlock()
=> TestMissingInRegularAndScriptAsync(
"""
#nullable enable
using System.Diagnostics;

class C
{
private object? _controlToLayout;
private bool _resumeLayout;
private int _layoutSuspendCount;

public void Dispose()
{
if (_controlToLayout is not null)
{
_controlToLayout.ToString();

#if DEBUG
Debug.Assert(_layoutSuspendCount == 0, "Suspend/Resume layout mismatch!");
#endif
}
}
}
""");

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/65880")]
public Task TestIfStatement_WithPreprocessorDirective_DEBUG()
=> TestMissingInRegularAndScriptAsync(
"""
#nullable enable
using System.Diagnostics;

class C
{
private object? _controlToLayout;

public void Dispose()
{
if (_controlToLayout != null)
{
_controlToLayout.ToString();

#if DEBUG
Debug.WriteLine("Debug mode");
#endif
}
}
}
""");

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/65880")]
public Task TestIfStatement_WithPreprocessorDirective_BeforeStatement()
=> TestMissingInRegularAndScriptAsync(
"""
#nullable enable
using System.Diagnostics;

class C
{
private object? _controlToLayout;

public void Dispose()
{
if (_controlToLayout != null)
{
#if DEBUG
Debug.WriteLine("Debug mode");
#endif
_controlToLayout.ToString();
}
}
}
""");

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/65880")]
public Task TestIfStatement_WithoutPreprocessorDirective_StillWorks()
=> TestInRegularAndScriptAsync(
"""
#nullable enable
using System;

class C
{
private object? _controlToLayout;

public void Dispose()
{
[|if|] (_controlToLayout != null)
{
_controlToLayout.ToString();
}
}
}
""",
"""
#nullable enable
using System;

class C
{
private object? _controlToLayout;

public void Dispose()
{
_controlToLayout?.ToString();
}
}
""");

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/65880")]
public Task TestIfStatement_SingleStatement_WithPreprocessorDirective()
=> TestMissingInRegularAndScriptAsync(
"""
#nullable enable
using System.Diagnostics;

class C
{
private object? _controlToLayout;

public void Dispose()
{
if (_controlToLayout != null)
#if DEBUG
_controlToLayout.ToString();
#else
Debug.WriteLine("null");
#endif
}
}
""");
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private void AnalyzeIfStatementAndReportDiagnostic(

}

public IfStatementAnalysisResult? AnalyzeIfStatement(
public virtual IfStatementAnalysisResult? AnalyzeIfStatement(
SemanticModel semanticModel,
IMethodSymbol? referenceEqualsMethod,
TIfStatementSyntax ifStatement,
Expand Down
Loading