Skip to content

Commit

Permalink
Merge pull request #75779 from CyrusNajmabadi/suppressedConditional
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi authored Nov 6, 2024
2 parents 4fe8beb + 77df314 commit 4039bc3
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.UseConditionalExpression;

[Trait(Traits.Feature, Traits.Features.CodeActionsUseConditionalExpression)]
public partial class UseConditionalExpressionForReturnTests : AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest_NoEditor
public sealed class UseConditionalExpressionForReturnTests(ITestOutputHelper logger)
: AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest_NoEditor(logger)
{
private static readonly ParseOptions CSharp8 = CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp8);
private static readonly ParseOptions CSharp9 = CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp9);

public UseConditionalExpressionForReturnTests(ITestOutputHelper logger)
: base(logger)
{
}

internal override (DiagnosticAnalyzer, CodeFixProvider) CreateDiagnosticProviderAndFixer(Workspace workspace)
=> (new CSharpUseConditionalExpressionForReturnDiagnosticAnalyzer(),
new CSharpUseConditionalExpressionForReturnCodeFixProvider());
Expand Down Expand Up @@ -2281,4 +2277,35 @@ private bool AreSimilarCore(string node1, string node2)
}
""", title: AnalyzersResources.Simplify_check);
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/38879")]
public async Task TesSuppressionOperator()
{
await TestInRegularAndScript1Async("""
#nullable enable
class Program
{
public static string Method(bool empty)
{
[||]if (empty)
{
return string.Empty;
}
return null!;
}
}
""", """
#nullable enable
class Program
{
public static string Method(bool empty)
{
return empty ? string.Empty : null!;
}
}
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Formatting;
Expand Down Expand Up @@ -161,7 +160,12 @@ private TExpressionSyntax CastValueIfNecessary(
if (statement is IThrowOperation throwOperation)
return ConvertToExpression(throwOperation);

var sourceSyntax = value.Syntax.WithoutTrivia();
var suppressKind = this.SyntaxFacts.SyntaxKinds.SuppressNullableWarningExpression;
var sourceSyntax = value.Syntax;
while (sourceSyntax is { Parent.RawKind: var kind } && kind == suppressKind)
sourceSyntax = sourceSyntax.Parent;

sourceSyntax = sourceSyntax.WithoutTrivia();

// If there was an implicit conversion generated by the compiler, then convert that to an
// explicit conversion inside the condition. This is needed as there is no type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public int Convert<TSyntaxKind>(TSyntaxKind kind) where TSyntaxKind : struct
public int ReferenceEqualsExpression => (int)SyntaxKind.EqualsExpression;
public int ReferenceNotEqualsExpression => (int)SyntaxKind.NotEqualsExpression;
public int SimpleMemberAccessExpression => (int)SyntaxKind.SimpleMemberAccessExpression;
public int? SuppressNullableWarningExpression => (int)SyntaxKind.SuppressNullableWarningExpression;
public int TernaryConditionalExpression => (int)SyntaxKind.ConditionalExpression;
public int ThisExpression => (int)SyntaxKind.ThisExpression;
public int? ThrowExpression => (int)SyntaxKind.ThrowExpression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ internal interface ISyntaxKinds
int ReferenceEqualsExpression { get; }
int ReferenceNotEqualsExpression { get; }
int SimpleMemberAccessExpression { get; }
int? SuppressNullableWarningExpression { get; }
int TernaryConditionalExpression { get; }
int ThisExpression { get; }
int? ThrowExpression { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.LanguageService
Public ReadOnly Property ReferenceEqualsExpression As Integer = SyntaxKind.IsExpression Implements ISyntaxKinds.ReferenceEqualsExpression
Public ReadOnly Property ReferenceNotEqualsExpression As Integer = SyntaxKind.IsNotExpression Implements ISyntaxKinds.ReferenceNotEqualsExpression
Public ReadOnly Property SimpleMemberAccessExpression As Integer = SyntaxKind.SimpleMemberAccessExpression Implements ISyntaxKinds.SimpleMemberAccessExpression
Public ReadOnly Property SuppressNullableWarningExpression As Integer? Implements ISyntaxKinds.SuppressNullableWarningExpression
Public ReadOnly Property TernaryConditionalExpression As Integer = SyntaxKind.TernaryConditionalExpression Implements ISyntaxKinds.TernaryConditionalExpression
Public ReadOnly Property ThisExpression As Integer = SyntaxKind.MeExpression Implements ISyntaxKinds.ThisExpression
Public ReadOnly Property ThrowExpression As Integer? Implements ISyntaxKinds.ThrowExpression
Expand Down

0 comments on commit 4039bc3

Please sign in to comment.