Bug 551072 - Diagnose state changing assertions
Summary: Diagnose state changing assertions
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.13   Edit
Hardware: PC Windows 10
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-09-14 08:09 EDT by Ed Willink CLA
Modified: 2020-06-05 18:06 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ed Willink CLA 2019-09-14 08:09:12 EDT
In "[eclipse-dev] enable assert during tests?" Stephan Herrmann writes

"That said, in my case I was actually happy that one test run did NOT have "-ea" because it detected one of the stupidest mistakes I had made: putting a state-changing operation into "assert"."

Suggestion: if the body of an assert is state-changing provide a compiler error by default. If the body is too complex to analyze provide a compiler warning by default.
Comment 1 Stephan Herrmann CLA 2019-09-14 10:20:36 EDT
I was (obviously :) ) thinking about this. Unfortunately *every method call* is (practically) too complex to analyse.

This would change if Java had a standard @Pure annotations for pure functions ...
Comment 2 Ed Willink CLA 2019-09-14 12:33:46 EDT
A very quick scan of my own code suggests that over 75% of asserts are of the form

assert x;
assert !x;
assert x == null;
assert x != null;

Surely all analyzeable? If anything more complicated was a warning,it could be that it provokes only grumbles rather than complaints.

The check seems worthwhile, just need to decide whether the not-analyzeable default is warning or ignore.
Comment 3 Stephan Herrmann CLA 2020-05-18 19:06:36 EDT
schedule for assessment re comment 2 during 4.17 - no promise yet.