FN S1905: Redundant casts should not be used

Check the following code:

class Noncompliant
{
	public void UseSuffixForLiterals()
	{
		var d0 = (decimal)12.0; // Noncompliant, use 12.0m
		var d1 = (decimal)12; // Noncompliant, use 12m
		var d2 = (double)12; // Noncompliant, use 12d
		var f0 = (float)12.0; // Noncompliant, use 12.0f
		var n0 = (uint)12; // Noncompliant, use 12u
		var n1 = (long)12; // Noncompliant, use 12l
		var n2 = (ulong)12; // Noncompliant, use 12ul
	}
}

I think S1905 should report on this too, ideally with a code fix.

I assume this is also applicable for Java and C++, and there might be other stongly typed languages that you support where this is possible.

Tested with SonarAnalyzer.CSharp v10.7.0.110445

Hello @Corniel,

I confirm the FNs and added a re-producer. Strictly speaking, the cast isn’t redundant (in the sense that it can be removed without changing the meaning of the code) but the expression can be simplified. The rule is close enough to include this in the rule.

1 Like

@Martin_Strecker exactly my point. It makes more sense to extend the rule than to introduce a new one. :slight_smile:

Link to the internal tracking issue.