FP Java S2693 & S1171 Threads should not be started in constructors (anonymous classes)

  • Operating system: Windows
  • SonarQube for Eclipse plugin version: 10.11
  • Programming language you’re coding in: Java
  • Is connected mode used: No

The following code generates the warning. I think it should NOT since by definition the class is ‘final’ and the warning is not issued on final classes as appropraite.

            	        new Thread("name")
        		        {        	
        		            {
        		                setDaemon(true);
        		                start();
        		            }
                              }
        		      @Override	public void run()  { }

Hey @dandoy!

Thanks for the report. Can you provide a more complete code sample that reproduces the issue? Preferably a single file that includes the class definition.

package kevin;

public class FPTest {
    public static void main(String[] args)
	{
		new Thread("name")
	    {        	
	        {
	            setDaemon(true);
	            start();
	        }
	        @Override	public void run()  { return;}
	    };
	}
}

Hello @dandoy , thanks for reporting this issue.

I’m checking it myself, and I don’t get the rule S2693 triggered for that code, but I get S1171.

I’m using IntelliJ, SonarQube plugin 10.13

Could you please recheck and send a screenshot ?

My Company currently blocking sending/uploading screen capture. Here is the text from Sonar in my Eclipse IDE.

Description Resource Path Location Type

Move this “start” call to another method. FPTest.java /zKevin/src/kevin line 10 SonarLint Report Issue

By the way S1171 (we disabled) is also a FP when in an anonymous class since it cannot have a constructor.

Now I found that S2693 comes disabled by default. Now I get the same issue as you.
I’m investigating and I will come back with my findings.
Thanks for your patience.

Might as well get S1171 fixed also since it is generating a FP as you have shown, It should not give a positive on an anonymous class.

Any update on this?

Sorry for the delay @dandoy , we’ve been in an offsite the whole last week.
I will keep you posted.

I agree with you that for this inline class it doesn’t make any sense to trigger this rule.
I have created a ticket for the development team to take care : Jira

Hopefully you @jonathan.vila can create a jira for S1171 also which I mentioned and you initially produced also with the same code sample.

As we try to have threads as tidy as possible and 1 rule per thread, do you mind creating a new thread pointing to the issue with S1171? Thanks a lot.

Since you are the one that initially mentioned it in this thread, I’ll let you create the new thread.

The usual process is that community users are the ones in charge of creating the threads and adding all the info needed for us to investigate, answer and solve.

Thanks for your understanding.

I understand that is the ‘usual’ process but this is not ‘usual’ since you are the one that detected the FP. I am surprised that Sonar only address issues found by the user community and items found internally are not addressed unless also reported by a member of the community. Our company is a paying customer for SonarQuber Server Enterprise and we expect a quality product from a company that address issues found internally and not solely from the customer base. Thanks for your understanding.

Issue created for S1171
https://sonarsource.atlassian.net/browse/SONARJAVA-5340

Thanks