Skip to content

Commit

Permalink
Add repro for NET-882
Browse files Browse the repository at this point in the history
  • Loading branch information
mary-georgiou-sonarsource authored and sonartech committed Jan 29, 2025
1 parent 523faac commit 9144165
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,22 @@ void VarDeclarationWithReassignmentToEmptyCollection()
}
}
}

// https://sonarsource.atlassian.net/browse/NET-882
namespace ReproNET882
{
public class ReproNet882
{
public void Method()
{
// 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).
AddHandler(OnErrorEvent);
AddHandler(new EventHandler(OnErrorEvent)); // Fixed
}
private void OnErrorEvent(object sender, EventArgs e) { }

public void AddHandler(Delegate handler)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,22 @@ void VarDeclarationWithReassignmentToEmptyCollection()
}
}
}

// https://sonarsource.atlassian.net/browse/NET-882
namespace ReproNET882
{
public class ReproNet882
{
public void Method()
{
// 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).
AddHandler(OnErrorEvent);
AddHandler(new EventHandler(OnErrorEvent)); // Fixed
}
private void OnErrorEvent(object sender, EventArgs e) { }

public void AddHandler(Delegate handler)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,22 @@ void VarDeclarationWithReassignmentToEmptyCollection()
}
}
}

// https://sonarsource.atlassian.net/browse/NET-882
namespace ReproNET882
{
public class ReproNet882
{
public void Method()
{
// 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).
AddHandler(OnErrorEvent);
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.
}
private void OnErrorEvent(object sender, EventArgs e) { }

public void AddHandler(Delegate handler)
{
}
}
}

0 comments on commit 9144165

Please sign in to comment.