Skip to content

Commit 9144165

Browse files
mary-georgiou-sonarsourcesonartech
authored and
sonartech
committed
Add repro for NET-882
1 parent 523faac commit 9144165

3 files changed

+57
-0
lines changed

analyzers/tests/SonarAnalyzer.Test/TestCases/RedundantDeclaration.CSharp12.ArraySize.Fixed.cs

+19
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,22 @@ void VarDeclarationWithReassignmentToEmptyCollection()
7272
}
7373
}
7474
}
75+
76+
// https://sonarsource.atlassian.net/browse/NET-882
77+
namespace ReproNET882
78+
{
79+
public class ReproNet882
80+
{
81+
public void Method()
82+
{
83+
// In this example Action<object, RoutedEventArgs> is down-casted by AddHandler to EventHandler (EventHandler has the exact same definition as the Action but they are different).
84+
AddHandler(OnErrorEvent);
85+
AddHandler(new EventHandler(OnErrorEvent)); // Fixed
86+
}
87+
private void OnErrorEvent(object sender, EventArgs e) { }
88+
89+
public void AddHandler(Delegate handler)
90+
{
91+
}
92+
}
93+
}

analyzers/tests/SonarAnalyzer.Test/TestCases/RedundantDeclaration.CSharp12.LambdaParameterType.Fixed.cs

+19
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,22 @@ void VarDeclarationWithReassignmentToEmptyCollection()
7272
}
7373
}
7474
}
75+
76+
// https://sonarsource.atlassian.net/browse/NET-882
77+
namespace ReproNET882
78+
{
79+
public class ReproNet882
80+
{
81+
public void Method()
82+
{
83+
// In this example Action<object, RoutedEventArgs> is down-casted by AddHandler to EventHandler (EventHandler has the exact same definition as the Action but they are different).
84+
AddHandler(OnErrorEvent);
85+
AddHandler(new EventHandler(OnErrorEvent)); // Fixed
86+
}
87+
private void OnErrorEvent(object sender, EventArgs e) { }
88+
89+
public void AddHandler(Delegate handler)
90+
{
91+
}
92+
}
93+
}

analyzers/tests/SonarAnalyzer.Test/TestCases/RedundantDeclaration.CSharp12.cs

+19
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,22 @@ void VarDeclarationWithReassignmentToEmptyCollection()
7272
}
7373
}
7474
}
75+
76+
// https://sonarsource.atlassian.net/browse/NET-882
77+
namespace ReproNET882
78+
{
79+
public class ReproNet882
80+
{
81+
public void Method()
82+
{
83+
// In this example Action<object, RoutedEventArgs> is down-casted by AddHandler to EventHandler (EventHandler has the exact same definition as the Action but they are different).
84+
AddHandler(OnErrorEvent);
85+
AddHandler(new EventHandler(OnErrorEvent)); // Noncompliant FP, the inferred natural type is wrong and therefore an explicit delegate creation is needed. This is relevant for C# 10 and later.
86+
}
87+
private void OnErrorEvent(object sender, EventArgs e) { }
88+
89+
public void AddHandler(Delegate handler)
90+
{
91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)