Ignore 'Remove this useless assignment' When Assigning to 'default'

csharp

(Christopher Moffatt) #1

Must-share information (formatted with Markdown):

  • SonarQube - Version 6.7.2 (build 37468)
  • Wish to ignore “Dead stores should be removed” code smell when assignment is to “default” value.
  • Nothing yet…

Within C#, instead of declaring local variables with their datatype first, our team always uses the “var” syntax sugar. It is easier to see where variables are being declared if they all begin with “var”.

So instead of:

    int x;
    SqlCommand command;

We declare it as:

    var x = default( int );
    var command = default( SqlCommand );

It is a few more characters but the creation and assignment of the local variable is more clear.

SonarQube reports this as a “useless assignment to local variable”, which I can assure you it is not.

Your documentation states that the following are initializations are exceptions:
* initializations to -1 , 0 , 1 , null , true , false , "" and string.Empty.

I believe that you need to add “default” to your list of initialization exceptions.


(Valeri Hristov) #2

Hi Christopher, I agree that the default expression should be allowed if 0 is allowed. Ticket created.