False positive with S2583

Our stack is Blazor frontend, .Net core 6 backend, GitHub for ALM and CICD.

We’ve found that in a loop, if the iterator’s max value is defined outside of the function, the linter can’t identify it and determines that the code inside always evaluates to false.

protected const int WeekCount = 10;

protected decimal CostTotal
{
	get
	{
		var weeklyPay = 1;
		var totalEmployees = 2;
		var value = 0m;

		for (int week = 0; week < WeekCount; week++)
		{
			var totalEmployeesForWeek = /*(Warning here =>)*/ week > 2 ? 0 : totalEmployees;

			value += totalEmployeesForWeek * weeklyPay;
		}
		return value;
	}
}

However if defined inside the function or using an integer (ie week < 10) the linter doesn’t have an issue.

We’ve had this false positive both in SonarCloud and SonarLint, and thought it was interesting enough to share.