Skip to content

Commit 47d094b

Browse files
authored
Merge pull request #36124 from CyrusNajmabadi/directiveCheck
Don't offer 'convert if to conditional' if it crosses a preprocessor directive.
2 parents a3f2539 + 1941e9d commit 47d094b

File tree

13 files changed

+176
-135
lines changed

13 files changed

+176
-135
lines changed

src/EditorFeatures/CSharpTest/UseConditionalExpression/UseConditionalExpressionForReturnTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,26 @@ IEnumerable<bool> M(int a)
821821
{
822822
yield return a != 0;
823823
}
824+
}");
825+
}
826+
827+
[WorkItem(36117, "https://github.com/dotnet/roslyn/issues/36117")]
828+
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseConditionalExpression)]
829+
public async Task TestMissingWhenCrossingPreprocessorDirective()
830+
{
831+
await TestMissingInRegularAndScriptAsync(
832+
@"
833+
class C
834+
{
835+
int M()
836+
{
837+
bool check = true;
838+
#if true
839+
[||]if (check)
840+
return 3;
841+
#endif
842+
return 2;
843+
}
824844
}");
825845
}
826846
}

src/EditorFeatures/VisualBasicTest/UseConditionalExpression/UseConditionalExpressionForReturnTests.vb

Lines changed: 67 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.UseConditionalExpr
1818
Public Async Function TestOnSimpleReturn() As Task
1919
Await TestInRegularAndScriptAsync(
2020
"
21-
class
21+
class C
2222
function M() as integer
2323
[||]if true
2424
return 0
@@ -28,7 +28,7 @@ class
2828
end function
2929
end class",
3030
"
31-
class
31+
class C
3232
function M() as integer
3333
Return If(true, 0, 1)
3434
end function
@@ -39,7 +39,7 @@ end class")
3939
Public Async Function TestOnSimpleReturnNoBlocks() As Task
4040
Await TestInRegularAndScriptAsync(
4141
"
42-
class
42+
class C
4343
function M() as integer
4444
[||]if true
4545
return 0
@@ -49,7 +49,7 @@ class
4949
end function
5050
end class",
5151
"
52-
class
52+
class C
5353
function M() as integer
5454
Return If(true, 0, 1)
5555
end function
@@ -60,7 +60,7 @@ end class")
6060
Public Async Function TestOnSimpleReturnNoBlocks_NotInBlock() As Task
6161
Await TestInRegularAndScriptAsync(
6262
"
63-
class
63+
class C
6464
function M() as integer
6565
if true
6666
[||]if true
@@ -72,7 +72,7 @@ class
7272
end function
7373
end class",
7474
"
75-
class
75+
class C
7676
function M() as integer
7777
if true
7878
Return If(true, 0, 1)
@@ -85,7 +85,7 @@ end class")
8585
Public Async Function TestMissingReturnValue1() As Task
8686
Await TestMissingInRegularAndScriptAsync(
8787
"
88-
class
88+
class C
8989
function M() as integer
9090
[||]if true
9191
return 0
@@ -100,7 +100,7 @@ end class")
100100
Public Async Function TestMissingReturnValue2() As Task
101101
Await TestMissingInRegularAndScriptAsync(
102102
"
103-
class
103+
class C
104104
function M() as integer
105105
[||]if true
106106
return
@@ -115,7 +115,7 @@ end class")
115115
Public Async Function TestMissingReturnValue3() As Task
116116
Await TestMissingInRegularAndScriptAsync(
117117
"
118-
class
118+
class C
119119
function M() as integer
120120
[||]if true
121121
return
@@ -130,7 +130,7 @@ end class")
130130
Public Async Function TestWithNoElseBlockButFollowingReturn() As Task
131131
Await TestInRegularAndScriptAsync(
132132
"
133-
class
133+
class C
134134
function M() as integer
135135
[||]if true
136136
return 0
@@ -140,7 +140,7 @@ class
140140
end function
141141
end class",
142142
"
143-
class
143+
class C
144144
function M() as integer
145145
Return If(true, 0, 1)
146146
end function
@@ -151,7 +151,7 @@ end class")
151151
Public Async Function TestMissingWithoutElse() As Task
152152
Await TestMissingInRegularAndScriptAsync(
153153
"
154-
class
154+
class C
155155
function M() as integer
156156
[||]if true
157157
return 0
@@ -164,7 +164,7 @@ end class")
164164
Public Async Function TestConversion1() As Task
165165
Await TestInRegularAndScriptAsync(
166166
"
167-
class
167+
class C
168168
function M() as object
169169
[||]if true
170170
return ""a""
@@ -174,7 +174,7 @@ class
174174
end function
175175
end class",
176176
"
177-
class
177+
class C
178178
function M() as object
179179
Return If(true, ""a"", ""b"")
180180
end function
@@ -185,7 +185,7 @@ end class")
185185
Public Async Function TestConversion2() As Task
186186
Await TestInRegularAndScriptAsync(
187187
"
188-
class
188+
class C
189189
function M() as string
190190
[||]if true
191191
return ""a""
@@ -195,7 +195,7 @@ class
195195
end function
196196
end class",
197197
"
198-
class
198+
class C
199199
function M() as string
200200
Return If(true, ""a"", nothing)
201201
end function
@@ -206,7 +206,7 @@ end class")
206206
Public Async Function TestConversion3() As Task
207207
Await TestInRegularAndScriptAsync(
208208
"
209-
class
209+
class C
210210
function M() as string
211211
[||]if true
212212
return nothing
@@ -216,7 +216,7 @@ class
216216
end function
217217
end class",
218218
"
219-
class
219+
class C
220220
function M() as string
221221
Return If(true, nothing, DirectCast(nothing, String))
222222
end function
@@ -227,7 +227,7 @@ end class")
227227
Public Async Function TestKeepTriviaAroundIf() As Task
228228
Await TestInRegularAndScriptAsync(
229229
"
230-
class
230+
class C
231231
function M() as integer
232232
' leading
233233
[||]if true
@@ -238,7 +238,7 @@ class
238238
end function
239239
end class",
240240
"
241-
class
241+
class C
242242
function M() as integer
243243
' leading
244244
Return If(true, 0, 1) ' trailing
@@ -250,7 +250,7 @@ end class")
250250
Public Async Function TestFixAll1() As Task
251251
Await TestInRegularAndScriptAsync(
252252
"
253-
class
253+
class C
254254
function M() as integer
255255
{|FixAllInDocument:if|} true
256256
return 0
@@ -266,7 +266,7 @@ class
266266
end function
267267
end class",
268268
"
269-
class
269+
class C
270270
function M() as integer
271271
Return If(true, 0, 1)
272272

@@ -279,7 +279,7 @@ end class")
279279
Public Async Function TestMultiLine1() As Task
280280
Await TestInRegularAndScriptAsync(
281281
"
282-
class
282+
class C
283283
function M() as integer
284284
[||]if true
285285
return Foo(
@@ -290,7 +290,7 @@ class
290290
end function
291291
end class",
292292
"
293-
class
293+
class C
294294
function M() as integer
295295
Return If(true,
296296
Foo(
@@ -304,7 +304,7 @@ end class")
304304
Public Async Function TestMultiLine2() As Task
305305
Await TestInRegularAndScriptAsync(
306306
"
307-
class
307+
class C
308308
function M() as integer
309309
[||]if true
310310
return 0
@@ -315,7 +315,7 @@ class
315315
end function
316316
end class",
317317
"
318-
class
318+
class C
319319
function M() as integer
320320
Return If(true,
321321
0,
@@ -329,7 +329,7 @@ end class")
329329
Public Async Function TestMultiLine3() As Task
330330
Await TestInRegularAndScriptAsync(
331331
"
332-
class
332+
class C
333333
function M() as integer
334334
[||]if true
335335
return Foo(
@@ -341,7 +341,7 @@ class
341341
end function
342342
end class",
343343
"
344-
class
344+
class C
345345
function M() as integer
346346
Return If(true,
347347
Foo(
@@ -357,7 +357,7 @@ end class")
357357
Public Async Function TestOnYield() As Task
358358
Await TestInRegularAndScriptAsync(
359359
"
360-
class
360+
class C
361361
iterator function M() as integer
362362
[||]if true
363363
yield 0
@@ -367,7 +367,7 @@ class
367367
end function
368368
end class",
369369
"
370-
class
370+
class C
371371
iterator function M() as integer
372372
Yield If(true, 0, 1)
373373
end function
@@ -381,7 +381,7 @@ end class")
381381
"
382382
imports system.collections.generic
383383

384-
class
384+
class C
385385
iterator function M() as IEnumerable(of integer)
386386
[||]if true
387387
yield 0
@@ -393,10 +393,45 @@ end class",
393393
"
394394
imports system.collections.generic
395395

396-
class
396+
class C
397397
iterator function M() as IEnumerable(of integer)
398398
Yield If(true, 0, 1)
399399
end function
400+
end class")
401+
End Function
402+
403+
<WorkItem(36117, "https://github.com/dotnet/roslyn/issues/36117")>
404+
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseConditionalExpression)>
405+
Public Async Function TestMissingWhenCrossingPreprocessorDirective1() As Task
406+
Await TestMissingInRegularAndScriptAsync(
407+
"
408+
class C
409+
function M() as integer
410+
dim check as boolean = true
411+
#if true
412+
[||]if check
413+
return 3
414+
#end if
415+
return 2
416+
end function
417+
end class")
418+
End Function
419+
420+
<WorkItem(36117, "https://github.com/dotnet/roslyn/issues/36117")>
421+
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseConditionalExpression)>
422+
Public Async Function TestMissingWhenCrossingPreprocessorDirective2() As Task
423+
Await TestMissingInRegularAndScriptAsync(
424+
"
425+
class C
426+
function M() as integer
427+
dim check as boolean = true
428+
#if true
429+
[||]if check
430+
return 3
431+
end if
432+
#end if
433+
return 2
434+
end function
400435
end class")
401436
End Function
402437
End Class

src/Features/Core/Portable/UseConditionalExpression/AbstractUseConditionalExpressionDiagnosticAnalyzer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ protected sealed override void InitializeWorker(AnalysisContext context)
3939
private void AnalyzeOperation(OperationAnalysisContext context)
4040
{
4141
var ifOperation = (IConditionalOperation)context.Operation;
42-
var ifStatement = ifOperation.Syntax as TIfStatementSyntax;
43-
if (ifStatement == null)
42+
if (!(ifOperation.Syntax is TIfStatementSyntax ifStatement))
4443
{
4544
return;
4645
}

src/Features/Core/Portable/UseConditionalExpression/ForReturn/UseConditionalExpressionForReturnHelpers.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ public static bool TryMatchPattern(
3535

3636
if (falseStatement == null)
3737
{
38-
var parentBlock = ifOperation.Parent as IBlockOperation;
39-
if (parentBlock == null)
38+
if (!(ifOperation.Parent is IBlockOperation parentBlock))
4039
{
4140
return false;
4241
}

0 commit comments

Comments
 (0)