FP on squid:S2589 when values of variables changed only in listeners

SonarQube Version: 7.9.1.27448
SonarJava Version: 5.14
Java Version: 1.8

Description:

Variable is set at beginning of a method. The value of the variable will be changed only in listeners.
SonarQube issues will be shown on conditions. The conditions should be changed so that they does not always evaluate to false/true.

Minimum code example:

public class BooleanExpression {
	private static boolean checkExpression = true;
	
	public static void main(String[] args) {
		// variable must be set else no sonar issue will be show in line 35
		checkExpression = false;
		
		JFrame frame = new JFrame("Test");
		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		JDialog dialog = new JDialog(frame, "Test Dialog");
		dialog.setSize(100, 100);
		dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		
		// dialog must be modal. Condition should wait until Dialog is closed
		dialog.setModalityType(ModalityType.APPLICATION_MODAL);
		
		JButton button = new JButton("set True");
		
		// change value in listener
		button.addActionListener(e -> checkExpression = true);
		
		dialog.add(button);
		
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		dialog.setLocationRelativeTo(null);
		dialog.setVisible(true);
		
		// Issue: Change this condition so that it does not always evaluate to "false"
		if (checkExpression) {
			System.out.println("checkExpression is true");
		}
	}
}

Hey Nicolas,

I can’t trigger S2589 on this code using SonarJava v6.0.1 – mind upgrading and trying again?

S2583 is triggered – is it possible you made a typo in your post?

Colin

1 Like

Hey Colin,

I missed negation in the condition. With negation it’s S2589 without S2583 but I would say both cases are fp.

Nicolas

Hello @Nicolas,

I just checked, and it seems that this FP is still present on the current version, I therefore created a ticket (SONARJAVA-3319) to track it.

Thanks for taking time to report it, and providing a code example!

Best,
Quentin

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