Bug 543684 - [12] Warn against dubious assignments inside a switch expression
Summary: [12] Warn against dubious assignments inside a switch expression
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.11   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-01-22 09:00 EST by Stephan Herrmann CLA
Modified: 2019-01-22 09:00 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Stephan Herrmann CLA 2019-01-22 09:00:59 EST
In a new test case I found this:

X x = new X();
x  = switch (i) {
	case 1  ->   {
		x = null;
		break x;
	}
	default -> null;
	};

While all seems legal, the assignment "x = null" looks very suspicious to me. Will developers realize, that the switch *expression* produces a side effect which will be overwritten by the value of the switch expression itself?

Change the case block to
   x = new X();
   break null;
and you have a perfect riddle.

One reason this program violates quick intuition: the assignment inside the switch expression appears lexically below (perhaps much below) the final assignment of the switch expression's value, so it is very hard to see, that the outer assignment occurs *later*.

I could imagine different levels of warning:
- warn against any assignment of an outer local inside a switch expression.
- only warn when switch expression's value is being assigned to the same variable
- further filter to report only situations where the inner assigned value is never read

The above example meets all these criteria. We may either pick one of these levels, or create one warning option with sub-options.