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

Disable 'use coalesce expression' when statements cross a PP boundary #76173

Merged
merged 1 commit into from
Dec 2, 2024
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,8 +5,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis.Analyzers.UseCoalesceExpression;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.LanguageService;
Expand All @@ -17,7 +15,7 @@
namespace Microsoft.CodeAnalysis.CSharp.Analyzers.UseCoalesceExpression;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal class CSharpUseCoalesceExpressionForIfNullStatementCheckDiagnosticAnalyzer :
internal sealed class CSharpUseCoalesceExpressionForIfNullStatementCheckDiagnosticAnalyzer :
AbstractUseCoalesceExpressionForIfNullStatementCheckDiagnosticAnalyzer<
SyntaxKind,
ExpressionSyntax,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Microsoft.CodeAnalysis.CSharp.Analyzers.UnitTests.UseCoalesceExpressio
CSharpUseCoalesceExpressionForIfNullStatementCheckCodeFixProvider>;

[Trait(Traits.Feature, Traits.Features.CodeActionsUseCoalesceExpression)]
public class UseCoalesceExpressionForIfNullStatementCheckTests
public sealed class UseCoalesceExpressionForIfNullStatementCheckTests
{
[Fact]
public async Task TestLocalDeclaration_ThrowStatement()
Expand Down Expand Up @@ -680,4 +680,29 @@ class D : I
"""
}.RunAsync();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/70514")]
public async Task TestNotAcrossPreprocessorRegion()
{
await new VerifyCS.Test
{
TestCode = """
#define DEBUG

class C
{
void M()
{
var item = FindItem() as C;
#if DEBUG
if (item == null)
throw new System.InvalidOperationException();
#endif
}

object FindItem() => null;
}
""",
}.RunAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
// 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.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.LanguageService;
Expand All @@ -20,22 +15,18 @@ internal abstract class AbstractUseCoalesceExpressionForIfNullStatementCheckDiag
TExpressionSyntax,
TStatementSyntax,
TVariableDeclarator,
TIfStatementSyntax> : AbstractBuiltInCodeStyleDiagnosticAnalyzer
TIfStatementSyntax>() : AbstractBuiltInCodeStyleDiagnosticAnalyzer(
IDEDiagnosticIds.UseCoalesceExpressionForIfNullCheckDiagnosticId,
EnforceOnBuildValues.UseCoalesceExpression,
CodeStyleOptions2.PreferCoalesceExpression,
new LocalizableResourceString(nameof(AnalyzersResources.Use_coalesce_expression), AnalyzersResources.ResourceManager, typeof(AnalyzersResources)),
new LocalizableResourceString(nameof(AnalyzersResources.Null_check_can_be_simplified), AnalyzersResources.ResourceManager, typeof(AnalyzersResources)))
where TSyntaxKind : struct
where TExpressionSyntax : SyntaxNode
where TStatementSyntax : SyntaxNode
where TVariableDeclarator : SyntaxNode
where TIfStatementSyntax : TStatementSyntax
{
protected AbstractUseCoalesceExpressionForIfNullStatementCheckDiagnosticAnalyzer()
: base(IDEDiagnosticIds.UseCoalesceExpressionForIfNullCheckDiagnosticId,
EnforceOnBuildValues.UseCoalesceExpression,
CodeStyleOptions2.PreferCoalesceExpression,
new LocalizableResourceString(nameof(AnalyzersResources.Use_coalesce_expression), AnalyzersResources.ResourceManager, typeof(AnalyzersResources)),
new LocalizableResourceString(nameof(AnalyzersResources.Null_check_can_be_simplified), AnalyzersResources.ResourceManager, typeof(AnalyzersResources)))
{
}

protected abstract TSyntaxKind IfStatementKind { get; }
protected abstract ISyntaxFacts SyntaxFacts { get; }

Expand Down Expand Up @@ -91,6 +82,9 @@ private void AnalyzeSyntax(SyntaxNodeAnalysisContext context)
return;
}

if (syntaxFacts.ContainsInterleavedDirective([previousStatement, ifStatement], cancellationToken))
return;

TExpressionSyntax? expressionToCoalesce;

if (syntaxFacts.IsLocalDeclarationStatement(previousStatement))
Expand Down Expand Up @@ -120,12 +114,13 @@ private void AnalyzeSyntax(SyntaxNodeAnalysisContext context)
ifStatement.GetFirstToken().GetLocation(),
option.Notification,
context.Options,
ImmutableArray.Create(
expressionToCoalesce.GetLocation(),
[expressionToCoalesce.GetLocation(),
ifStatement.GetLocation(),
whenTrueStatement.GetLocation()),
whenTrueStatement.GetLocation()],
properties: null));

return;

bool AnalyzeLocalDeclarationForm(
TStatementSyntax localDeclarationStatement,
[NotNullWhen(true)] out TExpressionSyntax? expressionToCoalesce)
Expand Down
Loading