Skip to content

Commit e6da0af

Browse files
Merge pull request #69851 from CyrusNajmabadi/useCompoundPP
Fix 'use compound coalesce' analyzer in the presence of pp directives
2 parents 29ae80f + 8bfe17a commit e6da0af

File tree

3 files changed

+47
-19
lines changed

3 files changed

+47
-19
lines changed

src/Analyzers/CSharp/Analyzers/UseCompoundAssignment/CSharpUseCompoundCoalesceAssignmentDiagnosticAnalyzer.cs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ private void AnalyzeIfStatement(SyntaxNodeAnalysisContext context)
132132
if (ifStatement.Else != null)
133133
return;
134134

135-
if (!GetWhenTrueAssignment(ifStatement, out _, out var assignment))
135+
if (!GetWhenTrueAssignment(ifStatement, out var whenTrue, out var assignment))
136136
return;
137137

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

156-
if (ifStatement.Statement is BlockSyntax block)
157-
{
158-
// Single is safe here as GetWhenTrueAssignment will return null if we have a block without a single
159-
// statement in it.
160-
var firstStatement = block.Statements.Single();
161-
162-
// Don't want to offer anything if our if-statement body has any conditional directives in it. That
163-
// means there's some other code that may run under some other conditions, that we do not want to now
164-
// run conditionally outside of the 'if' statement itself.
165-
if (firstStatement.GetLeadingTrivia().Any(t => t.HasStructure && t.GetStructure() is ConditionalDirectiveTriviaSyntax))
166-
return;
167-
}
156+
// Don't want to offer anything if our if-statement body has any conditional directives in it. That
157+
// means there's some other code that may run under some other conditions, that we do not want to now
158+
// run conditionally outside of the 'if' statement itself.
159+
if (whenTrue.GetLeadingTrivia().Any(static t => t.GetStructure() is ConditionalDirectiveTriviaSyntax))
160+
return;
168161

162+
// pointers cannot use ??=
169163
if (semanticModel.GetTypeInfo(testedExpression, cancellationToken).Type is IPointerTypeSymbol or IFunctionPointerTypeSymbol)
170-
{
171-
// pointers cannot use ??=
172164
return;
173-
}
174165

175166
// Good match.
176167
context.ReportDiagnostic(DiagnosticHelper.Create(

src/Analyzers/CSharp/CodeFixes/UseCompoundAssignment/CSharpUseCompoundCoalesceAssignmentCodeFixProvider.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System;
65
using System.Collections.Immutable;
76
using System.Composition;
87
using System.Diagnostics.CodeAnalysis;
@@ -13,7 +12,6 @@
1312
using Microsoft.CodeAnalysis.CodeFixes;
1413
using Microsoft.CodeAnalysis.CSharp.Extensions;
1514
using Microsoft.CodeAnalysis.CSharp.Syntax;
16-
using Microsoft.CodeAnalysis.CSharp.UseCoalesceExpression;
1715
using Microsoft.CodeAnalysis.Diagnostics;
1816
using Microsoft.CodeAnalysis.Editing;
1917
using Microsoft.CodeAnalysis.Formatting;

src/Analyzers/CSharp/Tests/UseCompoundAssignment/UseCompoundCoalesceAssignmentTests.cs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
using System.Threading.Tasks;
66
using Microsoft.CodeAnalysis.CSharp;
7-
using Microsoft.CodeAnalysis.CSharp.Shared.Extensions;
87
using Microsoft.CodeAnalysis.CSharp.UseCompoundAssignment;
98
using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions;
109
using Microsoft.CodeAnalysis.Test.Utilities;
@@ -897,6 +896,46 @@ static void Main(object o)
897896
""");
898897
}
899898

899+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/63552")]
900+
public async Task TestIfStatementWithPreprocessorBlock7()
901+
{
902+
await TestMissingAsync(
903+
"""
904+
using System;
905+
class C
906+
{
907+
static void Main(object o)
908+
{
909+
if (o is null)
910+
#if true
911+
o = "";
912+
#endif
913+
}
914+
}
915+
""");
916+
}
917+
918+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/63552")]
919+
public async Task TestIfStatementWithPreprocessorBlock8()
920+
{
921+
await TestMissingAsync(
922+
"""
923+
using System;
924+
class C
925+
{
926+
static void Main(object o)
927+
{
928+
if (o is null)
929+
#if true
930+
o = "";
931+
#else
932+
o = "";
933+
#endif
934+
}
935+
}
936+
""");
937+
}
938+
900939
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/62473")]
901940
public async Task TestPointerCannotUseCoalesceAssignment()
902941
{

0 commit comments

Comments
 (0)