Skip to content

Commit

Permalink
Merge pull request #69851 from CyrusNajmabadi/useCompoundPP
Browse files Browse the repository at this point in the history
Fix 'use compound coalesce' analyzer in the presence of pp directives
  • Loading branch information
CyrusNajmabadi authored Sep 7, 2023
2 parents 29ae80f + 8bfe17a commit e6da0af
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 19 deletions.
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

0 comments on commit e6da0af

Please sign in to comment.