Skip to content

Commit 0dbe4d0

Browse files
committed
Handle ref parameter detection for unused parameters
1 parent 5d89adf commit 0dbe4d0

File tree

4 files changed

+121
-3
lines changed

4 files changed

+121
-3
lines changed

CodeCracker.VisualBasic.sln

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ Global
7272
{1CD1A3EE-28CE-404B-A59E-AEACF762D938}.Release|Any CPU.ActiveCfg = Release|Any CPU
7373
{1CD1A3EE-28CE-404B-A59E-AEACF762D938}.Release|Any CPU.Build.0 = Release|Any CPU
7474
{B7B513B4-0317-4F32-B560-4BFC4FAEC239}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
75-
{B7B513B4-0317-4F32-B560-4BFC4FAEC239}.Debug|Any CPU.Build.0 = Debug|Any CPU
7675
{B7B513B4-0317-4F32-B560-4BFC4FAEC239}.DebugNoVsix|Any CPU.ActiveCfg = Debug|Any CPU
7776
{B7B513B4-0317-4F32-B560-4BFC4FAEC239}.Release|Any CPU.ActiveCfg = Release|Any CPU
7877
{B7B513B4-0317-4F32-B560-4BFC4FAEC239}.Release|Any CPU.Build.0 = Release|Any CPU

src/VisualBasic/CodeCracker/Usage/UnusedParametersAnalyzer.vb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ You should delete the parameter in such cases."
5050
For Each parameter In parameters
5151
Dim parameterSymbol = parameter.Value
5252
If parameterSymbol Is Nothing Then Continue For
53-
If Not dataFlowAnalysis.ReadInside.Contains(parameterSymbol) Then 'AndAlso Not dataFlowAnalysis.WrittenOutside.Contains(parameterSymbol)
53+
If Not dataFlowAnalysis.ReadInside.Contains(parameterSymbol) AndAlso
54+
Not dataFlowAnalysis.WrittenInside.Contains(parameterSymbol) Then
5455
context = CreateDiagnostic(context, parameter.Key)
5556
End If
5657
Next

test/CSharp/CodeCracker.Test/Usage/UnusedParametersTests.cs

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,5 +449,97 @@ class Derived : Base
449449
}";
450450
await VerifyCSharpHasNoDiagnosticsAsync(source);
451451
}
452+
453+
[Fact]
454+
public async Task CallWithRefPeremeterDoesNotCreateDiagnostic()
455+
{
456+
const string source = @"
457+
class TypeName
458+
{
459+
bool TryParse(string input, ref int output)
460+
{
461+
try
462+
{
463+
output = int.Parse(input);
464+
return true;
465+
}
466+
catch
467+
{
468+
return false;
469+
}
470+
}
471+
}";
472+
await VerifyCSharpHasNoDiagnosticsAsync(source);
473+
}
474+
[Fact]
475+
public async Task CallWithUnusedRefPeremeterCreateDiagnostic()
476+
{
477+
const string source = @"
478+
class TypeName
479+
{
480+
bool TryParse(string input, ref int output, ref int out2)
481+
{
482+
try
483+
{
484+
output = int.Parse(input);
485+
return true;
486+
}
487+
catch
488+
{
489+
return false;
490+
}
452491
}
453-
}
492+
}";
493+
494+
await VerifyCSharpDiagnosticAsync(source, CreateDiagnosticResult("out2", 4, 49));
495+
}
496+
497+
[Fact]
498+
public async Task CallWithOutPeremeterDoesNotCreateDiagnostic()
499+
{
500+
const string source = @"
501+
class TypeName
502+
{
503+
bool TryParse(string input, out int output)
504+
{
505+
try
506+
{
507+
output = int.Parse(input);
508+
return true;
509+
}
510+
catch
511+
{
512+
return false;
513+
}
514+
}
515+
}";
516+
await VerifyCSharpHasNoDiagnosticsAsync(source);
517+
}
518+
[Fact]
519+
public async Task CallWithUnusedOutPeremeterCreateDiagnostic()
520+
{
521+
const string source = @"
522+
class TypeName
523+
{
524+
bool TryParse(string input, out int output, out int out2)
525+
{
526+
try
527+
{
528+
output = int.Parse(input);
529+
return true;
530+
}
531+
catch
532+
{
533+
return false;
534+
}
535+
}
536+
}";
537+
538+
await VerifyCSharpDiagnosticAsync(source, CreateDiagnosticResult("out2", 4, 49));
539+
}
540+
541+
}
542+
543+
}
544+
545+

test/VisualBasic/CodeCracker.Test/Usage/UnusedParameterTests.vb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,32 @@ End Class
361361
Await VerifyBasicHasNoDiagnosticsAsync(source)
362362
End Function
363363

364+
<Fact>
365+
Public Async Function CallWithRefParameterDoesNotCreateDiagnostic() As Task
366+
Const source = "
367+
Class Base
368+
Private Function TryParse(input As String, ByRef output As Integer) As Boolean
369+
output = CInt(input)
370+
Return True
371+
End Function
372+
End Class
373+
"
374+
Await VerifyBasicHasNoDiagnosticsAsync(source)
375+
End Function
376+
377+
<Fact>
378+
Public Async Function CallWithUnusedRefParameterDoesCreateDiagnostic() As Task
379+
Const source = "
380+
Class Base
381+
Private Function TryParse(input As String, ByRef output As Integer, ByRef out2 As Integer) As Boolean
382+
output = CInt(input)
383+
Return True
384+
End Function
385+
End Class
386+
"
387+
Await VerifyBasicDiagnosticAsync(source, CreateDiagnosticResult("out2", 3, 77))
388+
End Function
389+
364390
Private Function CreateDiagnosticResult(parameterName As String, line As Integer, column As Integer) As DiagnosticResult
365391
Return New DiagnosticResult With {
366392
.Id = DiagnosticId.UnusedParameters.ToDiagnosticId(),

0 commit comments

Comments
 (0)