C# S3626 false positive when returning before local functions

  • Operating system: Windows 10
  • Visual Studio version: Visual Studio 2022
  • SonarLint plugin version: 7.4.0.80741
  • Programming language you’re coding in: C# 12
  • Is connected mode used: No

I’ve created a few local functions inside a method in a custom Windows Forms control I’m creating. According to JetBrains ReSharper, local functions should be at the end of the method body after a return statement. However, when I add the empty return statement, SonarLint flags it because the method it’s in is a void method.

Here is a skeleton of the method in question:

protected override void OnPaint(PaintEventArgs e)
{
	// code excluded

	return;

	void DrawDigit(char digit, Point digitOrigin)
	{
		// code excluded
	}

	IEnumerable<char> GetDigits()
	{
		// code excluded
	}

	IEnumerable<Segment> GetSegmentsForDigit(char digit)
	{
		// code excluded
	}
}

Here is a screenshot of the return statement being flagged:
image

Hello @fishnet37222,

Welcome to the community.

I don’t think this is a false positive.
In the case of your OnPaint method, it returns void, and using a return at the end of it doesn’t have any impact on the flow of the method execution. Thus, the statement is redundant.

As for the conflicting issues with ReSharper, I would say this is a matter of opinion, as I personally see this suggestion as purely cosmetic.

Have a good day,