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) { }
What does the declaration of someObject look like?
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.
Are you suggesting: --- struct S { int field; } void foo(S s) { with (s) // no error ++field; with (s) // error ++s.field; } --- ?
Yes, that would be great.
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.
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