Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't offer 'use compound operator' if pp-directives are present #63572

Merged
merged 2 commits into from
Aug 24, 2022
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 @@ -5,6 +5,7 @@
using System;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Extensions;
Expand Down Expand Up @@ -152,6 +153,19 @@ 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;
}

if (semanticModel.GetTypeInfo(testedExpression, cancellationToken).Type is IPointerTypeSymbol or IFunctionPointerTypeSymbol)
{
// pointers cannot use ??=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,135 @@ static void Main(object o)
}");
}

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

[WorkItem(63552, "https://github.com/dotnet/roslyn/issues/63552")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseCompoundAssignment)]
public async Task TestIfStatementWithPreprocessorBlock2()
{
await TestMissingAsync(
@"using System;
class C
{
static void Main(object o)
{
if (o is null)
{
#if X
Console.WriteLine(""Only run if o is null"");
#endif
o = """";
}
}
}");
}

[WorkItem(63552, "https://github.com/dotnet/roslyn/issues/63552")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseCompoundAssignment)]
public async Task TestIfStatementWithPreprocessorBlock3()
{
await TestMissingAsync(
@"using System;
class C
{
static void Main(object o)
{
if (o is null)
{
#if X
Console.WriteLine(""Only run if o is null"");
#else
o = """";
#endif
}
}
}");
}

[WorkItem(63552, "https://github.com/dotnet/roslyn/issues/63552")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseCompoundAssignment)]
public async Task TestIfStatementWithPreprocessorBlock4()
{
await TestMissingAsync(
@"using System;
class C
{
static void Main(object o)
{
if (o is null)
{
#if X
Console.WriteLine(""Only run if o is null"");
#elif true
o = """";
#endif
}
}
}");
}

[WorkItem(63552, "https://github.com/dotnet/roslyn/issues/63552")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseCompoundAssignment)]
public async Task TestIfStatementWithPreprocessorBlock5()
{
await TestMissingAsync(
@"using System;
class C
{
static void Main(object o)
{
if (o is null)
{
#if true
o = """";
#else
Console.WriteLine(""Only run if o is null"");
#endif
}
}
}");
}

[WorkItem(63552, "https://github.com/dotnet/roslyn/issues/63552")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseCompoundAssignment)]
public async Task TestIfStatementWithPreprocessorBlock6()
{
await TestMissingAsync(
@"using System;
class C
{
static void Main(object o)
{
if (o is null)
{
#if true
o = """";
#elif X
Console.WriteLine(""Only run if o is null"");
#endif
}
}
}");
}

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