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 @@ -132,7 +132,7 @@ private void AnalyzeIfStatement(SyntaxNodeAnalysisContext context)
if (ifStatement.Else != null)
return;

if (!GetWhenTrueAssignment(ifStatement, out _, out var assignment))
if (!GetWhenTrueAssignment(ifStatement, out var whenTrue, out var assignment))
return;

if (!IsReferenceEqualsNullCheck(semanticModel, ifStatement.Condition, cancellationToken, out var testedExpression))
Expand All @@ -153,24 +153,15 @@ private void AnalyzeIfStatement(SyntaxNodeAnalysisContext context)
return;
}

if (ifStatement.Statement is BlockSyntax block)
{
// Single is safe here as GetWhenTrueAssignment will return null if we have a block without a single
// statement in it.
var firstStatement = block.Statements.Single();

// Don't want to offer anything if our if-statement body has any conditional directives in it. That
// means there's some other code that may run under some other conditions, that we do not want to now
// run conditionally outside of the 'if' statement itself.
if (firstStatement.GetLeadingTrivia().Any(t => t.HasStructure && t.GetStructure() is ConditionalDirectiveTriviaSyntax))
return;
}
// Don't want to offer anything if our if-statement body has any conditional directives in it. That
// means there's some other code that may run under some other conditions, that we do not want to now
// run conditionally outside of the 'if' statement itself.
if (whenTrue.GetLeadingTrivia().Any(static t => t.GetStructure() is ConditionalDirectiveTriviaSyntax))
return;

// pointers cannot use ??=
if (semanticModel.GetTypeInfo(testedExpression, cancellationToken).Type is IPointerTypeSymbol or IFunctionPointerTypeSymbol)
{
// pointers cannot use ??=
return;
}

// Good match.
context.ReportDiagnostic(DiagnosticHelper.Create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Immutable;
using System.Composition;
using System.Diagnostics.CodeAnalysis;
Expand All @@ -13,7 +12,6 @@
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp.UseCoalesceExpression;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Formatting;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Shared.Extensions;
using Microsoft.CodeAnalysis.CSharp.UseCompoundAssignment;
using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions;
using Microsoft.CodeAnalysis.Test.Utilities;
Expand Down Expand Up @@ -897,6 +896,46 @@ static void Main(object o)
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/63552")]
public async Task TestIfStatementWithPreprocessorBlock7()
{
await TestMissingAsync(
"""
using System;
class C
{
static void Main(object o)
{
if (o is null)
#if true
o = "";
#endif
}
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/63552")]
public async Task TestIfStatementWithPreprocessorBlock8()
{
await TestMissingAsync(
"""
using System;
class C
{
static void Main(object o)
{
if (o is null)
#if true
o = "";
#else
o = "";
#endif
}
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/62473")]
public async Task TestPointerCannotUseCoalesceAssignment()
{
Expand Down