Thread overview
[Issue 17138] Warn about superfluous "with" statements
May 12, 2017
Walter Bright
May 12, 2017
Eyal
May 13, 2017
Walter Bright
May 13, 2017
Eyal
May 13, 2017
Eyal
Dec 17, 2022
Iain Buclaw
May 12, 2017
https://issues.dlang.org/show_bug.cgi?id=17138

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@digitalmars.com

--- Comment #1 from Walter Bright <bugzilla@digitalmars.com> ---
What does the declaration of someObject look like?

--
May 12, 2017
https://issues.dlang.org/show_bug.cgi?id=17138

--- Comment #2 from Eyal <eyal@weka.io> ---
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.

--
May 13, 2017
https://issues.dlang.org/show_bug.cgi?id=17138

--- Comment #3 from Walter Bright <bugzilla@digitalmars.com> ---
Are you suggesting:

---
struct S {
    int field;
}

void foo(S s) {
    with (s)      // no error
        ++field;

    with (s)      // error
        ++s.field;
}
---

?

--
May 13, 2017
https://issues.dlang.org/show_bug.cgi?id=17138

--- Comment #4 from Eyal <eyal@weka.io> ---
Yes, that would be great.

--
May 13, 2017
https://issues.dlang.org/show_bug.cgi?id=17138

--- Comment #5 from Eyal <eyal@weka.io> ---
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.

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=17138

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P4

--