Bug 17138 – Warn about superfluous "with" statements

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-02-02T14:40:00Z
Last change time
2024-12-13T18:51:29Z
Assigned to
No Owner
Creator
Eyal
Moved to GitHub: dmd#17788 →

Comments

Comment #0 by eyal — 2017-02-02T14:40:00Z
When using: with(someObject) { BODY; } And none of the expressions within BODY actually use any members of "someObject", and the destructor of "someObject" is not called at the end of the "with" statement, it is a likely bug. I use the following RAII idiom: with(someLock.acquired()) { .. } But get no warning if I accidentally: with(someLock) { }
Comment #1 by bugzilla — 2017-05-12T00:53:42Z
What does the declaration of someObject look like?
Comment #2 by eyal — 2017-05-12T15:24:58Z
struct Object { auto acquired() { struct Acquired { Object obj; this(Object* obj) { this.obj = obj; obj.acquire(); } ~this() { obj.release(); } } return Acquired(&this); } ... } Resource someObject; with(someObject.acquired()) { // <-- makes sense .. } Resource someObject; with(someObject) { // Oops! Accidental senseless 'with' statement .. } The latter can be detected as senseless, since it doesn't affect the code in any way. If dropping the with() statement (and keeping its body instead) would result in the exact same semantics -- warn about superfluous 'with' statement.
Comment #3 by bugzilla — 2017-05-13T15:54:47Z
Are you suggesting: --- struct S { int field; } void foo(S s) { with (s) // no error ++field; with (s) // error ++s.field; } --- ?
Comment #4 by eyal — 2017-05-13T21:48:36Z
Yes, that would be great.
Comment #5 by eyal — 2017-05-13T21:53:25Z
Another case that is beneficial and should not be an error: If the expression results in an object that scopes over the block and has a destructor. Example: with(someMutex.acquired) { // No use of any fields from the MutexAcquired object } // dtor of MutexAcquired called here, releasing the mutex So here you'd want no error either.
Comment #6 by robert.schadek — 2024-12-13T18:51:29Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/17788 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB