www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Dscanner: intentionally unused variable

reply Ivan Trombley <itrombley dot-borg.org> writes:
While working with SDL, I found that I kept using the same 
pattern over and over:
- Get the current clip rectangle.
- Set a new clip rectangle.
- restore the old clip rectangle on scope (exit).

Instead of writing that code again and again, I wrote a simple 
function that returns a struct which restores the old clip 
rectangle in it's destructor. This works great but now dscanner 
complains about the variable being unused.

Is there a way tell dscanner that a variable is intentionally 
unused?
Jan 06
next sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sun, Jan 07, 2018 at 12:18:27AM +0000, Ivan Trombley via Digitalmars-d wrote:
 While working with SDL, I found that I kept using the same pattern over and
 over:
 - Get the current clip rectangle.
 - Set a new clip rectangle.
 - restore the old clip rectangle on scope (exit).
 
 Instead of writing that code again and again, I wrote a simple
 function that returns a struct which restores the old clip rectangle
 in it's destructor.  This works great but now dscanner complains about
 the variable being unused.
 
 Is there a way tell dscanner that a variable is intentionally unused?
IMO, dscanner should be fixed to suppress that warning when the variable in question has a non-trivial dtor that may produce side-effects at the end of the scope. In such cases, it may be that the whole reason for the variable is to trigger the dtor's side-effects, as is your case here, so technically it isn't "unused". T -- Don't throw out the baby with the bathwater. Use your hands...
Jan 06
parent Basile B. <b2.temp gmx.com> writes:
On Sunday, 7 January 2018 at 00:22:15 UTC, H. S. Teoh wrote:
 On Sun, Jan 07, 2018 at 12:18:27AM +0000, Ivan Trombley via 
 Digitalmars-d wrote:
 While working with SDL, I found that I kept using the same 
 pattern over and
 over:
 - Get the current clip rectangle.
 - Set a new clip rectangle.
 - restore the old clip rectangle on scope (exit).
 
 Instead of writing that code again and again, I wrote a simple 
 function that returns a struct which restores the old clip 
 rectangle in it's destructor.  This works great but now 
 dscanner complains about the variable being unused.
 
 Is there a way tell dscanner that a variable is intentionally 
 unused?
IMO, dscanner should be fixed to suppress that warning when the variable in question has a non-trivial dtor that may produce side-effects at the end of the scope. In such cases, it may be that the whole reason for the variable is to trigger the dtor's side-effects, as is your case here, so technically it isn't "unused". T
Unfortunately this is not possible without a big refactoring. The struct can be declared in another module and D-Scanner only sees the VariableDeclaration because its scope is limited to the current module.
Jan 07
prev sibling next sibling parent reply SimonN <eiderdaus gmail.com> writes:
On Sunday, 7 January 2018 at 00:18:27 UTC, Ivan Trombley wrote:
 While working with SDL, I found that I kept using the same 
 pattern over and over:
 - Get the current clip rectangle.
 - Set a new clip rectangle.
 - restore the old clip rectangle on scope (exit).

 Instead of writing that code again and again, I wrote a simple 
 function that returns a struct which restores the old clip 
 rectangle in it's destructor. This works great but now dscanner 
 complains about the variable being unused.

 Is there a way tell dscanner that a variable is intentionally 
 unused?
Another way would be to have the RAII wrapper in a with statement, but it produces extra indentation, which you might not like: with (MyStruct(100, 200)) { // code that uses the new clip rectangle } -- Simon
Jan 06
parent Ivan Trombley <itrombley dot-borg.org> writes:
On Sunday, 7 January 2018 at 03:41:18 UTC, SimonN wrote:
 Another way would be to have the RAII wrapper in a with 
 statement, but it produces extra indentation, which you might 
 not like:

     with (MyStruct(100, 200)) {
         // code that uses the new clip rectangle
     }

 -- Simon
This works good enough. Thanks.
Jan 07
prev sibling next sibling parent reply Basile B. <b2.temp gmx.com> writes:
On Sunday, 7 January 2018 at 00:18:27 UTC, Ivan Trombley wrote:
 While working with SDL, I found that I kept using the same 
 pattern over and over:
 - Get the current clip rectangle.
 - Set a new clip rectangle.
 - restore the old clip rectangle on scope (exit).

 Instead of writing that code again and again, I wrote a simple 
 function that returns a struct which restores the old clip 
 rectangle in it's destructor. This works great but now dscanner 
 complains about the variable being unused.

 Is there a way tell dscanner that a variable is intentionally 
 unused?
You can disable the "unused variable check" for the module in the dscanner.ini file located in the project directory see https://github.com/dlang-community/D-Scanner#selecting-modules-for-a-specific-check. More simple is to understand D-Scanner limitations and accept that warnings are only warnings and that a message doesn't necessarily mean that there's something to do.
Jan 07
parent Ivan Trombley <itrombley dot-borg.org> writes:
On Sunday, 7 January 2018 at 08:46:40 UTC, Basile B. wrote:
 More simple is to understand D-Scanner limitations and accept 
 that warnings are only warnings and that a message doesn't 
 necessarily mean that there's something to do.
If the output is clogged with warnings then it's more difficult to see the actual issues, limiting the value of dscanner. It makes sense to try to find a reasonable solution.
Jan 07
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2018-01-07 01:18, Ivan Trombley wrote:
 While working with SDL, I found that I kept using the same pattern over 
 and over:
 - Get the current clip rectangle.
 - Set a new clip rectangle.
 - restore the old clip rectangle on scope (exit).
 
 Instead of writing that code again and again, I wrote a simple function 
 that returns a struct which restores the old clip rectangle in it's 
 destructor. This works great but now dscanner complains about the 
 variable being unused.
 
 Is there a way tell dscanner that a variable is intentionally unused?
I don't know about D-Scanner but I know that other similar tools allow you to prefix the name of a variable with an underscore to indicate that the variable is intentionally unused. -- /Jacob Carlborg
Jan 07
parent reply visitor <visitor gmail.com> writes:
On Sunday, 7 January 2018 at 16:14:11 UTC, Jacob Carlborg wrote:
 On 2018-01-07 01:18, Ivan Trombley wrote:
 Is there a way tell dscanner that a variable is intentionally 
 unused?
I don't know about D-Scanner but I know that other similar tools allow you to prefix the name of a variable with an underscore to indicate that the variable is intentionally unused.
It seems a simple underscore "_" as a variable name tells Dscanner exactly that.
Jan 07
parent reply visitor <visitor gmail.com> writes:
On Sunday, 7 January 2018 at 16:29:42 UTC, visitor wrote:
 On Sunday, 7 January 2018 at 16:14:11 UTC, Jacob Carlborg wrote:
 On 2018-01-07 01:18, Ivan Trombley wrote:
 Is there a way tell dscanner that a variable is intentionally 
 unused?
I don't know about D-Scanner but I know that other similar tools allow you to prefix the name of a variable with an underscore to indicate that the variable is intentionally unused.
It seems a simple underscore "_" as a variable name tells Dscanner exactly that.
Any number of underscores but underscores only apparently.
Jan 07
parent Ivan Trombley <itrombley dot-borg.org> writes:
On Sunday, 7 January 2018 at 17:02:02 UTC, visitor wrote:
 It seems a simple underscore "_" as a variable name tells 
 Dscanner exactly that.
Any number of underscores but underscores only apparently.
That works too. Thanks.
Jan 07