Sonar Rule tsql:S1116 Empty statements should be removed not correct for THROW Statements

I think the rule tsql:S1116 Empty statements should be removed is not always correct for T-SQL using THROW Statements.
In some cases we have to throw an error in a catch or if block

IF (1=1)
BEGIN
	;THROW 50000, 'Custom Error Message', 1;
END

or

IF (1=1)
BEGIN;
	THROW 50000, 'Custom Error Message', 1;
END

The leading semicolon is important. See Microsoft documentation THROW (Transact-SQL) - SQL Server | Microsoft Learn
“The statement before the THROW statement must be followed by the semicolon (:wink: statement terminator.”

THX

Hey there.

What version of SonarQube are you using?

Hi!

  • Enterprise Edition
  • Version 10.2.1 (build 78527)

Hello @ArminPrieschl,

Thank you for reporting this issue.

I think this is similar to the issue you describe in your other post.

As I said in the other post, I believe BEGIN is not a statement as it is part of the BEGIN...END statement.
Technically, within the BEGIN...END block, there are no statements before THROW.
So the semicolon is not necessary before the THROW statement.

This should work:

IF (1=1)
BEGIN
	THROW 50000, 'Custom Error Message', 1;
END;

Let us know if your request to Microsoft brings more light on this.

Thank you,

Microsoft Request:

Developer Community?

1 Like