squid:S2589: check !=null in finally block is not an "always true expression"

  • SonarQube Community Edition Version 7.5 (build 20543)

  • squid:S2589 rule

  • Sonar detect Remove this expression which always evaluates to “true” but the expression can be false

  • To reproduce issue create a java class with the method:


public void run()
	{
		Object x = ... //call to sample method
		try
		{
			if (x.equals("aaa")) //if x is null there is a NullPointerException
			{
				//do something
			}
		}
		catch (NumberFormatException a)
		{
				//do something
		}
		finally
		{
//finally block is executed even if there is NullPointerException in try block, so x can be null
			if (x!=null)
			{
				//do something
			}
		}
	}

The system detect “if” in finally block as useless but this is an error because if x is null, finally block is executed and if block not.

Thank you for reporting this problem, I created ticket to track it https://jira.sonarsource.com/browse/SONARJAVA-3169

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.