Skip to content

Commit

Permalink
Disable 'use coalesce expression' when statements cross a PP boundary (
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi authored Dec 2, 2024
2 parents 8be1649 + 0cbbeb4 commit 5d2b0f4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
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

0 comments on commit 5d2b0f4

Please sign in to comment.