Skip to content
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 @@ -821,6 +821,26 @@ IEnumerable<bool> M(int a)
{
yield return a != 0;
}
}");
}

[WorkItem(36117, "https://github.com/dotnet/roslyn/issues/36117")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseConditionalExpression)]
public async Task TestMissingWhenCrossingPreprocessorDirective()
{
await TestMissingInRegularAndScriptAsync(
@"
class C
{
int M()
{
bool check = true;
#if true
[||]if (check)
return 3;
#endif
return 2;
}
}");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.UseConditionalExpr
Public Async Function TestOnSimpleReturn() As Task
Await TestInRegularAndScriptAsync(
"
class
class C
function M() as integer
[||]if true
return 0
Expand All @@ -28,7 +28,7 @@ class
end function
end class",
"
class
class C
function M() as integer
Return If(true, 0, 1)
end function
Expand All @@ -39,7 +39,7 @@ end class")
Public Async Function TestOnSimpleReturnNoBlocks() As Task
Await TestInRegularAndScriptAsync(
"
class
class C
function M() as integer
[||]if true
return 0
Expand All @@ -49,7 +49,7 @@ class
end function
end class",
"
class
class C
function M() as integer
Return If(true, 0, 1)
end function
Expand All @@ -60,7 +60,7 @@ end class")
Public Async Function TestOnSimpleReturnNoBlocks_NotInBlock() As Task
Await TestInRegularAndScriptAsync(
"
class
class C
function M() as integer
if true
[||]if true
Expand All @@ -72,7 +72,7 @@ class
end function
end class",
"
class
class C
function M() as integer
if true
Return If(true, 0, 1)
Expand All @@ -85,7 +85,7 @@ end class")
Public Async Function TestMissingReturnValue1() As Task
Await TestMissingInRegularAndScriptAsync(
"
class
class C
function M() as integer
[||]if true
return 0
Expand All @@ -100,7 +100,7 @@ end class")
Public Async Function TestMissingReturnValue2() As Task
Await TestMissingInRegularAndScriptAsync(
"
class
class C
function M() as integer
[||]if true
return
Expand All @@ -115,7 +115,7 @@ end class")
Public Async Function TestMissingReturnValue3() As Task
Await TestMissingInRegularAndScriptAsync(
"
class
class C
function M() as integer
[||]if true
return
Expand All @@ -130,7 +130,7 @@ end class")
Public Async Function TestWithNoElseBlockButFollowingReturn() As Task
Await TestInRegularAndScriptAsync(
"
class
class C
function M() as integer
[||]if true
return 0
Expand All @@ -140,7 +140,7 @@ class
end function
end class",
"
class
class C
function M() as integer
Return If(true, 0, 1)
end function
Expand All @@ -151,7 +151,7 @@ end class")
Public Async Function TestMissingWithoutElse() As Task
Await TestMissingInRegularAndScriptAsync(
"
class
class C
function M() as integer
[||]if true
return 0
Expand All @@ -164,7 +164,7 @@ end class")
Public Async Function TestConversion1() As Task
Await TestInRegularAndScriptAsync(
"
class
class C
function M() as object
[||]if true
return ""a""
Expand All @@ -174,7 +174,7 @@ class
end function
end class",
"
class
class C
function M() as object
Return If(true, ""a"", ""b"")
end function
Expand All @@ -185,7 +185,7 @@ end class")
Public Async Function TestConversion2() As Task
Await TestInRegularAndScriptAsync(
"
class
class C
function M() as string
[||]if true
return ""a""
Expand All @@ -195,7 +195,7 @@ class
end function
end class",
"
class
class C
function M() as string
Return If(true, ""a"", nothing)
end function
Expand All @@ -206,7 +206,7 @@ end class")
Public Async Function TestConversion3() As Task
Await TestInRegularAndScriptAsync(
"
class
class C
function M() as string
[||]if true
return nothing
Expand All @@ -216,7 +216,7 @@ class
end function
end class",
"
class
class C
function M() as string
Return If(true, nothing, DirectCast(nothing, String))
end function
Expand All @@ -227,7 +227,7 @@ end class")
Public Async Function TestKeepTriviaAroundIf() As Task
Await TestInRegularAndScriptAsync(
"
class
class C
function M() as integer
' leading
[||]if true
Expand All @@ -238,7 +238,7 @@ class
end function
end class",
"
class
class C
function M() as integer
' leading
Return If(true, 0, 1) ' trailing
Expand All @@ -250,7 +250,7 @@ end class")
Public Async Function TestFixAll1() As Task
Await TestInRegularAndScriptAsync(
"
class
class C
function M() as integer
{|FixAllInDocument:if|} true
return 0
Expand All @@ -266,7 +266,7 @@ class
end function
end class",
"
class
class C
function M() as integer
Return If(true, 0, 1)

Expand All @@ -279,7 +279,7 @@ end class")
Public Async Function TestMultiLine1() As Task
Await TestInRegularAndScriptAsync(
"
class
class C
function M() as integer
[||]if true
return Foo(
Expand All @@ -290,7 +290,7 @@ class
end function
end class",
"
class
class C
function M() as integer
Return If(true,
Foo(
Expand All @@ -304,7 +304,7 @@ end class")
Public Async Function TestMultiLine2() As Task
Await TestInRegularAndScriptAsync(
"
class
class C
function M() as integer
[||]if true
return 0
Expand All @@ -315,7 +315,7 @@ class
end function
end class",
"
class
class C
function M() as integer
Return If(true,
0,
Expand All @@ -329,7 +329,7 @@ end class")
Public Async Function TestMultiLine3() As Task
Await TestInRegularAndScriptAsync(
"
class
class C
function M() as integer
[||]if true
return Foo(
Expand All @@ -341,7 +341,7 @@ class
end function
end class",
"
class
class C
function M() as integer
Return If(true,
Foo(
Expand All @@ -357,7 +357,7 @@ end class")
Public Async Function TestOnYield() As Task
Await TestInRegularAndScriptAsync(
"
class
class C
iterator function M() as integer
[||]if true
yield 0
Expand All @@ -367,7 +367,7 @@ class
end function
end class",
"
class
class C
iterator function M() as integer
Yield If(true, 0, 1)
end function
Expand All @@ -381,7 +381,7 @@ end class")
"
imports system.collections.generic

class
class C
iterator function M() as IEnumerable(of integer)
[||]if true
yield 0
Expand All @@ -393,10 +393,45 @@ end class",
"
imports system.collections.generic

class
class C
iterator function M() as IEnumerable(of integer)
Yield If(true, 0, 1)
end function
end class")
End Function

<WorkItem(36117, "https://github.com/dotnet/roslyn/issues/36117")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseConditionalExpression)>
Public Async Function TestMissingWhenCrossingPreprocessorDirective1() As Task
Await TestMissingInRegularAndScriptAsync(
"
class C
function M() as integer
dim check as boolean = true
#if true
[||]if check
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this valid syntax for VB if statement?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what part? not having parens? If so, yes this is valid VB :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant:

If check Then
Return 3
End If

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah. the then is optional in VB. Automatic code cleanup adds it. but it's not necessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But you would still need the End If?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

smacks-head. sorry. i get it now. will add hte other test as well :)

return 3
#end if
return 2
end function
end class")
End Function

<WorkItem(36117, "https://github.com/dotnet/roslyn/issues/36117")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseConditionalExpression)>
Public Async Function TestMissingWhenCrossingPreprocessorDirective2() As Task
Await TestMissingInRegularAndScriptAsync(
"
class C
function M() as integer
dim check as boolean = true
#if true
[||]if check
return 3
end if
#end if
return 2
end function
end class")
End Function
End Class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ protected sealed override void InitializeWorker(AnalysisContext context)
private void AnalyzeOperation(OperationAnalysisContext context)
{
var ifOperation = (IConditionalOperation)context.Operation;
var ifStatement = ifOperation.Syntax as TIfStatementSyntax;
if (ifStatement == null)
if (!(ifOperation.Syntax is TIfStatementSyntax ifStatement))
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public static bool TryMatchPattern(

if (falseStatement == null)
{
var parentBlock = ifOperation.Parent as IBlockOperation;
if (parentBlock == null)
if (!(ifOperation.Parent is IBlockOperation parentBlock))
{
return false;
}
Expand Down
Loading