RSPEC-1125 does not follow modern exception throwing

  • SonarQube 4.1, Sonar Scanner 5.6.0.48455
  • return a success, or throw an exception
  • this is where I have fun
return msg == null ? true : throw new Exception(msg);

that is flagged by RSPEC-1125;
This however is NOT flagged:

if (msg != null) throw new Exception(msg);
return true;

They are the exact same in the compile ILASM it’s just a different syntax. The “throw” command should not be evaluated the same as a standard expression in this circumstance.

I have even done this to bypass Sonarqube RSPEC-1125:

private bool ValidatedRules(object value, LoadRestrictions testRule)
{
	if (!this.HasCustomRule) return true;  //<-- boolean literal

	(string msg, bool? valid) = !this.LoadRules.TestBit(testRule) ? (null, true) //<-- boolean literal
		: value switch
		{
			WpsMissing => ("LoadRules requires source column to be present", (bool?)null), //<-- boolean literal
			WpsEmpty => ("LoadRules requires source column's to have data", null), //<-- boolean literal
			WpsError => ("Unforseen error occurred in loading data to/from source", null), //<-- boolean literal
			_ => (null, true) //<-- boolean literal
		};

	return valid ?? throw new Exception(msg);
}

This last is a bit excessive, but it shows the nature of doing a simply ternary including a “throw” is far more readable than other craziness that is accepted by SonarQube…
The coalescing operator ?? uses this style of notation all the time and SonarQube does not complain, therefore usage of boolean literal in a ternary that also throws an exception should be equally acceptable.

Redundant Boolean literals should be removed from expressions to improve readability.

If that is true, then my last solution above should be rampant with RSPEC-1125 flags, as

  • It is rather unreadable
  • Boolean literals are extremely redundant
  • And it is merely written crazy to circumnavigate RSPEC-1125 flagging this:
return msg == null ? true : throw new Exception(msg);

In the above ternary the boolean literal is by definition NOT REDUNDANT.
Thanks
Jaeden “Sifo Dyas” al’Raec Ruiner

Hey there.

Are you really using an 8.5 year old version of SonarQube?